34

Is it possible in stripe payment,

First we will validate credit card using stripe, then we generate token and create customers. we will save token instead of credit card information in database and later we will make payments from customers on basis of token or customer instead of credit card information.

In javscript file how do we handle stripeResponseHandler and function stripeResponseHandler(status, response)?

because we have already generate token using

Stripe.createToken({
            number: cardNumber,
            cvc: cardCVC,
            exp_month: $('#expiration-month').val(),
            exp_year: $('#expiration-year').val()
        }, stripeResponseHandler);

in payment step how we call stripeResponseHandler?


Please understand the requirement

1- Save the Token by verifying credit card information, in this case no payment is involve. amount/price will zero(0).

2- Save this Token in your database, but this token is use once not many time.If use this Token later it will not work.

3- Create customer will create a customer at stripe.com and we can also save in our database but the reason is that we will not recharge from our site , we have to login at stripe.com and recharge from that site. when we recharge from that site , we are unable to save records of that in our database.Also at time of creating customer, we have to create a recharge for latter . we also need credit card again if we use this client. so this is the main issue.

4- we can make own function stripeResponseHandler , because through stripeResponseHandler verification of cards can be done at stripe.js at stripe server.

Brad Larson
  • 168,330
  • 45
  • 388
  • 563
Shahzad
  • 341
  • 1
  • 3
  • 3
  • 1
    Each time we need a new token for payment through credit card information, This example https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later , will just create a customer in stripe.com and we use this user id... This need amount but in our case first we verify credit card and save token and later after 1 day, 1 month or any time make payment from that user account. – Shahzad Aug 23 '13 at 04:22
  • For anyone stumbling upon this later, Stripe has a new API for [setting up future payments here.](https://stripe.com/docs/payments/save-and-reuse) – Brian Chan Apr 28 '20 at 19:15

5 Answers5

37

Instead of saving the token itself, I recommend creating a customer and saving your customer ID. You can then charge your customer at any time in the future. See our documentation on saving card details for later.

In javscript file how we handle stripeResponseHandler and function stripeResponseHandler(status, response).

You will need to create a function and pass it as your stripeResponseHandler when calling createToken. All this function needs to do is insert your token into your form and submit it. There's a simple example of that here: https://gist.github.com/boucher/1750375

Thomas Orozco
  • 45,796
  • 9
  • 97
  • 109
brian
  • 3,276
  • 2
  • 24
  • 35
24

In Stripe, in order to save a card (or bank account) to charge later, you have to create a customer, then add payment sources (card or bank_account) to that customer.

Once you create a customer with a payment source (or sources), you have 3 options to create a charge.

  1. Charge the customer using default source:

    Stripe::Charge.create(
        amount: 1000,
        currency: 'usd',
        customer: 'cus_xxxx'
    )
    
  2. Charge the customer using a credit card:

    Stripe::Charge.create(
        amount: 5000,
        currency: 'usd',
        customer: 'cus_xxxx',
        card: 'card_xxxx'
    )
    
  3. Charge the customer using a bank account:

    Stripe::Charge.create(
        amount: 8000,
        currency: 'usd',
        customer: 'cus_xxxx',
        bank_account: 'ba_xxxx'
    )
    
Pang
  • 8,605
  • 144
  • 77
  • 113
Châu Hồng Lĩnh
  • 1,880
  • 1
  • 16
  • 22
8

Instead of saving the tokens create a customer object and save only the card ids locally. When you make a payment you can optionally define the card to be charged.(If you pass the customer id to stripe.charges.create).In that case you do not have to pass the token. With this approach you do not need to deal with the default card either.

stripe.charges.create({
amount: 400,
currency: "usd",
card: "card_xxxxx", 
customer: "cus_xxxxxx", 
Ilker Baltaci
  • 10,869
  • 6
  • 57
  • 73
  • Note: that the response returns the customer_id, so it seems like it should work from the source (token) w/o the customer_id and then you can put it in your db after the payment succeeded... – Andy Hayden Jan 07 '16 at 06:08
2

Use this code snipped and later you can capture payment on transaction id by passing capture: true

token = params[:stripeToken]
    # Charge the user's card:
    charge = Stripe::Charge.create(
      :amount => 1000,
      :currency => "usd",
      :description => "Example charge",
      :capture => false,
      :source => token,
    )

for more detail information refer link as stated:

capture:- optional, default is true Whether or not to immediately capture the charge. When false, the charge issues an authorization (or pre-authorization), and will need to be captured later. Uncaptured charges expire in 7 days. For more information, see authorizing charges and settling later.

I think it solves the problem you are facing

Rahul Sharma
  • 1,172
  • 9
  • 18
0

For creating token, you first need to refer stripe.js from stripe.com

   <script src="https://js.stripe.com/v3/"></script>

And then add below code to add your card info and generate token.

    var stripe = Stripe('Your stripe publisheable key');
  var elements = stripe.elements;

stripe.createToken(elements[0], additionalData).then(function (result) {
                example.classList.remove('submitting');

                if (result.token) {
                    // If we received a token, show the token ID.
                    example.querySelector('.token').innerText = result.token.id;
                    example.classList.add('submitted');
                }

Here, you will get a token, that will be necessary to create your customer. Use below code to create your customer. I used C#.NET

StripeConfiguration.SetApiKey("sk_test_JTJYT2SJCb3JjLQQ4I5ShDLD");

var options = new CustomerCreateOptions {
  Description = "Customer for jenny.rosen@example.com",
  SourceToken = "tok_amex"
};

var service = new CustomerService();
Customer customer = service.Create(options);

Then, you can cut your price form this user from the card token you got from stripe like below:

StripeConfiguration.SetApiKey("sk_test_JTJYT2SJCb3JjLQQ4I5ShDLD");

var options = new ChargeCreateOptions {
    Amount = 2000,
    Currency = "aud",
    Description = "Charge for jenny.rosen@example.com",
    SourceId = "tok_amex" // obtained with Stripe.js, }; var service = new ChargeService(); Charge charge = service.Create(options);
Abdus Salam Azad
  • 3,109
  • 31
  • 23