Add admin UI: setup wizard, goals, rules editor, donations, moderation, overlays, settings
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0b9e6e7cbb
commit
4efdef72f6
18 files changed
+2169
No files matched your search
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Admin;
|
||||
|
||||
use StreamGoals\Core\Settings;
|
||||
use StreamGoals\PayPal\Client;
|
||||
use StreamGoals\PayPal\PayPalException;
|
||||
use StreamGoals\PayPal\WpCache;
|
||||
use StreamGoals\PayPal\WpTransport;
|
||||
|
||||
/** Saves PayPal credentials, tests them, and registers the webhook. Secrets are never echoed or logged. */
|
||||
final class PayPalConnector {
|
||||
|
||||
private const VERIFIED = 'sg_paypal_verified';
|
||||
|
||||
public static function fingerprint( array $s ): string {
|
||||
return hash_hmac( 'sha256', $s['paypal_mode'] . '|' . $s['paypal_client_id'] . '|' . $s['paypal_client_secret'], function_exists( 'wp_salt' ) ? wp_salt( 'auth' ) : 'sg' );
|
||||
}
|
||||
|
||||
/** True once Test connection succeeded for the credentials currently saved. */
|
||||
public static function isConnected(): bool {
|
||||
$s = Settings::all();
|
||||
return '' !== $s['paypal_client_id'] && '' !== $s['paypal_client_secret']
|
||||
&& hash_equals( (string) get_option( self::VERIFIED, '' ), self::fingerprint( $s ) );
|
||||
}
|
||||
|
||||
public static function webhookUrl(): string {
|
||||
return rest_url( 'streamgoals/v1/webhook' );
|
||||
}
|
||||
|
||||
/** Fields rendered by both the wizard and the Settings screen. */
|
||||
public static function fields(): void {
|
||||
$s = Settings::all();
|
||||
$mode = 'live' === $s['paypal_mode'] ? 'live' : 'sandbox';
|
||||
?>
|
||||
<div class="sg-tabs" role="tablist">
|
||||
<label class="sg-tab"><input type="radio" name="paypal_mode" value="sandbox" <?php checked( $mode, 'sandbox' ); ?>> <strong>Sandbox</strong> <span>(practice mode, fake money)</span></label>
|
||||
<label class="sg-tab"><input type="radio" name="paypal_mode" value="live" <?php checked( $mode, 'live' ); ?>> <strong>Live</strong> <span>(real money)</span></label>
|
||||
</div>
|
||||
<p class="description">Start with Sandbox to try everything for free. When you are ready, create a Live app the same way and switch here. Sandbox and Live use <em>different</em> Client IDs and Secrets.</p>
|
||||
<table class="form-table" role="presentation">
|
||||
<tr>
|
||||
<th scope="row"><label for="sg_client_id">Client ID</label></th>
|
||||
<td><input type="text" id="sg_client_id" name="paypal_client_id" class="regular-text code" autocomplete="off" spellcheck="false" value="<?php echo esc_attr( (string) $s['paypal_client_id'] ); ?>"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="sg_secret">Secret</label></th>
|
||||
<td>
|
||||
<input type="password" id="sg_secret" name="paypal_client_secret" class="regular-text code" autocomplete="new-password" spellcheck="false"
|
||||
value="" placeholder="<?php echo esc_attr( '' !== $s['paypal_client_secret'] ? Input::maskSecret( (string) $s['paypal_client_secret'] ) : 'Paste your Secret here' ); ?>">
|
||||
<p class="description"><?php echo '' !== $s['paypal_client_secret'] ? 'A Secret is saved. Leave this empty to keep it, or paste a new one to replace it.' : 'The Secret stays on your server and is never shown again.'; ?></p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function instructions(): void {
|
||||
?>
|
||||
<ol class="sg-steps">
|
||||
<li>You need a <strong>PayPal Business</strong> account (free). A personal account cannot receive donations this way. <a href="https://www.paypal.com/business" target="_blank" rel="noopener noreferrer">Create one</a> or upgrade your existing account in PayPal settings.</li>
|
||||
<li>Open <a href="https://developer.paypal.com/dashboard/applications" target="_blank" rel="noopener noreferrer">developer.paypal.com/dashboard/applications</a> and log in with that Business account.</li>
|
||||
<li>At the top, pick <strong>Sandbox</strong> (to practise) or <strong>Live</strong> (real money) and click <strong>Create App</strong>.</li>
|
||||
<li>Give it any name (for example "My stream"), keep the type <strong>Merchant</strong>, and click <strong>Create App</strong>.</li>
|
||||
<li>On the next page, copy the <strong>Client ID</strong>. Under <strong>Secret key 1</strong> click <strong>Show</strong> and copy the <strong>Secret</strong>.</li>
|
||||
<li>Choose the same mode below (Sandbox or Live), paste both values, and press the button. We will check them and set up payment notifications (the webhook) for you.</li>
|
||||
</ol>
|
||||
<p class="description">Webhook note: PayPal can only send notifications to a public <strong>https://</strong> website. On a local test site the webhook step will fail, which is fine for trying things out.</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
/** admin-post: save credentials, test, register webhook. */
|
||||
public static function handle(): void {
|
||||
Admin::guard( 'sg_paypal_connect' );
|
||||
$from = Admin::post_str( 'return', 20 );
|
||||
// Computed lazily: the wizard lives at a different menu slug depending on whether PayPal is connected.
|
||||
$to = static fn( int $step ): string => 'settings' === $from ? Admin::url( 'streamgoals-settings' ) : SetupPage::url( $step );
|
||||
|
||||
$old = Settings::all();
|
||||
$post = Admin::post_raw();
|
||||
$mode = ( $post['paypal_mode'] ?? '' ) === 'live' ? 'live' : 'sandbox';
|
||||
$cid = Input::text( $post['paypal_client_id'] ?? '', 200 );
|
||||
$sec = Input::secretUnchanged( (string) ( $post['paypal_client_secret'] ?? '' ) ) ? (string) $old['paypal_client_secret'] : Input::text( $post['paypal_client_secret'], 300 );
|
||||
|
||||
if ( '' === $cid || '' === $sec ) {
|
||||
Admin::redirect( $to( 1 ), 'error', 'Please enter both the Client ID and the Secret from your PayPal app.' );
|
||||
}
|
||||
$changed = $mode !== $old['paypal_mode'] || $cid !== $old['paypal_client_id'] || $sec !== $old['paypal_client_secret'];
|
||||
$update = array( 'paypal_mode' => $mode, 'paypal_client_id' => $cid, 'paypal_client_secret' => $sec );
|
||||
if ( $changed ) {
|
||||
$update['paypal_webhook_id'] = ''; // a webhook belongs to one specific app/mode
|
||||
delete_option( self::VERIFIED );
|
||||
}
|
||||
Settings::update( $update );
|
||||
|
||||
$client = new Client( new WpTransport(), new WpCache(), $mode, $cid, $sec );
|
||||
$err = $client->testConnection();
|
||||
if ( null !== $err ) {
|
||||
Admin::redirect( $to( 1 ), 'error', 'Connection failed. ' . self::scrub( $err, $sec ) . ' Double-check that you copied the right values and that the mode (Sandbox/Live) matches the app.' );
|
||||
}
|
||||
update_option( self::VERIFIED, self::fingerprint( Settings::all() ), false );
|
||||
|
||||
$msg = 'Connected to PayPal (' . ( 'live' === $mode ? 'Live' : 'Sandbox' ) . ').';
|
||||
$type = 'success';
|
||||
if ( '' === (string) Settings::get( 'paypal_webhook_id', '' ) ) {
|
||||
try {
|
||||
$id = $client->createWebhook( self::webhookUrl() );
|
||||
Settings::update( array( 'paypal_webhook_id' => $id ) );
|
||||
$msg .= ' Payment notifications (webhook) were set up automatically.';
|
||||
} catch ( PayPalException $e ) {
|
||||
$type = 'warning';
|
||||
$msg .= ' However, the webhook could not be registered automatically: ' . self::scrub( $e->getMessage(), $sec )
|
||||
. ' Webhooks need a public https:// site, and a webhook for this address may already exist in your PayPal app (delete it there and try again).';
|
||||
}
|
||||
}
|
||||
Admin::redirect( $to( 2 ), $type, $msg );
|
||||
}
|
||||
|
||||
/** Defensive: make sure an error text can never contain the secret or client id. */
|
||||
public static function scrub( string $text, string $secret ): string {
|
||||
$text = '' !== $secret ? str_replace( $secret, '[hidden]', $text ) : $text;
|
||||
return Input::text( $text, 400 );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user