Changeset 6709 for sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/sponsor-payment-stripe.php
- Timestamp:
- 02/21/2018 07:41:06 PM (8 years ago)
- 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 15 15 const STEP_PAYMENT_DETAILS = 2; 16 16 const STEP_PAYMENT_SUCCESS = 3; 17 const CSS_VERSION = 1; 17 const JS_VERSION = 1; 18 const CSS_VERSION = 2; 18 19 19 20 /** … … 33 34 34 35 $data = array( 35 'keys' => $keys,36 'step' => STEP_SELECT_INVOICE,36 'keys' => $keys, 37 'step' => STEP_SELECT_INVOICE, 37 38 '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(), 40 41 ); 41 42 42 if ( ! empty( $_POST['sponsor_payment_submit'] ) ) { 43 $submitted = filter_input( INPUT_POST, 'sponsor_payment_submit' ); 44 45 if ( $submitted ) { 43 46 _handle_post_data( $data ); // $data passed by ref. 44 47 } 45 48 46 49 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 47 64 require_once( dirname( __DIR__ ) . '/views/sponsor-payment/main.php' ); 48 65 } … … 80 97 array( 81 98 'key' => 'Start Date (YYYY-mm-dd)', 82 'value' => strtotime( '- 3 months' ),99 'value' => strtotime( '-2 years' ), 83 100 'compare' => '>' 84 101 ) … … 97 114 */ 98 115 function _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 ) { 102 119 // 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 ) { 105 169 $data['errors'][] = 'Please select a currency.'; 106 170 return; 107 171 } 108 172 109 $currency = $_POST['currency'];110 173 if ( ! array_key_exists( $currency, $data['currencies'] ) || false !== strpos( $currency, 'null' ) ) { 111 174 $data['errors'][] = 'Invalid currency.'; … … 113 176 } 114 177 115 if ( empty( $_POST['amount'] ) ) { 178 $amount = round( $amount, 2 ); 179 180 if ( ! $amount ) { 116 181 $data['errors'][] = 'Please enter a payment amount.'; 117 182 return; 118 183 } 119 184 120 $amount = round( floatval( $_POST['amount'] ), 2 );121 185 if ( $amount < 1.00 ) { 122 186 $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.';149 187 return; 150 188 } … … 153 191 $data['step'] = STEP_PAYMENT_DETAILS; 154 192 $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, 159 199 ); 160 200 … … 168 208 169 209 // 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 ) { 172 216 $data['errors'][] = 'Stripe token not found.'; 173 217 return; 174 218 } 175 219 220 if ( ! $payment_data_json || ! $payment_data_signature ) { 221 $data['errors'][] = 'Payment data is missing.'; 222 return; 223 } 224 176 225 // 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 ) ) { 180 229 $data['errors'][] = 'Could not verify payload signature.'; 181 230 return; 182 231 } 183 232 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 } 187 256 188 257 $body = array( 189 'amount' => round( $payment_data['amount'], 2 ) * 100, 258 'amount' => round( $payment_data['amount'], 2 ) * 100, // TODO handle zero-decimal currencies. 190 259 '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, 199 263 ); 200 264 … … 203 267 $charge = $stripe->charge( $body ); 204 268 } 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 ); 206 273 return; 207 274 }
Note: See TracChangeset
for help on using the changeset viewer.