Making WordPress.org


Ignore:
Timestamp:
02/21/2018 07:41:06 PM (8 years ago)
Author:
coreymckrill
Message:

WordCamp Payments: Add an "other" option on the Sponsorship Payment page

This allows a payment to be submitted that isn't associated with a particular
WordCamp or sponsor invoice.

Props coreymckrill

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/sponsor-payment-stripe.php

    r6611 r6709  
    1515const STEP_PAYMENT_DETAILS = 2;
    1616const STEP_PAYMENT_SUCCESS = 3;
    17 const CSS_VERSION          = 1;
     17const JS_VERSION           = 1;
     18const CSS_VERSION          = 2;
    1819
    1920/**
     
    3334
    3435    $data = array(
    35         'keys'       => $keys,
    36         'step'       => STEP_SELECT_INVOICE,
     36        'keys'                   => $keys,
     37        'step'                   => STEP_SELECT_INVOICE,
    3738        'wordcamp_query_options' => get_wordcamp_query_options(),
    38         'currencies' => WordCamp_Budgets::get_currencies(),
    39         'errors'     => array(),
     39        'currencies'             => WordCamp_Budgets::get_currencies(),
     40        'errors'                 => array(),
    4041    );
    4142
    42     if ( ! empty( $_POST['sponsor_payment_submit'] ) ) {
     43    $submitted = filter_input( INPUT_POST, 'sponsor_payment_submit' );
     44
     45    if ( $submitted ) {
    4346        _handle_post_data( $data ); // $data passed by ref.
    4447    }
    4548
    4649    wp_enqueue_style( 'wcb-sponsor-payments', plugins_url( 'css/sponsor-payments.css', __DIR__ ), array(), CSS_VERSION );
     50    wp_enqueue_script( 'wcb-sponsor-payments', plugins_url( 'javascript/sponsor-payments.js', __DIR__ ), array( 'jquery' ), JS_VERSION, true );
     51
     52    wp_localize_script(
     53        'wcb-sponsor-payments',
     54        'WordCampSponsorPayments',
     55        array(
     56            'steps' => array(
     57                'select-invoice'  => STEP_SELECT_INVOICE,
     58                'payment-details' => STEP_PAYMENT_DETAILS,
     59                'payment-success' => STEP_PAYMENT_SUCCESS,
     60            ),
     61        )
     62    );
     63
    4764    require_once( dirname( __DIR__ ) . '/views/sponsor-payment/main.php' );
    4865}
     
    8097            array(
    8198                'key'     => 'Start Date (YYYY-mm-dd)',
    82                 'value'   => strtotime( '-3 months' ),
     99                'value'   => strtotime( '-2 years' ),
    83100                'compare' => '>'
    84101            )
     
    97114 */
    98115function _handle_post_data( &$data ) {
    99     $step = isset( $_POST['step'] ) ? absint( $_POST['step'] ) : STEP_SELECT_INVOICE;
    100 
    101     switch ( $_POST['step'] ) {
     116    $step = filter_input( INPUT_POST, 'step' );
     117
     118    switch ( $step ) {
    102119        // An invoice, event, currency and amount have been selected.
    103         case STEP_SELECT_INVOICE:
    104             if ( empty( $_POST['currency'] ) ) {
     120        default :
     121        case STEP_SELECT_INVOICE :
     122            $payment_type = filter_input( INPUT_POST, 'payment_type' );
     123            $wordcamp_id  = filter_input( INPUT_POST, 'wordcamp_id', FILTER_VALIDATE_INT );
     124            $invoice_id   = filter_input( INPUT_POST, 'invoice_id', FILTER_VALIDATE_INT );
     125            $description  = filter_input( INPUT_POST, 'description' );
     126            $currency     = filter_input( INPUT_POST, 'currency' );
     127            $amount       = filter_input( INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT );
     128
     129            switch ( $payment_type ) {
     130                default :
     131                case 'invoice' :
     132                    if ( ! $wordcamp_id ) {
     133                        $data['errors'][] = 'Please select an event.';
     134                        return;
     135                    }
     136
     137                    // Make sure the selected WordCamp is valid.
     138                    $valid_ids = wp_list_pluck( get_wordcamps( get_wordcamp_query_options() ), 'ID' );
     139
     140                    if ( ! in_array( $wordcamp_id, $valid_ids ) ) {
     141                        $data['errors'][] = 'Please select a valid event.';
     142                        return;
     143                    }
     144
     145                    $wordcamp_site_id = get_wordcamp_site_id( get_post( $wordcamp_id ) );
     146
     147                    if ( empty( $wordcamp_site_id ) ) {
     148                        $data['errors'][] = 'Could not find a site for this WordCamp.';
     149                        return;
     150                    }
     151
     152                    if ( ! $invoice_id ) {
     153                        $data['errors'][] = 'Please provide a valid invoice ID.';
     154                        return;
     155                    }
     156                    break;
     157
     158                case 'other' :
     159                    $description = substr( sanitize_text_field( $description ), 0, 100 );
     160
     161                    if ( ! $description ) {
     162                        $data['errors'][] = 'Please describe the purpose of the payment.';
     163                        return;
     164                    }
     165                    break;
     166            }
     167
     168            if ( ! $currency ) {
    105169                $data['errors'][] = 'Please select a currency.';
    106170                return;
    107171            }
    108172
    109             $currency = $_POST['currency'];
    110173            if ( ! array_key_exists( $currency, $data['currencies'] ) || false !== strpos( $currency, 'null' ) ) {
    111174                $data['errors'][] = 'Invalid currency.';
     
    113176            }
    114177
    115             if ( empty( $_POST['amount'] ) ) {
     178            $amount = round( $amount, 2 );
     179
     180            if ( ! $amount ) {
    116181                $data['errors'][] = 'Please enter a payment amount.';
    117182                return;
    118183            }
    119184
    120             $amount = round( floatval( $_POST['amount'] ), 2 );
    121185            if ( $amount < 1.00 ) {
    122186                $data['errors'][] = 'Amount can not be less than 1.00.';
    123                 return;
    124             }
    125 
    126             if ( empty( $_POST['wordcamp_id'] ) ) {
    127                 $data['errors'][] = 'Please select an event.';
    128                 return;
    129             }
    130 
    131             // Make sure the selected WordCamp is valid.
    132             $wordcamp_id = absint( $_POST['wordcamp_id'] );
    133             $valid_ids   = wp_list_pluck( get_wordcamps( get_wordcamp_query_options() ), 'ID' );
    134 
    135             if ( ! in_array( $wordcamp_id, $valid_ids ) ) {
    136                 $data['errors'][] = 'Please select a valid event.';
    137                 return;
    138             }
    139 
    140             if ( empty( $_POST['invoice_id'] ) ) {
    141                 $data['errors'][] = 'Please provide a valid invoice ID.';
    142                 return;
    143             }
    144 
    145             $invoice_id       = absint( $_POST['invoice_id'] );
    146             $wordcamp_site_id = get_wordcamp_site_id( get_post( $wordcamp_id ) );
    147             if ( empty( $wordcamp_site_id ) ) {
    148                 $data['errors'][] = 'Could not find a site for this WordCamp.';
    149187                return;
    150188            }
     
    153191            $data['step']    = STEP_PAYMENT_DETAILS;
    154192            $data['payment'] = array(
    155                 'currency'    => $currency,
    156                 'amount'      => $amount,
    157                 'wordcamp_id' => $wordcamp_id,
    158                 'invoice_id'  => $invoice_id,
     193                'payment_type' => $payment_type,
     194                'wordcamp_id'  => $wordcamp_id,
     195                'invoice_id'   => $invoice_id,
     196                'description'  => $description,
     197                'currency'     => $currency,
     198                'amount'       => $amount,
    159199            );
    160200
     
    168208
    169209        // The card details have been entered and Stripe has submitted our form.
    170         case STEP_PAYMENT_DETAILS:
    171             if ( empty( $_POST['stripeToken'] ) ) {
     210        case STEP_PAYMENT_DETAILS :
     211            $stripe_token           = filter_input( INPUT_POST, 'stripeToken' );
     212            $payment_data_json      = filter_input( INPUT_POST, 'payment_data_json' );
     213            $payment_data_signature = filter_input( INPUT_POST, 'payment_data_signature' );
     214
     215            if ( ! $stripe_token ) {
    172216                $data['errors'][] = 'Stripe token not found.';
    173217                return;
    174218            }
    175219
     220            if ( ! $payment_data_json || ! $payment_data_signature ) {
     221                $data['errors'][] = 'Payment data is missing.';
     222                return;
     223            }
     224
    176225            // Make sure our data hasn't been altered.
    177             $payment_data_str = wp_unslash( $_POST['payment_data_json'] );
    178             $payment_data     = json_decode( $payment_data_str, true );
    179             if ( ! hash_equals( hash_hmac( 'sha256', $payment_data_str, $data['keys']['hmac_key'] ), $_POST['payment_data_signature'] ) ) {
     226            $payment_data_str = wp_unslash( $payment_data_json );
     227
     228            if ( ! hash_equals( hash_hmac( 'sha256', $payment_data_str, $data['keys']['hmac_key'] ), $payment_data_signature ) ) {
    180229                $data['errors'][] = 'Could not verify payload signature.';
    181230                return;
    182231            }
    183232
    184             $wordcamp_obj      = get_post( $payment_data['wordcamp_id'] );
    185             $wordcamp_site_id  = get_wordcamp_site_id( $wordcamp_obj );
    186             $wordcamp_site_url = set_url_scheme( esc_url_raw( get_blog_option( $wordcamp_site_id, 'home', '' ) ), 'https' );
     233            $payment_data = json_decode( $payment_data_str, true );
     234
     235            switch ( $payment_data['payment_type'] ) {
     236                case 'invoice' :
     237                    $wordcamp_obj     = get_post( $payment_data['wordcamp_id'] );
     238                    $wordcamp_site_id = get_wordcamp_site_id( $wordcamp_obj );
     239
     240                    $description = sprintf( 'WordCamp Sponsorship: %s', get_wordcamp_name( $wordcamp_site_id ) );
     241                    $metadata    = array(
     242                        'invoice_id'       => $payment_data['invoice_id'],
     243                        'wordcamp_id'      => $payment_data['wordcamp_id'],
     244                        'wordcamp_site_id' => $wordcamp_site_id,
     245                        'wordcamp_url'     => set_url_scheme( esc_url_raw( get_blog_option( $wordcamp_site_id, 'home', '' ) ), 'https' ),
     246                    );
     247                    break;
     248
     249                case 'other' :
     250                    $description = 'Other Payment';
     251                    $metadata    = array(
     252                        'description' => $payment_data['description'],
     253                    );
     254                    break;
     255            }
    187256
    188257            $body = array(
    189                 'amount'      => round( $payment_data['amount'], 2 ) * 100,
     258                'amount'      => round( $payment_data['amount'], 2 ) * 100, // TODO handle zero-decimal currencies.
    190259                'currency'    => $payment_data['currency'],
    191                 'source'      => $_POST['stripeToken'],
    192                 'description' => 'WordCamp Sponsorship: ' . $wordcamp_obj->post_title,
    193                 'metadata'    => array(
    194                     'invoice_id'       => $payment_data['invoice_id'],
    195                     'wordcamp_id'      => $payment_data['wordcamp_id'],
    196                     'wordcamp_site_id' => $wordcamp_site_id,
    197                     'wordcamp_url'     => $wordcamp_site_url,
    198                 ),
     260                'source'      => $stripe_token,
     261                'description' => $description,
     262                'metadata'    => $metadata,
    199263            );
    200264
     
    203267                $charge = $stripe->charge( $body );
    204268            } catch ( Exception $exception ) {
    205                 $data['errors'][] = "An error occurred, please try another card. If that doesn't work, please contact ". EMAIL_CENTRAL_SUPPORT .".";
     269                $data['errors'][] = sprintf(
     270                    "An error occurred, please try another card. If that doesn't work, please contact %s.",
     271                    EMAIL_CENTRAL_SUPPORT
     272                );
    206273                return;
    207274            }
Note: See TracChangeset for help on using the changeset viewer.