在這裏只說一下stripe的使用,其他visa,PayPal都大同小異。
支付文檔這裏有:https://stripe.com/docs/payments
直接貼一下對接代碼,自己琢磨一下
<script src="https://checkout.stripe.com/checkout.js"></script>
<b class='stripepay' pay="stripepay">Credit Card Pay</b>
// 在線支付
var url = "/order/stripePay";
var handler = StripeCheckout.configure({
key: js_stripe_key,//參數規定的祕鑰key。需自己申請
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
email: email,
allowRememberMe: false,
token: function (token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
data.token_id = token.id; //stripe規定參數
data.stripe_currency = js_stripe_currency; //幣種RMB
data.stripe_amount = totalmoney; //價格
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: "json",
beforeSend: function (XMLHttpRequest) {
$(".mask_layer").show()
}, success: function (data) {
if (data.code == '200') {
alert('pay success');
} else {
alert('pay fail');
}
}, complete: function (XMLHttpRequest, textStatus) {
$(".mask_layer").hide()
}, error: function (XMLHttpRequest, textStatus) {
}
});
}
});
// Open Checkout with further options:
handler.open({
name: '',
description: '',
currency: js_stripe_currency, //幣種
amount: totalmoney //金額,需要*100的,
});
// Close Checkout on page navigation:
window.addEventListener('popstate', function () {
handler.close();
});
最後是php的服務端代碼,一般寫處理邏輯
// 此處爲在線支付
try {
Stripe::setApiKey(env('STRIPE_KEY'));
$charge = Charge::create([
'amount' => $request->stripe_amount,
'currency' => $request->stripe_currency,
'source' => $request->token_id,
]);
if (json_decode($charge, true)['status'] != 'succeeded') {
return response()->json(['code' => 403, 'message' => 'Order Failed']);
} else {
DB::update('update order_list set pay_type = ? where id = ?',[1,$result]);
}
} catch (Exception $exception) {
return response()->json(['code' => 404, 'message' => 'Order Failed']);
}
brother~~~~~