我終于設法通過 Payment Intents API 在 Laravel 中實作了新的 Stripe 支付元素。但是,我現在需要捕獲有關付款的資訊并將它們存盤在我的資料庫中 - 具體來說,我需要以下資料:
- 交易編號
- 支付狀態(失敗/待處理/成功等)
- 付款方式型別(卡/Google Pay/Apple Pay/等)
- 實際向客戶收取的金額
- 客戶實際支付的貨幣
- 用戶在付款表單中輸入的郵政編碼
所有這些資訊似乎都在Payment Intent 物件中可用,但沒有幾個 Stripe 指南指定如何在服務器上捕獲它們。我想避免使用 webhook,因為它們對于抓取和持久化我已經檢索到的資料似乎有點過分了。
這也無濟于事,這要歸功于 Stripe 檔案的 AJAX/PHP 解決方案的設定方式,試圖在服務器端轉儲和洗掉任何變數會導致整個客戶端流程中斷,從而阻止支付表單呈現并阻止任何除錯資訊。從本質上講,這使得 Payment Intents API 的整個實作無法在服務器上進行除錯。
以前來過這里的人知道我將如何獲取這些資訊嗎?
JavaScript/AJAX 的相關部分:
const stripe = Stripe(<TEST_PUBLISHABLE_KEY>);
const fonts = [
{
cssSrc:
"https://fonts.googleapis.com/css2?family=Open Sans:wght@300;400;500;600;700&display=swap",
},
];
const appearance = {
theme: "stripe",
labels: "floating",
variables: {
colorText: "#2c2c2c",
fontFamily: "Open Sans, Segoe UI, sans-serif",
borderRadius: "4px",
},
};
let elements;
initialize();
checkStatus();
document
.querySelector("#payment-form")
.addEventListener("submit", handleSubmit);
// Fetches a payment intent and captures the client secret
async function initialize() {
const { clientSecret } = await fetch("/payment/stripe", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-TOKEN": document.querySelector('input[name="_token"]').value,
},
}).then((r) => r.json());
elements = stripe.elements({ fonts, appearance, clientSecret });
const paymentElement = elements.create("payment");
paymentElement.mount("#payment-element");
}
async function handleSubmit(e) {
e.preventDefault();
setLoading(true);
const { error } = await stripe.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: "http://localhost.rc/success"
},
});
if (error.type === "card_error" || error.type === "validation_error") {
showMessage(error.message);
} else {
showMessage("An unexpected error occured.");
}
setLoading(false);
}
// Fetches the payment intent status after payment submission
async function checkStatus() {
const clientSecret = new URLSearchParams(window.location.search).get(
"payment_intent_client_secret"
);
if (!clientSecret) {
return;
}
const { paymentIntent } = await stripe.retrievePaymentIntent(clientSecret);
switch (paymentIntent.status) {
case "succeeded":
showMessage("Payment succeeded!");
break;
case "processing":
showMessage("Your payment is processing.");
break;
case "requires_payment_method":
showMessage("Your payment was not successful, please try again.");
break;
default:
showMessage("Something went wrong.");
break;
}
}
路由檔案:
Route::post('/payment/stripe', [TransactionController::class, "stripe"]);
事務控制器:
public function stripe(Request $request) {
Stripe\Stripe::setApiKey(env(<TEST_SECRET_KEY>));
header('Content-Type: application/json');
try {
$paymentIntent = Stripe\PaymentIntent::create([
'amount' => 2.99,
'currency' => 'gbp',
'automatic_payment_methods' => [
'enabled' => true,
],
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
$this->storeStripe($paymentIntent, $output);
echo json_encode($output);
} catch (Stripe\Exception\CardException $e) {
echo 'Error code is:' . $e->getError()->code;
$paymentIntentId = $e->getError()->payment_intent->id;
$paymentIntent = Stripe\PaymentIntent::retrieve($paymentIntentId);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
}
如何從支付意圖中捕獲上述資訊以存盤在我的資料庫中?
uj5u.com熱心網友回復:
我知道你不會喜歡這個,但我還是會說。老實說,我認為實作webhook 端點、偵聽器和接收器功能是您最好的選擇,原因如下:
Stripe Payment Intent 在支付通過多個狀態時捕獲其生命周期。由于 Stripe 之外的各種支付網路不保證特定的回應時間,這些轉換可以是異步的。
因此,除非您正在偵聽該事件,否則您無法確定何時是查詢 API 以獲取已完成的付款意圖的適當時間。payment_intent.succeeded此外,在某些情況下,付款方式可能會在初始處理后被拒絕(例如,可疑的欺詐卡等)。使用 webhook 方法可以讓您了解這些變化。
最后,雖然您現在可能只關心將這些資料存盤在您的資料庫中,但范圍確實會增加,并且盡早實施 webhook 偵聽器意味著如果您需要采取其他操作,例如,您將準備好解決方案
- 向您的客戶發送電子郵件通知
- 調整收入對賬
- 處理履行行動
- 其他的東西.....
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/432532.html
上一篇:無法在資料庫中存盤加密的整數
