Reflection to Tealeaf Course3 Week8 (1/2)
#Subscription
This week it’s still about Stripe. Last week, we use Stripe to charge once, and Stripe also offer us to charge monthly. This is called Subscription
.
First following the instruction here to set Plan.
Then write the test for customer charge
1 | describe StripeWrapper, vcr: true do |
StripeWrapper::Charge.customer
is written like this:
1 | module StripeWrapper |
Here is a tip, in Week7, since we merge code into UserSignup
service object, although we change behavior from charge
to customer
, it’s unrelated to UsersControler#create
code.
1 | #some code... |
We only need to modified code in UserSignup.sign_up
.
1 | def sign_up(stripe_token = nil, invitation_token = nil) |
#Stripe Webhooks
We can review Stripe payments on Stripe’s dashboard, but what if we want to track and save these records in our website?
Here we instroduce Webhook
We need Stripe to trigger an event for our server, then we can hold response from Stripe into our database.
- Go
Account setting
->Webhooks
->Add endpoint...
- Input your website url. <- explain it later
- Select test mode, and press Done
- Click
Send test webhook
For developement, we use RequestBin here to inspect HTTP requests.
Create a RequestBin Url for Stripe endpoint, from the link above, and input the url to Step 2. Click Send test webhook
and go back refresh RequestBin page, you will see the response.
Instead of this, we also can review the response from Stripe dashboard
->Events & Webhooks
, select one of record and check the Webhook Details
.
Why we want to check response here? It’s just prepared for test data.
For example, create a RSpec file for succeeded payment
, and convert response to a fake data :event_data
1 | describe "Create payment on successful charge" do |
According to the data, the event type is charge.succeeded
.
In order to receive Stripe Webhook event, we need the gem stripe_event
1 | gem 'stripe_event' |
In config/routes.rb
1 | mount StripeEvent::Engine, at: '/stripe_events' # provide a custom path |
Get mark here for the path /stripe_events
, because it’s the endpoint for setting webhook in Stripe’s account setting. Create a webhook on Stripe, for example, my website is https://tomo-myflix.herokuapp.com
, then endpoint url is https://tomo-myflix.herokuapp.com/stripe_events
Then Stripe_Event help us to deal with stripe events here:
1 | StripeEvent.configure do |events| |
customer_token
is created when creating User
. Add this column in User
to track Stripe record. And code this in service object UserSignup.sign_up
:
1 | #.... |
customer_token
and amount
can get from response event.data.object
.
If your development follow the TDD process, here should code the test first like this
1 | describe "Create payment on successful charge" do |
Notice that we use post '/stripe_events', event_data
to simulate Stripe emit a post to our server. So this is only for test to trigger StripeEvent in initializers/stripe.rb
.
So far, we can create Payment
by Stripe Webhook.
#ngrok for local test
When we run the server on localhost, there is no way to let Stripe directly emit the post to our local machine. And we can use ngrok
to do the response transfer.
Download ngrok and install.
1 | $ngrok 3000 |
Get an URL from ngrok, and set this url to Stripe Webhook endpoint. Run the rails server, and operate app by manual to check if payment record be saved.
Just a memo here, becuase I can’t use ngrok on my Mac(looks like security problem). but it’s ok because I test successful on Heroku.
The most important here is to set the app stirpe events URL in Stripe, for example my end point is:
1 | https://tomo-myflix.herokuapp.com/stripe_events |
#ATDD for payment view
The process for admin views payment
is not very complex, so we can just write the spec outside-in, some also call this ATDD
.
Here is a post talking about the differences between TDD, ATDD, BDD.
1 | feature 'Admin sees payments' do |