php - Stripe token error -
i'm trying setup section on site collects card details (using stripe), save customer, , charge @ later date. looked through several tutorials , still getting error, in particular:
undefined variable: token in /applications/xampp/xamppfiles/htdocs/love-deals/admin/billing.php on line 21 failed save customer id db. want able save customer id users table in database (user created) used @ later date payments, life of me cant seem past error! appreciated.
thanks in advance
kaylee
here code far:
payment page, form:
<?php $userid = (int) $_get['id']; require('../inc/connect/config.php'); ?> <header> <!-- css --> <link href="admin.css" rel="stylesheet"> <link href="bootstrap.min.css" rel="stylesheet"> <script type="text/javascript" src="https://js.stripe.com/v2/"></script> <?php echo '<script type="text/javascript"> stripe.setpublishablekey("' . stripe_public_key . '"); </script>'; ?> <script type="text/javascript" src="../js/billing.js"></script> </header> <body> <form action="billing.php?id=<?php echo $userid; ?>" method="post" id="payment-form"> <span id="payment-errors" class=""></span> <div class="form-row"> <label> <span>card number</span> <input type="text" size="20" data-stripe="number" class="card-number"> </label> </div> <div class="form-row"> <label> <span>expiration (mm/yy)</span> <input type="text" size="2" data-stripe="exp_month" class="card-expiry-month"> </label> <span> / </span> <input type="text" size="2" data-stripe="exp_year" class="card-expiry-year"> </div> <div class="form-row"> <label> <span>cvc</span> <input type="text" size="4" data-stripe="cvc" class="card-cvc"> </label> </div> <input id="submitbtn" type="submit" class="submit" value="submit payment"> </form> </body>
billing.js:
function reporterror(msg) { // show error in form: $('#payment-errors').text(msg).addclass('alert alert-danger'); // re-enable submit button: $('#submitbtn').prop('disabled', false); return false; } // assumes jquery loaded! // watch document ready: $(document).ready(function() { // watch form submission: $("#payment-form").submit(function(event) { // flag variable: var error = false; // disable submit button prevent repeated clicks: $('#submitbtn').attr("disabled", "disabled"); // values: var ccnum = $('.card-number').val(), cvcnum = $('.card-cvc').val(), expmonth = $('.card-expiry-month').val(), expyear = $('.card-expiry-year').val(); // validate number: if (!stripe.card.validatecardnumber(ccnum)) { error = true; reporterror('the credit card number appears invalid.'); } // validate cvc: if (!stripe.card.validatecvc(cvcnum)) { error = true; reporterror('the cvc number appears invalid.'); } // validate expiration: if (!stripe.card.validateexpiry(expmonth, expyear)) { error = true; reporterror('the expiration date appears invalid.'); } // validate other form elements, if needed! // check errors: if (!error) { // stripe token: stripe.card.createtoken({ number: ccnum, cvc: cvcnum, exp_month: expmonth, exp_year: expyear }, striperesponsehandler); } // prevent form submitting: return false; }); // form submission }); // document ready. // function handles stripe response: function striperesponsehandler(status, response) { // check error: if (response.error) { reporterror(response.error.message); } else { // no errors, submit form: var f = $("#payment-form"); // token contains id, last4, , card type: var token = response['id']; // insert token form gets submitted server f.append("<input type='hidden' name='stripetoken' value='" + token + "' />"); // submit form: f.get(0).submit(); } } // end of striperesponsehandler() function.
and billing.php:
<?php $userid = (int) $_get['id']; require('../inc/connect/config.php'); require_once('inc/stripe-php/init.php'); session_start(); \stripe\stripe::setapikey(stripe_private_key); if ($_server['request_method'] == 'post') { $errors = array(); if (isset($_post['stripetoken'])) { $token = $_post['stripetoken']; } else { $errors['token'] = 'your payment details cannot processed. have not been charged. please confirm have javascript enabled , try again.'; } } // end of form submission conditional. // create customer $customer = \stripe\customer::create(array( "source" => $token, "description" => "example customer") ); $custid = $customer->id; try { $sql = 'insert users(cust) values(:cust) id :id'; $query = $db->prepare($sql); $query->execute(array(':cust'=>$custid, ':id'=>$userid)); header('location: ../admin/signup.php?joined'); } catch (pdoexception $e) { echo 'failed save customer id db'; } ?>
here updated php:
<?php $userid = (int) $_get['id']; require('../inc/connect/config.php'); require_once('inc/stripe-php/init.php'); session_start(); if ($_server['request_method'] == 'post') { $errors = array(); if (isset($_post['stripetoken'])) { $token = $_post['stripetoken']; // check duplicate submission, in case: // uses sessions, use cookie instead. if (isset($_session['token']) && ($_session['token'] == $token)) { $errors['token'] = 'you have apparently resubmitted form. please not that.'; } else { // new submission. $_session['token'] = $token; } } else { $errors['token'] = 'your payment details cannot processed. have not been charged. please confirm have javascript enabled , try again.'; } if(empty($errors)) { \stripe\stripe::setapikey(stripe_private_key); // create customer $customer = \stripe\customer::create(array( "source" => $token, "description" => "example customer") ); $custid = $customer->id; try { $sql = 'insert users(cust) values(:cust) id :id'; $query = $db->prepare($sql); $query->execute(array(':cust'=>$custid, ':id'=>$userid)); header('location: ../admin/signup.php?joined'); } catch (pdoexception $e) { echo 'failed save customer id db'; } } } // end of form submission conditional. ?>
Comments
Post a Comment