user3304303
user3304303

Reputation: 1043

How do you create a charge for existing Stripe Customer and associate the charge to that Customer?

Sorry if this is dumb question, but I can't find clear answer anywhere. I have my system currently setup where someone signs up on my website and I immediately create a customer in Stripe (no payments yet, I just put their info in my database AND create a customer in Stripe).

So later on, I offer my users a chance to make a one-time payment for their Monthly service (NOTE: They are not on a subscription, they just prefer to pay each month without signing up for subscription/auto-pay). I'd like to associate their payment with their already existing customer in Stripe. Is that possible, or am I doing the flow incorrectly?

I tried adding the "id" line to this below (thinking it would associate the charge with that existing Stripe customer), but it kicked an error.

$charge = \Stripe\Charge::create(array(
  "amount" => $_SESSION['stripeprice'],
  "currency" => "usd",
  "description" => "Payment for " . $_SESSION['frequency'],
  "receipt_email" => $_SESSION['email'],
  "source" => $token,
  "id" => "cus_C7m7BYvF6Mr0Do"
));

Any info would be appreciated as I am spinning my wheels trying to figure out how future NON-subscription payments can be assigned to existing Stripe customers, or if that's not how it's supposed to work (i.e. maybe I am thinking about it wrong)

Upvotes: 4

Views: 1484

Answers (1)

russpol
russpol

Reputation: 116

Instead of "id", use "customer" to attach the charge to a customer

Edit: If both source and customer are provided, the source must be attached to that customer https://stripe.com/docs/api/php#attach_source. If just customer is provided, Stripe will use the default source for that customer, which you can do by updating the customer with a source token https://stripe.com/docs/api/php#update_customer

Upvotes: 4

Related Questions