Tennyx
Tennyx

Reputation: 153

Using Stripe.js for kickstarter-style application. How can I test cards before iterating over and charging them all?

So I am building an app that acts similarly to kickstarter/gofundme etc. in that once a price goal is met, it charges all the contributing customers. My process is, when a customer contributes towards the price goal I create a stripe customer and store their id:

    customer = stripe.Customer.create(
      email = <user_email>,
      source = <stripe_customer_token>
    )

I store this information in an array in my database. Once the price goal is met, I iterate over the array charging each customer using that initial card that stripe verified as legitimate.

    for customer in customer_array:
      charge = stripe.Charge.create(
        amount = <amount>,
        customer = <customer>,
      )

The problem I foresee is that sometimes these price goals will be up for weeks. On day 1, a customer could contribute and be verified by stripe as a legitimate card, but in 3 weeks that card could expire or be cancelled etc. Is there any way to run some sort of verification on a customer token before charging it to make sure it's still legitimate? I would hate to iterate over 9/10 customers in an array and charge them for being legit, then hit the final customer in the array with an invalid card. I am using stripe.js so hopefully there's a solution without incorporating the stripe api. Thanks.

Upvotes: 1

Views: 96

Answers (1)

Wils
Wils

Reputation: 1231

\Stripe\Charge::create(array(
  "amount" => 2000,
  "currency" => "usd",
  "capture" => false,
  "source" => "tok_mastercard", 
  "description" => "Charge for [email protected]"
));

First Charge it with capture false, and capture it afterwards using the id returned from above. With this method, you could validate a card with more confident. In extreme case, for example credit card cancel or stolen, the bank can overrule all reserved fund command.

$ch = \Stripe\Charge::retrieve("ch_1C9s1N2eZvKYlo2Cmpk4KBgS");
$ch->capture();

Upvotes: 1

Related Questions