I have a checkout process that 1) creates a user account in Stripe and my database, 2) creates a paymentMethod in Stripe and logs the last4 in the database, 3) creates the subscription in Stripe and logs the subscription to database, 4) sends welcome email and creates a job record in the database to build the service for the subscription.
However, during this process, a lot can go wrong. Some of the things I can think of are: 1) credit card number is invalid, card is declined, API rate limiting, database server goes down/overloaded, etc...
I'm using a try/catch in PHP. However, how can I avoid re-running parts of the try if the user simply needs to fix some input like using a different card number because the other one declined?
At this point the customer record is already created, so I don't need to re-run those parts.
try {
- 1
- Create Stripe Payment Method
// this will throw any credit card validation errors such as invalid dates, csc, etc...
- 2
- Create Stripe Customer
- Attach payment method to Customer
- Create record in database for customers table
- 3
- Create Stripe subscription
- Create record in database for subscriptions table
- 4
- Create record in job table to send out welcome email and build service for customer
- display thank you page
} catch
- Stripe CreditCard validation
- Stripe API Rate Limit
- Stripe API network error
- other Stripe error handling
- PDO Exceptions
}
Should I create a session variable for each section and use an if block of code to prevent re-running it?
For example if section 2 completes, do something like $_SESSION['customer'] = 'done';
and then do:
if ($_SESSION['customer'] == 'done') {
// don't run
} else {
// run code
$_SESSION['customer'] = 'done';
}