Stripe 串接
Stripe
stripe為一個金流串接服務提供者,其特點在於串接簡單。
API文件:https://stripe.com/docs/api
1.先到其網站註冊帳號:https://stripe.com
需要具有其許可國家內的銀行帳號與手機。
2.接著就是試著串接API
商店Dashboard:https://dashboard.stripe.com
建立商店後會可以進入一個Dashboard介面,在左下方把Test打開即可進入測試模式。
3.程式範例:
Server.js
const keyPublishable = process.env.PUBLISHABLE_KEY;
const keySecret = process.env.SECRET_KEY;
const app = require("express")();
const stripe = require("stripe")(keySecret);
app.set("view engine", "pug");
app.use(require("body-parser").urlencoded({extended: false}));
app.get("/", (req, res) =>
res.render("index.pug", {keyPublishable}));
app.post("/charge", (req, res) => {
let amount = 500;
stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
})
.then(customer => {
stripe.charges.create({
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
})
console.log(req.body.stripeToken)
console.log(req.body.stripeToken)
}
)
.then(charge => res.render("charge.pug"));
});
app.listen(4567);views/index.pug
views/charge.pug
原理:
要求客戶付款的都在checkout文件:https://stripe.com/docs/checkout
1.引入stripe的官方script後,加入一個元件
此元件會產生官方的付款按鈕
2.之後點擊按鈕後會要求填入信用卡資訊與email

3.送出後會發送Request給stripe
然後stripe成功後回傳request給剛才元件上寫的Endpoint,也就是form上寫的action位置
4.接著我們後端App.js接到後會執行
產生如下請求
5.最後前一個請求返回後會進行確認請求
5.最後前一個請求返回後會進行確認請求
以上的請求詳細內容都可以在左側log tab看到
使用React
可使用此模組:
https://github.com/stripe/react-stripe-js
文件:https://stripe.com/docs/stripe-js/react
測試用信用卡:4242 4242 4242 4242
測試用的key也不同
流程:
1.使用官方的元件輸入信用卡號碼與資訊,然後前端呼叫stripe.createPaymentMethod 傳給後端
2.後端呼叫並產生stripe.paymentIntents 然後將paymentIntent.client_secret 傳給前端
3.前端使用stripe.confirmCardPayment 加上剛才的paymentIntent.client_secret確認交易
4.成功後再發一個request給後端更新使用者購買狀態
Server.js
確認付款
使用stripe.paymentIntents.create 回傳的client secret 並丟給前端,然後前端使用stripe.confirmCardPayment(clientSecret)
來判斷
https://stripe.com/docs/payments/payment-intents/verifying-status
客製化按鈕
https://github.com/stripe/elements-examples
https://stripe.com/docs/stripe-js
可直接加上css
前端完整版範例:
App.js
App.css

訂閱機制
存入卡片資料供後續使用
1.在前端使用createPaymentMethod 並發請求給後端之後在後端使用
createPaymentMethod 並發請求給後端之後在後端使用server.js
web.js
之後客戶成功刷卡後的付款資訊就會存入stripe
2. 讀取先前存入的付款資訊
最後把 paymentIntentSaved 中的 client_secret 給前端然後 confirmPayment 即可
Last updated
Was this helpful?

