Stripe Checkout - Subscription Issue with Recognized Stripe User

154 Views Asked by At

I have Stripe working great. Upon a customer's donation, a new Subscription is created, and it works great - except if Stripe recognizes the email and says, "Enter the verification code."

If the customer does that, for some reason, a new subscription is not created and the customer is not charged.

Here is my charge-monthly.php

<?php

require_once('init.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_**************");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$amount = $_POST['amount'];
$finalamount = $amount * 100;
$dollars = ".00";
$plan = "/month"; 
$dash = " - "; 
$monthlyplan = $amount .$dollars .$plan .$dash .$email; 


//Create monthly plan
$plan = \Stripe\Plan::create(array(
  "name" => $monthlyplan,
  "id" => $monthlyplan,
  "interval" => "month",
  "currency" => "usd",
  "amount" => $finalamount,
));


// Create a Customer
$customer = \Stripe\Customer::create(array(
  "source" => $token,
  "description" => "MONTHLY DONATION",
    "plan" => $monthlyplan, 
  "email" => $email, )
);


?>

Any ideas why when Stripe recognizes the user and he is "logged in" it does not allow me to create a subscription?

In the Stripe log, I receive this 400 error:

{
   "error": {
   "type": "invalid_request_error",
   "message": "Plan already exists."
   }
 }

But there definitely isn't a plan created... ah!

1

There are 1 best solutions below

0
On

The reason your request is failing is because if a user comes back with the same email address and wants to sign up for the same plan, you already have an existing plan with that name,

$monthlyplan = $amount .$dollars .$plan .$dash .$email;

so your call to \Stripe\Plan::create will return an error and cause the rest of your calls to fail here.

You could add something like a unique id or time to your plan id.

http://php.net/manual/en/function.time.php http://php.net/manual/en/function.uniqid.php

Some other ways that folks typically handle this are:

  • Create a single plan for $1, and then adjust the quantity when creating your subscription. So a monthly plan for $1 with quantity of 100, would charge $100 month.

  • Store the amount that a customer will pay within your application. Subscribe your customers to a $0/month plan. Use webhooks to listen for invoice.created events. Have your webhook handler add an Invoice Item every month for the balance.