Donate form: custom-amount label, visible minimum, fee-cover info icon; hard 1.00 floor; purge abandoned checkouts; drop refund filters; soften personal-account wording
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a259c0b0a1
commit
e22675f54b
13 files changed
+177
-15
No files matched your search
@@ -24,7 +24,7 @@ final class DonationsPage {
|
||||
|
||||
echo '<form method="get" class="sg-filter"><input type="hidden" name="page" value="streamgoals-donations">';
|
||||
echo '<select name="status" aria-label="Status"><option value="">All statuses</option>';
|
||||
foreach ( array( 'completed', 'pending', 'refunded', 'reversed' ) as $s ) {
|
||||
foreach ( array( 'completed', 'pending' ) as $s ) {
|
||||
echo '<option value="' . esc_attr( $s ) . '"' . selected( $status, $s, false ) . '>' . esc_html( ucfirst( $s ) ) . '</option>';
|
||||
}
|
||||
echo '</select> <label><input type="checkbox" name="tests" value="hide"' . checked( $tests, 'hide', false ) . '> Hide test donations</label> ';
|
||||
|
||||
@@ -59,7 +59,7 @@ final class PayPalConnector {
|
||||
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>To take <em>real</em> payments PayPal requires a <strong>PayPal Business</strong> account (free; your own name is fine). Practising in Sandbox needs no business account. <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>
|
||||
|
||||
@@ -33,7 +33,7 @@ final class SettingsPage {
|
||||
echo '<option value="' . esc_attr( $c ) . '"' . selected( $cur, $c, false ) . '>' . esc_html( $c . ' - ' . $name ) . '</option>';
|
||||
}
|
||||
echo '</select><p class="description">One currency for the whole site. It applies to goals you create from now on; existing goals keep their currency. Check the fixed fee below after changing it.</p></td></tr>';
|
||||
echo '<tr><th scope="row"><label for="sg_min">Minimum donation (' . esc_html( $cur ) . ')</label></th><td><input type="text" id="sg_min" name="min_donation" class="small-text" inputmode="decimal" value="' . esc_attr( Format::decimal( (int) $s['min_donation_minor'], $cur ) ) . '"></td></tr>';
|
||||
echo '<tr><th scope="row"><label for="sg_min">Minimum donation (' . esc_html( $cur ) . ')</label></th><td><input type="text" id="sg_min" name="min_donation" class="small-text" inputmode="decimal" value="' . esc_attr( Format::decimal( \StreamGoals\Core\Settings::effectiveMin( (int) $s['min_donation_minor'] ), $cur ) ) . '"> <span class="description">At least ' . esc_html( Format::money( \StreamGoals\Core\Settings::HARD_MIN_MINOR, $cur ) ) . '. Visitors cannot donate less than this.</span></td></tr>';
|
||||
echo '<tr><th scope="row"><label for="sg_len">Longest message</label></th><td><input type="number" id="sg_len" name="message_max_length" min="0" max="500" class="small-text" value="' . (int) $s['message_max_length'] . '"> characters <span class="description">(0 to 500; 0 = no messages)</span></td></tr>';
|
||||
echo '<tr><th scope="row">Names</th><td><fieldset>';
|
||||
foreach ( self::POLICIES as $k => $l ) {
|
||||
@@ -64,8 +64,8 @@ final class SettingsPage {
|
||||
$upd = array();
|
||||
|
||||
$min = Input::amountToMinor( $post['min_donation'] ?? '', $cur );
|
||||
if ( null === $min || $min < 1 ) {
|
||||
$errors[] = 'Minimum donation must be an amount greater than zero.';
|
||||
if ( null === $min || $min < \StreamGoals\Core\Settings::HARD_MIN_MINOR ) {
|
||||
$errors[] = 'Minimum donation must be at least ' . Format::money( \StreamGoals\Core\Settings::HARD_MIN_MINOR, $cur ) . '. PayPal\'s fixed fee makes smaller donations lose money.';
|
||||
} else {
|
||||
$upd['min_donation_minor'] = $min;
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ final class Store {
|
||||
$w = array( '1=1' );
|
||||
$args = array();
|
||||
$st = $f['status'] ?? '';
|
||||
if ( in_array( $st, array( 'pending', 'completed', 'refunded', 'reversed' ), true ) ) {
|
||||
if ( in_array( $st, array( 'pending', 'completed' ), true ) ) {
|
||||
$w[] = 'status = %s';
|
||||
$args[] = $st;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Core;
|
||||
|
||||
/**
|
||||
* Daily housekeeping: removes checkouts that were started but never paid.
|
||||
*
|
||||
* Only rows that never received a PayPal capture id are purged, and only after a week, so a slow payment
|
||||
* that PayPal finalizes later through the webhook is never discarded.
|
||||
*/
|
||||
final class Maintenance {
|
||||
|
||||
public const HOOK = 'sg_purge_abandoned';
|
||||
public const MAX_AGE_DAYS = 7;
|
||||
|
||||
public static function register(): void {
|
||||
add_action( self::HOOK, array( self::class, 'purge_abandoned' ) );
|
||||
if ( ! wp_next_scheduled( self::HOOK ) ) {
|
||||
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::HOOK );
|
||||
}
|
||||
}
|
||||
|
||||
public static function unschedule(): void {
|
||||
wp_clear_scheduled_hook( self::HOOK );
|
||||
}
|
||||
|
||||
/** @return int Number of rows removed. */
|
||||
public static function purge_abandoned(): int {
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'sg_donations';
|
||||
$cutoff = gmdate( 'Y-m-d H:i:s', time() - self::MAX_AGE_DAYS * DAY_IN_SECONDS );
|
||||
$n = $wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM {$table} WHERE status = 'pending' AND (paypal_capture_id IS NULL OR paypal_capture_id = '') AND goal_id > 0 AND created_at < %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
||||
$cutoff
|
||||
)
|
||||
);
|
||||
return (int) $n;
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ final class Plugin {
|
||||
|
||||
public static function boot(): void {
|
||||
Installer::maybe_upgrade();
|
||||
Maintenance::register();
|
||||
foreach ( self::MODULES as $module ) {
|
||||
if ( class_exists( $module ) && method_exists( $module, 'register' ) ) {
|
||||
$module::register();
|
||||
|
||||
@@ -8,6 +8,17 @@ namespace StreamGoals\Core;
|
||||
*/
|
||||
final class Settings {
|
||||
|
||||
/**
|
||||
* Absolute minimum donation in minor units (1.00 of the currency). PayPal's fixed per-transaction fee
|
||||
* makes smaller donations a net loss for the streamer, so the configured minimum can never go below this.
|
||||
*/
|
||||
public const HARD_MIN_MINOR = 100;
|
||||
|
||||
/** The minimum that is actually enforced: the configured value, but never below the hard floor. */
|
||||
public static function effectiveMin( int $configured ): int {
|
||||
return max( self::HARD_MIN_MINOR, $configured );
|
||||
}
|
||||
|
||||
public static function defaults(): array {
|
||||
return array(
|
||||
'paypal_mode' => 'sandbox', // sandbox | live
|
||||
|
||||
@@ -94,9 +94,9 @@ final class OrderValidator {
|
||||
} catch ( \InvalidArgumentException $e ) {
|
||||
throw new ValidationException( 'invalid_amount', 'Please enter a valid amount.' );
|
||||
}
|
||||
$min = (int) ( $settings['min_donation_minor'] ?? 0 );
|
||||
$min = \StreamGoals\Core\Settings::effectiveMin( (int) ( $settings['min_donation_minor'] ?? 0 ) );
|
||||
if ( $base <= 0 || $base < $min ) {
|
||||
throw new ValidationException( 'amount_too_small', 'The amount is below the minimum donation.' );
|
||||
throw new ValidationException( 'amount_too_small', 'The minimum donation is ' . ( new Money( $min, $currency ) )->toDecimalString() . ' ' . $currency . '.' );
|
||||
}
|
||||
if ( $base > 99999999 ) { // sanity cap (~$999,999.99)
|
||||
throw new ValidationException( 'amount_too_large', 'The amount is too large.' );
|
||||
|
||||
@@ -177,7 +177,7 @@ final class DonateController {
|
||||
'restUrl' => esc_url_raw( rest_url( self::NS ) ),
|
||||
'clientId' => (string) $s['paypal_client_id'],
|
||||
'policy' => (string) $s['anonymity_policy'],
|
||||
'minMinor' => (int) $s['min_donation_minor'],
|
||||
'minMinor' => \StreamGoals\Core\Settings::effectiveMin( (int) $s['min_donation_minor'] ),
|
||||
'messageMax' => min( 500, (int) $s['message_max_length'] ),
|
||||
'goals' => $goals,
|
||||
'i18n' => array(
|
||||
@@ -193,6 +193,14 @@ final class DonateController {
|
||||
'error' => __( 'Something went wrong. Please try again.', 'streamgoals-donate' ),
|
||||
'sdkError' => __( 'Could not load PayPal.', 'streamgoals-donate' ),
|
||||
'fixForm' => __( 'Please fix the form before paying.', 'streamgoals-donate' ),
|
||||
'customAmount' => __( 'Or enter your own amount', 'streamgoals-donate' ),
|
||||
'customPlaceholder' => __( 'Other amount', 'streamgoals-donate' ),
|
||||
/* translators: %s: formatted minimum amount, e.g. $1.00 */
|
||||
'minHint' => __( 'Minimum donation: %s', 'streamgoals-donate' ),
|
||||
/* translators: %s: formatted minimum amount, e.g. $1.00 */
|
||||
'belowMin' => __( 'The minimum donation is %s.', 'streamgoals-donate' ),
|
||||
'coverInfoLabel' => __( 'What does this mean?', 'streamgoals-donate' ),
|
||||
'coverInfo' => __( 'PayPal takes a small fee from every donation. Tick this box to add that fee to your total, so the full amount you chose reaches the streamer. It is optional.', 'streamgoals-donate' ),
|
||||
),
|
||||
);
|
||||
wp_add_inline_script( 'streamgoals-donate', 'window.StreamGoalsDonate=' . wp_json_encode( $config, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . ';', 'before' );
|
||||
|
||||
Reference in new issue
Block a user