Reflection to Tealeaf Course3 Week6 (2/2)
These week we have two main part: CarrierWave for uploading images as an admin, Stripe for payments
The first part about CarrierWave for uploading images as an Admin is here.
#Stripe for Payments
Two articles about payments:
- How Do You Process Credit Card by Jason Fried
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.
###Stripe & Figaro
1 | gem 'stripe' |
$ figaro install
Use Figaro to set Environment variables, Do Not hard code it!!
1 | #config/application.yaml |
If Stripe account doesn’t activate, we only can use test key
sk_test_xxxxxxx
even ENV = production
I also add this file config/initailizers/figaro.rb
1 | Figaro.require_keys("STRIPE_SECRET_KEY", "STRIPE_PUBLISHABLE_KEY") |
It’s convenient to check if you forget to set Environment variables on deploy machine(ex. Circle CI or Heroku).
Figaro also provide a way to set Heroku ENV.
$ figaro heroku:set -e production
Stripe Custom Form
- Setting form view
1 | <script type="text/javascript" src="https://js.stripe.com/v2/"></script> |
When deploy to Heroku, I got this error
Failed to load resource: the server responded with a status of 404 (Not Found)
This is becuase assets pipeline, add this to the file
1 | Rails.application.config.assets.precompile += ['payment.js'] |
Key Points:
- use
id: 'payment-form'
in form_form, this is corespond toassets/javascripts/payment.js
content. - use
for
andid
to connect label and input data-stripe = "number"
anddata-stripe="cvc"
data: { stripe: "exp-yaer"}
anddata: { stripe: "exp-month"}
Attributes of data-stripe
is for Stripe to know credit infomation.
Then in our custom form:
1 | %section.register.container |
###Setting in Controller Action
1 | # Set your secret key: remember to change this to your live secret key in production |
Now we click submit button, charge will be deliever to Stripe.
Notice again, don’t foget to set ENV on Circle CI or Heroku if you deploy code to there.