Laravel Omnipay - 需要transactionReference参数

时间:2017-05-24 06:08:36

标签: php laravel payment-gateway omnipay mollie

我正在使用名为Attendize的开源票证系统。

他们已经整合了付款服务提供商Stripe。现在,我正在努力与付款提供商Mollie合作。

问题是我一直在绊倒这个错误:

enter image description here

我的代码如下所示:

$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(),
    ]);
}

当我调试我的代码时,他会进入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,
        ]);
    }

}

问题(错误)是$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
)

更新2:

我在退回功能中添加了$gateway->setApiKey($apiKey);。但问题是我的回答成功。所以他没有进入$response->isSuccessful()。当我在检查它是否成功之前转储我的$response变量时,它会显示:https://pastebin.com/NKCsxJ7B

你可以看到这样的错误:

[error] => Array ( [type] => request [message] => The payment id is invalid )

Mollie的付款看起来像这样:

enter image description here

更新3:

在我的return函数中,我尝试检查响应对象的状态,如下所示:$response->status()。这给了我以下错误:

  

调用未定义的方法Omnipay \ Mollie \ Message \ CompletePurchaseResponse :: status()

然后我尝试$response->getStatus(),但这没有给我任何回报。

2 个答案:

答案 0 :(得分:4)

@Daan在他的评论中说的是正确的,您从登录页面收到错误,而不是创建交易的页面。

在该目标网页上,您将进行如下调用:

$omnipay->completePurchase($data);

在该@data数组中,您需要添加'transactionReference'字段,该字段应该是您POST网址收到的http://myurl.dev/e/1/checkout/success?is_payment_successful=‌​1个参数之一。

可能有用的调试辅助工具是打开该URL的代码或记录整个$_POST数组,您可以使用它来检查需要从该数组中提取的参数。它在网关之间有所不同。

答案 1 :(得分:2)

这可能与此故障单有关:https://github.com/thephpleague/omnipay-eway/issues/13

要解决此问题,我建议您使用

检查状态代码
if ($request->status() == 201) {
    //successful created
}

我的理论是它正在检查200

该功能在此处定义:

https://github.com/thephpleague/omnipay-mollie/blob/master/src/Message/AbstractResponse.php

  public function isSuccessful()
    {
        return !$this->isRedirect() && !isset($this->data['error']);
    }

它可能会因为您期望重定向而失败!

201因为我的邮差测试 enter image description here