Reflection to Tealeaf Course3 Week8 (2/2)
#Handle Payments Failures
Here we review what Jason Fried said in How Do You Process Credit Card:
The one thing we’re often surprised by is how many accounts have charge issues so it’s important to really think about the error handling and customer experience issues related to declined cards.
So there are thousands of reasons a credit card may become declined after user register our website. It’s important to handle these payment failures.
Stripe also provide the test situation for us.
Card Number:
4000000000000341
Description:Attaching this card to a Customer object will succeed, but attempts to charge the customer will fail.
This special card number will create a customer but charge will fail.
We may produce failed webhook event by Stripe Dashboard.
Open Payments
on dashboard and click Create Payment
, input the above invalid Card Number info and click Done. Then we can find a failure charge in Events & Webhooks
.
As the previous post, we can get failed data as :event_data
for test:
1 | describe 'customer on failed charge' do |
Notice that :event_data
type is charge.failed
.
Then we add event charge.failed
in strip.rb
1 | StripeEvent.configure do |events| |
We simplpy deactivate user here, usually, it should send a warning mail to the user.
Here is the request test:
1 | describe 'customer on failed charge' do |
We need to add column active
for User
to store the status.
1 | add_column :users, :active, :boolean, default: true |
Make the active
column default is true, otherwise Rails will set false
for default value.
#A Short Conclusion for Stripe
We cover Stripe for last three weeks.
In week 6, we introduce Stripe for charge. How to build a custom form to charge. And test by using
vcr
for replay response.In week 7, we refactor code into
UserSignup
, as anService Object
of OOP concept. And we need to use javascript driverwebkit
andselenium
in RSpec because of Stripe api.In week 8, we use
Subscription
to charge monthly instead of charge once. And we handle events byWebhook
, to deal withcharge.succeeded
andcharge.failed
events.