Stripe Making Checkout Pages Using PHP | Lon Hosford www.lonhosford.com Using Stripe JS and Ajax START SNIPPET #1: SNIPPET #2:

Checkout

SNIPPET #3:

Loading ...

SNIPPET #4:

Processing ...

Thank you for your order.

Something Went Horribly Wrong!

SNIPPET #5: display:none; CHECK POINT #1: SNIPPET #6: SNIPPET #7: SNIPPET #8: // JQ shorthand for $(document).ready() $(function() { console.log("HELLO JQ"); /** * Click event for checkout */ $('#checkout-btn').click(function(e){ console.log("$('#checkout-btn')"); }); }); SNIPPET #9: /** * Create a Stripe configuration object */ var stripeConfig = { }; /** * Create a Stripe form handler object */ var stripeFormHandler = StripeCheckout.configure(stripeConfig); SNIPPET #10 // Open Stripe checkout form stripeFormHandler.open({ amount:$('#amount').val() * 100 }); e.preventDefault(); SNIPPET #11: key:$('#stripe-pk').val(), SNIPPET #12: image:"logo_128x128.png", description:$('#quantity').val() + ' ' + $('#description').val(), //panelLabel:'Order total', name :$("#company-name").val(), //allowRememberMe:false, SNIPPET #13: token:function(token){ console.log("token", token); } SNIPPET #14: /** * Close Checkout on page navigation */ $(window).on('popstate', function() { stripeFormHandler.close(); }); SNIPPET #15: display:none; SNIPPET #16: display:block; SNIPPET #17: $('#checkout-loading-message').hide(); $('#checkout-btn-container').show(); CHECK POINT #2: SNIPPET #18: include_once 'common.inc.php'; /** * Charge a Customer Card Via Stripe */ SNIPPET #19: //Disable HTTP Cache header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Pragma: no-cache'); // HTTP 1.0. header('Expires: 0'); // Proxies. SNIPPET #20: //Respond with JSON content header('Content-Type: application/json'); SNIPPET #21: echo json_encode($return_data); SNIPPET #22: dataSend = {}; dataSend.stripeToken = token.id; dataSend.amount = $('#amount').val(); dataSend.quantity = $('#quantity').val(); dataSend.statement_descriptor = $('#statement-descriptor').val(); dataSend.description = $("#description").val(); dataSend.receipt_email = token.email; console.log("dataSend", dataSend); SNIPPET #23: $.ajax({ type:"post", url:'checkout_charge_card.php', data:dataSend, dataType:'json', }) .done(function(data, status){ console.log("$.ajax.done"); console.log("data: ", data); console.log("status: ", status); }) .fail(function(data, status, error){ console.log("$.ajax.fail"); console.log("data: ", data); console.log("status: ", status); console.log("error: ", error); }); SNIPPET #24: // Create the charge on Stripe's servers - this will charge the user's card try{ // Create a Stripe\Charge OOP object $charge = \Stripe\Charge::create(array( "amount" => $_POST['amount'] * 100, // Convert to cents "currency" => "usd", "source" => $_POST['stripeToken'], "receipt_email" => $_POST['receipt_email'], "statement_descriptor" => substr(strtoupper($_POST['statement_descriptor']), 0, 22), "description" => $_POST['quantity'] . ' ' . $_POST['description']) ); $return_data["success"] = true; $return_data["charge"]["id"] = $charge->id; $return_data["charge"]["status"] = $charge->status; //Invalid request errors arise when your request has invalid parameters. }catch(\Stripe\Error\InvalidRequest $e){ } SNIPPET #25: //$return_data["charge"]["id"] = $charge->id; //$return_data["charge"]["status"] = $charge->status; CHECK POINT #3: SNIPPET #26: $('#checkout-btn').hide() $('#checkout-processing-message').show(); SNIPPET #27: $('#checkout-processing-message').hide(); if (data.success){ $('#checkout-success-message').show(); }else{ $('#checkout-fail-message').show(); } SNIPPET #28: dataSend.amount = "ABC"; SNIPPET #29: $return_data['error']['httpStatus'] = $e->getHttpStatus() ; $body = $e->getJsonBody(); $error_info = $body['error']; $return_data['error']['type'] = $error_info['type']; if(isset($error_info['message'])){ $return_data['error']['message'] = $error_info['message']; } SNIPPET #30: error_log("CRITICAL ERROR: Stripe InvalidRequest"); error_log('Stripe InvalidRequest - httpStatus:' . $e->getHttpStatus()); $body = $e->getJsonBody(); $error_info = $body['error']; if(isset($error_info['message'])){ error_log('Stripe InvalidRequest - message:' . $error_info['message']); } SNIPPET #31: dataSend.amount = $('#amount').val(); SNIPPET #32: $('#checkout-processing-message').hide(); $('#checkout-fail-message').show(); SNIPPET #33: //include_once 'common.inc.php'; SNIPPET #34: include_once 'common.inc.php'; COMPLETED