0
votes

I am instantiating stripe with composer and attempting to subscribe a customer to a subscription plan in test mode using the card number 4000 0000 0000 0341. The documentation says this will be accepted but then throw an error when used to create a subscription. My questions is, why does the create subscription step always return success with this card number?

My charge code is

<form action="subscribe.php" method="post">
                            <input type="hidden" name="customer" value="<? echo $stripeID ?>">
                            <input type="hidden" name="plan" value="<? echo $thisPlan['id'] ?>">
                            <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                                    data-key="<?php echo $stripe['publishable_key']; ?>"
                                    data-name="www.example.com"
                                    data-description="<? echo $thisPlan['name'] ?>"
                                    data-amount="<? echo $thisPlan['amount'] ?>"
                                    data-locale="auto"
                                    data-panel-label="Subscribe Now"
                                    data-label="Subscribe"
                                    data-allow-remember-me="false">
                          </script>
                        </form>

and my subscribe.php is

    try {
    $result = \Stripe\Subscription::create(array(
      "customer" => $_POST['customer'],
      "plan" => $_POST['plan']
    ));
  echo "Subscription successful";
}
catch (Stripe\Error\Base $e) {
  // Code to do something with the $e exception object when an error occurs
  echo($e->getMessage());
} catch (Exception $e) {
  // Catch any other non-Stripe exceptions
}
1
What are the plan details? That test card won't fail until a charge is attempted, so if the plan is free or has a free trial then the subscription will successfully create.John C
Thanks John. The plan(s) are recurring with positive amounts and no free trial period so I was expecting the first charge to be immediate. Is that not so? On this page: stripe.com/docs/recipes/subscription-signup it states, under testing errors, "she will get an error when she tries to create the subscription. She hits the “Pay $20.00” button and this time instead of being taken to her thank you page, she is redirected to her error page.user1142052
I think I have found the answer. The customer had been setup with default card 4242 4242 4242 4242 and it was that which was used, not the 0341 number which I entered into the charge form. So a customer can't override the cardnumber to be used for a different subscription.user1142052
Makes sense, I just did a test with that card number and got an 'Your card was declined.' as expected.John C
I've confirmed that now by deleting the old card and retrying with the 0341 number. The response is "This customer has no attached payment source". Using the 1111 test number gave tehsame result so Ihave to associate the card number with the customer before applying the subscription.user1142052

1 Answers

0
votes

The question was why isn't the test card number generating an error response. The answer is in the comments: the customer already had a default card assigned and stripe always uses the default even if a new card is provided in the javascript form. My solution was to update the customer with new card number returned in the token and then apply the subscription:

try {  
    $cu = \Stripe\Customer::retrieve($_POST['customer']);
    $cu->source = $_POST['stripeToken']; // obtained with Stripe.js
    $cu->save();
}

try {
$result = \Stripe\Subscription::create(array(
  "customer" => $_POST['customer'],
  "plan" => $_POST['plan']
));

echo "\nSubscription successful"; }