nie*_*lsv 11 php payment-gateway laravel omnipay mollie
我正在使用名为Attendize的开源票证系统.
他们已经将支付提供商Stripe集成在一起.现在我正在尝试与支付提供商Mollie合作.
问题是我一直在绊倒这个错误:
我的代码看起来像这样:
$transaction_data += [
'transactionId' => $event_id . date('YmdHis'),
'returnUrl' => route('showEventCheckoutPaymentReturn', [
'event_id' => $event_id,
'is_payment_successful' => 1
]),
];
$apiKey = "test_gSDS4xNA96AfNmmdwB3fAA47******";
$gateway->setApiKey($apiKey);
$transaction = $gateway->purchase($transaction_data);
$response = $transaction->send();
if ($response->isSuccessful()) {
session()->push('ticket_order_' . $event_id . '.transaction_id',
$response->getTransactionReference());
return $this->completeOrder($event_id);
} elseif ($response->isRedirect()) {
/*
* As we're going off-site for payment we need to store some data in a session so it's available
* when we return
*/
session()->push('ticket_order_' . $event_id . '.transaction_data', $transaction_data);
Log::info("Redirect url: " . $response->getRedirectUrl());
$return = [
'status' => 'success',
'redirectUrl' => $response->getRedirectUrl(),
'message' => 'Redirecting to ' . $ticket_order['payment_gateway']->provider_name
];
// GET method requests should not have redirectData on the JSON return string
if($response->getRedirectMethod() == 'POST') {
$return['redirectData'] = $response->getRedirectData();
}
return response()->json($return);
} else {
// display error to customer
return response()->json([
'status' => 'error',
'message' => $response->getMessage(),
]);
}
Run Code Online (Sandbox Code Playgroud)
当我调试我的代码时,他会进入elseif ($response->isRedirect()) {.我被重定向到Mollie并且可以成功付款.但当我重定向回到http://myurl.dev/e/1/checkout/success?is_payment_successful=1我收到错误.
更新:
在我的返回函数中,我有以下代码:
public function showEventCheckoutPaymentReturn(Request $request, $event_id)
{
if ($request->get('is_payment_cancelled') == '1') {
session()->flash('message', 'You cancelled your payment. You may try again.');
return response()->redirectToRoute('showEventCheckout', [
'event_id' => $event_id,
'is_payment_cancelled' => 1,
]);
}
$ticket_order = session()->get('ticket_order_' . $event_id);
$gateway = Omnipay::create($ticket_order['payment_gateway']->name);
$gateway->initialize($ticket_order['account_payment_gateway']->config + [
'testMode' => config('attendize.enable_test_payments'),
]);
$transaction = $gateway->completePurchase($ticket_order['transaction_data'][0]);
$response = $transaction->send();
if ($response->isSuccessful()) {
session()->push('ticket_order_' . $event_id . '.transaction_id', $response->getTransactionReference());
return $this->completeOrder($event_id, false);
} else {
session()->flash('message', $response->getMessage());
return response()->redirectToRoute('showEventCheckout', [
'event_id' => $event_id,
'is_payment_failed' => 1,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
问题(错误)是$response = $transaction->send();.
该数组$ticket_order['transaction_data'][0]包含:
Array
(
[amount] => 80
[currency] => EUR
[description] => Order for customer: niels@email.be
[transactionId] => 120170529082422
[returnUrl] => http://eventy.dev/e/1/checkout/success?is_payment_successful=1
)
Run Code Online (Sandbox Code Playgroud)
更新2:
我已经添加$gateway->setApiKey($apiKey);了我的返回功能.但问题是我的回答并不成功.所以他没有进入$response->isSuccessful().当我$response在检查它是否成功之前转储我的变量时,它会显示:https://pastebin.com/NKCsxJ7B.
你可以看到这样的错误:
[error] => Array
(
[type] => request
[message] => The payment id is invalid
)
Mollie的付款看起来像这样:
更新3:
在我的返回函数中,我试图像这样检查响应对象的状态:$response->status().这给了我以下错误:
调用未定义的方法Omnipay\Mollie\Message\CompletePurchaseResponse :: status()
然后我尝试了,$response->getStatus()但这没有给我任何回报.
这可能与这张票有关:https ://github.com/thephpleague/omnipay-eway/issues/13
为了解决这个检查,我建议检查状态代码
if ($request->status() == 201) {
//successful created
}
Run Code Online (Sandbox Code Playgroud)
我的理论是它正在检查 200
该函数定义如下:
https://github.com/thephpleague/omnipay-mollie/blob/master/src/Message/AbstractResponse.php
public function isSuccessful()
{
return !$this->isRedirect() && !isset($this->data['error']);
}
Run Code Online (Sandbox Code Playgroud)
它可能会失败,因为您期望重定向!