6
votes

Hi i have seen the code for stripe payment as below. First create a customer object

$customer = \Stripe\Customer::create(array(
          "card" => $token,
          "description" => "Product Purchase for Book",
          "email" => "[email protected]"
 ));

Then charge by using that customer object

  \Stripe\Charge::create(array(
         "amount" => $amount, # amount in cents, again
         "currency" => 'usd',
         "customer" => $customer->id)
   );

But below is the code which can be used to charge the user directly without creating any customer object.

\Stripe\Charge::create(array(
       "amount" => 3000,
       "currency" => "eur",
       "card" => $_POST['stripeToken'],
       "description" => $_POST['email'],
       "metadata" => array("order_id" => "6735", "userid" => '1111')
));

So can you please explain me below things

  1. Which one is better?
  2. What is the benefit of creating a customer object?
  3. Can use store and use that customer object to charge that user any any time say recurring payment?

Thanks in advance

1

1 Answers

8
votes

There are several benefits to creating the customer object first:

  1. You can charge multiple items to the same user, thus providing both you and the customer a billing history. This is valuable to you as a store owner (e.g. offering deals to repeat customers) and valuable to the customer to quickly pull up their purchase history.

  2. Fraud prevention

  3. Trends and analytics

  4. As you've already stated, subscriptions

A footnote: I personally maintain my own user-base and update both the Stripe customer object and my user data when a transaction occurs. This allows me to extend the Stripe customer with my own custom data and run complex analytics to discover trends.