laravel - Omnipay 从条纹中获取交易费用
问题描述
我一直在尝试使用thephpleague/omnipay-stripe
.
我不希望退回或设置申请费,而是 Stripe 为每笔交易收取的实际费用。
到目前为止,这是我的代码:
if ($request->input('stripeToken')) {
$gateway = Omnipay::create('Stripe\PaymentIntents');
$gateway->initialize([
'apiKey' => env('STRIPE_SECRET'),
]);
$token = $request->input('stripeToken');
$paymentMethodId = $request->get('paymentMethodId');
$response = $gateway->purchase([
'amount' => session('cost'),
'currency' => env('STRIPE_CURRENCY'),
'description' => session('payment_title'),
'paymentMethod' => $paymentMethodId,
'token' => $token,
'name' => \Auth::user()->name,
'returnUrl' => route('customer.charge.stripe.return_url'),
'confirm' => true,
])->send();
if ($response->isSuccessful()) {
$arr_payment_data = $response->getData();
$data = [
'type' => session('payment_type'),
'cost' => session('cost'),
'duration' => session('duration'),
'description' => session('payment_title'),
'transaction_id' => $arr_payment_data['id'],
'status' => $arr_payment_data['status'],
'fee' => // Whatever I need to call to get fee,
'payment_details' => $arr_payment_data
];
Payments::add_payment_to_db($data);
$request->session()->forget(['cost', 'payment_title', 'duration']);
return redirect()->route('customer.dashboard')->with([
'status' => __('customer.payments.success'),
'alert' => 'success',
]);
} elseif($response->isRedirect()) {
$response->redirect();
} else {
// payment failed: display message to customer
return redirect()->back()->with([
'status' => $response->getMessage(),
'alert' => 'danger',
]);
}
我已经尝试了一些事情,但我不确定如何正确收取费用。
任何有关如何获得费用的帮助或想法将不胜感激。
非常感谢
解决方案
购买成功后,您需要检索付款意图才能获得费用。我不使用 Omnipay,但也许这种方法可以帮助您朝正确的方向射击。这是我在使用 Stripe API 成功收费后收取费用的方式。
protected function getFeeDetails($payment_intent_id, $user)
{
Stripe::setApiKey(env('STRIPE_SECRET'));
$payment_intent = PaymentIntent::retrieve([
'id' => $payment_intent_id,
'expand' => ['charges.data.balance_transaction'],
],
[
'stripe_account' => $user->stripe_user_id
]);
return $payment_intent->charges->data[0]->balance_transaction->fee;
}
推荐阅读
- php - 用户提交表单选择时的PHP查询
- c# - 显式实例化使用 DI 的 C# 类 - 为什么它不起作用?
- c++ - C++:通过引用 unique_ptr 对象使 unique_ptr 为空
- informatica - 过去 30 天的 Informatica 作业信息
- java - Flutter:构建失败可能是由于插件中的 AndroidX 不兼容。该工具即将尝试使用Jetfier解决不兼容问题
- outlook - 在不同的行中键入时 Outlook 文本消失
- python - 我无法在 rasa 中添加音频剪辑作为聊天机器人的输出
- c++ - 警告 C5240:“nodiscard”:在此句法位置忽略属性
- awk - 关于 sed awk grep 的使用
- ssis - SSIS复制文件任务不复制自定义日期列