ruby on rails - Create Customer w/ simple Stripe Checkout -
my aim to verify users card information , store info in customer object.
i run pay-upon delivery service , building hack protect against fake orders (we can charge people via strip dashboard if place false orders or don't show up).
a full stripe integration long term goal, need asap. i've read (re-read) the docs having trouble.
the simple stripe checkout works great, clueless how create customer there.
script:
<form action="/charge" method="post"> <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="test key" data-image="/square-image.png" data-name="food bazooka" data-description="customer verification. no charges :)" data-panel-label="confirm , verify" data-label="confirm"> </script> </form>
any feedback or ideas appreciated.
thanks!
you're using embedded form, need use custom forms , server side code.
you need first create single use token represents customer card form (assuming form contains credit card number, expiry date etc...)
taken docs:
form markup:
<form action="/customer" method="post" id="payment-form"> <span class="payment-errors"></span> <div class="form-row"> <label> <span>card number</span> <input type="text" size="20" data-stripe="number"/> </label> </div> <div class="form-row"> <label> <span>cvc</span> <input type="text" size="4" data-stripe="cvc"/> </label> </div> <div class="form-row"> <label> <span>expiration (mm/yyyy)</span> <input type="text" size="2" data-stripe="exp-month"/> </label> <span> / </span> <input type="text" size="4" data-stripe="exp-year"/> </div> <button type="submit">submit payment</button> </form>
javascript:
jquery(function($) { $('#payment-form').submit(function(event) { var $form = $(this); // disable submit button prevent repeated clicks $form.find('button').prop('disabled', true); stripe.card.createtoken($form, striperesponsehandler); // prevent form submitting default action return false; }); }); function striperesponsehandler(status, response) { var $form = $('#payment-form'); if (response.error) { // show errors on form $form.find('.payment-errors').text(response.error.message); $form.find('button').prop('disabled', false); } else { // response contains id , card, contains additional card details var token = response.id; // insert token form gets submitted server $form.append($('<input type="hidden" name="stripetoken" />').val(token)); // , submit $form.get(0).submit(); } };
this takes form, , adds hidden field called stripetoken
before submitting
notice form action /customer
i see you're using ruby on rails tag - need handle customer post controller
this you'll need do:
https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later
# set secret key: remember change live secret key in production # see keys here https://dashboard.stripe.com/account stripe.api_key = "sk_test_x9s2nhixfy399uonvakwjysn" # credit card details submitted form # notice stripetoken - hidden field token = params[:stripetoken] # create customer customer = stripe::customer.create( :card => token, :description => "payinguser@example.com" ) # charge customer instead of card # ** have commented block out, not want charge customer # stripe::charge.create( # :amount => 1000, # incents # :currency => "gbp", # :customer => customer.id # ) # save customer id in database can use later save_stripe_customer_id(user, customer.id)
Comments
Post a Comment