Changeset 4156
- Timestamp:
- 09/28/16 12:52:27 (3 months ago)
- Location:
- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/css/sponsor-payments.css
r4155 r4156 1 1 .wcorg-sponsor-payment label, 2 2 .wcorg-sponsor-payment .control { 3 4 3 float: left; 4 clear: both; 5 5 } 6 6 7 7 .wcorg-sponsor-payment label { 8 8 margin-top: 10px; 9 9 } 10 10 11 11 .wcorg-sponsor-payment .clear { 12 13 12 display: block; 13 margin-bottom: 20px; 14 14 } -
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/sponsor-payment-stripe.php
r4155 r4156 22 22 function render() { 23 23 24 25 26 if ( empty( $keys['publishable'] ) || empty( $keys['secret'] ) || empty( $keys['wcorg_hmac'] ) )27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 24 // Make sure we have Stripe keys and an HMAC. 25 $keys = _get_keys(); 26 if ( empty( $keys['publishable'] ) || empty( $keys['secret'] ) || empty( $keys['hmac_key'] ) ) 27 return; 28 29 // Make sure WordCamp_Budgets is available. 30 require_once( __DIR__ . '/wordcamp-budgets.php' ); 31 32 $data = array( 33 'keys' => $keys, 34 'step' => STEP_SELECT_INVOICE, 35 'wordcamps' => _get_wordcamps(), 36 'currencies' => \WordCamp_Budgets::get_currencies(), 37 'errors' => array(), 38 ); 39 40 if ( ! empty( $_POST['sponsor_payment_submit'] ) ) { 41 _handle_post_data( $data ); // $data passed by ref. 42 } 43 44 wp_enqueue_style( 'wcb-sponsor-payments', plugins_url( 'css/sponsor-payments.css', __DIR__ ), array(), CSS_VERSION ); 45 require_once( dirname( __DIR__ ) . '/views/sponsor-payment/main.php' ); 46 46 } 47 47 … … 52 52 */ 53 53 function _get_keys() { 54 55 56 57 58 59 60 61 54 return apply_filters( 'wcorg_sponsor_payment_stripe', array( 55 // Stripe API credentials. 56 'publishable' => '', 57 'secret' => '', 58 59 // An HMAC key used to sign some data in between requests. 60 'hmac_key' => '', 61 ) ); 62 62 } 63 63 … … 68 68 */ 69 69 function _get_wordcamps() { 70 71 72 73 74 75 76 77 'orderby'=> 'title',78 'order'=> 'asc',79 'meta_query'=> array( array(80 'key'=> 'Start Date (YYYY-mm-dd)',81 'value'=> strtotime( '-14 days' ),82 'compare'=> '>'83 84 85 86 87 70 static $wordcamps; 71 72 if ( ! isset( $wordcamps ) ) { 73 $wordcamps = get_posts( array( 74 'post_type' => 'wordcamp', 75 'post_status' => \WordCamp_Loader::get_public_post_statuses(), 76 'posts_per_page' => -1, 77 'orderby' => 'title', 78 'order' => 'asc', 79 'meta_query' => array( array( 80 'key' => 'Start Date (YYYY-mm-dd)', 81 'value' => strtotime( '-14 days' ), 82 'compare' => '>' 83 ) ) 84 ) ); 85 } 86 87 return $wordcamps; 88 88 } 89 89 … … 98 98 */ 99 99 function _handle_post_data( &$data ) { 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 } 100 $step = isset( $_POST['step'] ) ? absint( $_POST['step'] ) : STEP_SELECT_INVOICE; 101 102 switch ( $_POST['step'] ) { 103 case STEP_SELECT_INVOICE: 104 // An invoice, event, currency and amount have been selected. 105 106 if ( empty( $_POST['currency'] ) ) { 107 $data['errors'][] = 'Please select a currency.'; 108 return; 109 } 110 111 $currency = $_POST['currency']; 112 if ( ! array_key_exists( $currency, $data['currencies'] ) or strpos( $currency, 'null' ) === 0 ) { 113 $data['errors'][] = 'Invalid currency.'; 114 return; 115 } 116 117 if ( empty( $_POST['amount'] ) ) { 118 $data['errors'][] = 'Please enter a payment amount.'; 119 return; 120 } 121 122 $amount = round( floatval( $_POST['amount'] ), 2 ); 123 if ( $amount < 1.00 ) { 124 $data['errors'][] = 'Amount can not be less than 1.00.'; 125 return; 126 } 127 128 if ( empty( $_POST['wordcamp_id'] ) ) { 129 $data['errors'][] = 'Please select an event.'; 130 return; 131 } 132 133 // Make sure the selected WordCamp is valid. 134 $wordcamp_id = absint( $_POST['wordcamp_id'] ); 135 $valid_ids = wp_list_pluck( _get_wordcamps(), 'ID' ); 136 137 if ( ! in_array( $wordcamp_id, $valid_ids ) ) { 138 $data['errors'][] = 'Please select a valid event.'; 139 return; 140 } 141 142 if ( empty( $_POST['invoice_id'] ) ) { 143 $data['errors'][] = 'Please provide a valid invoice ID.'; 144 return; 145 } 146 147 $invoice_id = absint( $_POST['invoice_id'] ); 148 $wordcamp_site_id = get_wordcamp_site_id( get_post( $wordcamp_id ) ); 149 if ( empty( $wordcamp_site_id ) ) { 150 $data['errors'][] = 'Could not find a site for this WordCamp.'; 151 return; 152 } 153 154 // Next step is to collect the card details via Stripe. 155 $data['step'] = STEP_PAYMENT_DETAILS; 156 $data['payment'] = array( 157 'currency' => $currency, 158 'amount' => $amount, 159 'wordcamp_id' => $wordcamp_id, 160 'invoice_id' => $invoice_id, 161 ); 162 163 // Passed through to the charge step. 164 $data['payment_data_json'] = json_encode( $data['payment'] ); 165 $data['payment_data_signature'] = hash_hmac( 'sha256', $data['payment_data_json'], $data['keys']['hmac_key'] ); 166 167 // Add a WordCamp object for convenience. 168 $data['payment']['wordcamp_obj'] = get_post( $wordcamp_id ); 169 break; 170 171 case STEP_PAYMENT_DETAILS: 172 // The card details have been entered and Stripe has submitted our form. 173 174 if ( empty( $_POST['stripeToken'] ) ) { 175 $data['errors'][] = 'Stripe token not found.'; 176 return; 177 } 178 179 // Make sure our data hasn't been altered. 180 $payment_data_str = wp_unslash( $_POST['payment_data_json'] ); 181 $payment_data = json_decode( $payment_data_str, true ); 182 if ( ! hash_equals( hash_hmac( 'sha256', $payment_data_str, $data['keys']['hmac_key'] ), $_POST['payment_data_signature'] ) ) { 183 $data['errors'][] = 'Could not verify payload signature.'; 184 return; 185 } 186 187 $wordcamp_obj = get_post( $payment_data['wordcamp_id'] ); 188 189 require_once( __DIR__ . '/stripe-php/init.php' ); 190 \Stripe\Stripe::setApiKey( $data['keys']['secret'] ); 191 192 try { 193 $charge = \Stripe\Charge::create( array( 194 'amount' => round( $payment_data['amount'], 2 ) * 100, 195 'currency' => $payment_data['currency'], 196 'source' => $_POST['stripeToken'], 197 'description' => 'WordCamp Sponsorship: ' . $wordcamp_obj->post_title, 198 'metadata' => array( 199 'invoice_id' => $payment_data['invoice_id'], 200 'wordcamp_id' => $payment_data['wordcamp_id'], 201 ), 202 ) ); 203 } catch ( \Stripe\Error\Card $e ) { 204 $data['errors'][] = 'The card has been declined.'; 205 return; 206 } catch ( \Exception $e ) { 207 $data['errors'][] = 'An unknown error has occurred, please try again later.'; 208 return; 209 } 210 211 // All good! 212 $data['step'] = STEP_PAYMENT_SUCCESS; 213 break; 214 } 215 } -
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/views/sponsor-payment/main.php
r4155 r4156 6 6 7 7 <div id="container"> 8 8 <div id="content" class="wcorg-sponsor-payment" role="main"> 9 9 10 10 <h1 class="entry-title">Sponsorship Payment</h1> 11 11 12 13 14 15 16 12 <?php if ( ! empty( $data['errors'] ) ) : ?> 13 <?php foreach ( $data['errors'] as $error ) : ?> 14 <p><strong>Error:</strong> <?php echo esc_html( $error ); ?></p> 15 <?php endforeach; ?> 16 <?php endif; ?> 17 17 18 18 <?php if ( $data['step'] == STEP_SELECT_INVOICE ) : ?> 19 19 20 20 <p>Use this form to pay your WordCamp sponsorship fee to WordPress Community Support, PBC. If you did not receive an invoice ID yet, please get in touch with the event's Sponsorships Coordinator for more information.</p> 21 21 22 23 24 22 <form method="POST"> 23 <input type="hidden" name="step" value="<?php echo STEP_SELECT_INVOICE; ?>" /> 24 <input type="hidden" name="sponsor_payment_submit" value="1" /> 25 25 26 27 28 29 30 31 32 33 34 26 <label>Event</label> 27 <div class="control"> 28 <select name="wordcamp_id"> 29 <option value="" disabled selected>Select a WordCamp</option> 30 <?php foreach ( $data['wordcamps'] as $wordcamp ) : ?> 31 <option value="<?php echo esc_attr( $wordcamp->ID ); ?>"><?php echo esc_html( $wordcamp->post_title ); ?></option> 32 <?php endforeach; ?> 33 </select> 34 </div> 35 35 36 37 38 39 36 <label>Invoice ID</label> 37 <div class="control"> 38 <input type="text" name="invoice_id" /> 39 </div> 40 40 41 42 43 44 45 46 47 48 49 50 51 41 <label>Currency</label> 42 <div class="control"> 43 <select name="currency"> 44 <option value="" disabled selected>Select a Currency</option> 45 <?php foreach ( $data['currencies'] as $currency_key => $currency_name ) : ?> 46 <option value="<?php echo esc_attr( $currency_key ); ?>"> 47 <?php echo esc_html( $currency_name ); ?> 48 </option> 49 <?php endforeach; ?> 50 </select> 51 </div> 52 52 53 54 55 56 53 <label>Amount</label> 54 <div class="control"> 55 <input type="text" name="amount" /> 56 </div> 57 57 58 58 <div class="clear"></div> 59 59 60 61 60 <input type="submit" value="Continue" /> 61 </form> 62 62 63 63 <?php elseif ( $data['step'] == STEP_PAYMENT_DETAILS ) : ?> 64 64 65 65 <p>Please review the details below and hit "Make a Payment" when you're ready.</p> 66 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 67 <table> 68 <tr> 69 <td>Invoice</td> 70 <td><?php echo esc_html( $data['payment']['invoice_id'] ); ?></td> 71 </tr> 72 <tr> 73 <td>Event</td> 74 <td><?php echo esc_html( $data['payment']['wordcamp_obj']->post_title ); ?></td> 75 </tr> 76 <tr> 77 <td>Currency</td> 78 <td><?php echo esc_html( $data['payment']['currency'] ); ?></td> 79 </tr> 80 <tr> 81 <td>Amount</td> 82 <td><?php echo number_format( round( $data['payment']['amount'], 2 ), 2, '.', ' ' ); ?></td> 83 </tr> 84 </table> 85 85 86 87 88 89 90 86 <form method="POST"> 87 <input type="hidden" name="step" value="<?php echo STEP_PAYMENT_DETAILS; ?>" /> 88 <input type="hidden" name="sponsor_payment_submit" value="1" /> 89 <input type="hidden" name="payment_data_json" value="<?php echo esc_attr( $data['payment_data_json'] ); ?>" /> 90 <input type="hidden" name="payment_data_signature" value="<?php echo esc_attr( $data['payment_data_signature'] ); ?>" /> 91 91 92 93 94 95 96 97 98 99 100 101 102 103 104 92 <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 93 data-key="<?php echo esc_attr( $data['keys']['publishable'] ); ?>" 94 data-amount="<?php echo esc_attr( round( $data['payment']['amount'], 2 ) * 100 ); ?>" 95 data-currency="<?php echo esc_attr( $data['payment']['currency'] ); ?>" 96 data-name="WordPress Community Support, PBC" 97 data-description="Event Sponsorship Payment" 98 data-image="https://stripe.com/img/documentation/checkout/marketplace.png" 99 data-locale="auto" 100 data-panel-label="Pay" 101 data-label="Make a Payment" 102 data-zip-code="true"> 103 </script> 104 </form> 105 105 106 106 <?php elseif ( $data['step'] == STEP_PAYMENT_SUCCESS ) : ?> 107 107 108 108 <p><strong>Success!</strong> Your payment has been received, thank you!</p> 109 109 110 111 112 113 110 <ul> 111 <li><a href="<?php echo esc_url( add_query_arg( 'again', 1 ) ); ?>">Make another payment</a></li> 112 <li><a href="<?php echo esc_url( home_url( '/' ) ); ?>">Go back to Central</a></li> 113 </ul> 114 114 115 115 <?php endif; ?> 116 116 117 117 </div><!-- #content --> 118 118 </div><!-- #container --> 119 119
Note: See TracChangeset
for help on using the changeset viewer.