Response object - Payment with Mollie and Omnipay

末鹿安然 提交于 2020-02-24 04:20:11

问题


I'm trying to create a payment with Omnipay and Mollie in my Laravel project. I'm using the following 2 libraries:

  • https://github.com/barryvdh/laravel-omnipay
  • https://github.com/thephpleague/omnipay-mollie

I'm doing the following in my code:

$gateway = Omnipay\Omnipay::create('Mollie');

$gateway->setApiKey('test_gSDS4xNA96AfNmmdwB3fAA47zS84KN');

$params = [
    'amount' => $ticket_order['order_total'] + $ticket_order['organiser_booking_fee'],
    'description' => 'Bestelling voor klant: ' . $request->get('order_email'),
    'returnUrl' => URL::action('EventCheckoutController@fallback'),
];


$response = $gateway->purchase($params)->send();

if ($response->isSuccessful()) {
    session()->push('ticket_order_' . $event_id . '.transaction_id',
        $response->getTransactionReference());

    return $this->completeOrder($event_id);
}

The payment works. When the payment is done he goes back to the function fallback. But I don't know what to put in this function and how to go back to the line if($response->isSuccesfull()...).

The most important thing I need to do after the payment is :

session()->push('ticket_order_' . $event_id . '.transaction_id',
        $response->getTransactionReference());

return $this->completeOrder($event_id);

Can someone help me figure out how to work with the fallback function and above?


回答1:


A typical setup using Mollie consists of three separate pages:

  • a page to create the payment;
  • a page where Mollie posts the final payment state to in the background; and
  • a page where the consumer returns to after the payment.

The full flow is described in the Mollie docs. Also take a look at the flow diagram at that page.

DISCLAIMER: I've never used Omnipay myself and did not test the following code, but it should at least give you an idea how to set up your project.

Creating the payment:

$gateway = Omnipay\Omnipay::create('Mollie');
$gateway->setApiKey('test_gSDS4xNA96AfNmmdwB3fAA47zS84KN');

$params = [
    'amount' => $ticket_order['order_total'] + $ticket_order['organiser_booking_fee'],
    'description' => 'Bestelling voor klant: ' . $request->get('order_email'),
    'notifyUrl' => '', // URL to the second script
    'returnUrl' => '', // URL to the third script
];

$response = $gateway->purchase($params)->send();

if ($response->isRedirect()) {
    // Store the Mollie transaction ID in your local database
    store_in_database($response->getTransactionReference());
    // Redirect to the Mollie payment screen
    $response->redirect();
} else {
    // Payment failed: display message to the customer
    echo $response->getMessage();
}

Receiving the webhook:

$gateway = Omnipay\Omnipay::create('Mollie');
$gateway->setApiKey('test_gSDS4xNA96AfNmmdwB3fAA47zS84KN');

$params = [
    'transactionReference' => $_POST['id'],
];

$response = $gateway->fetchTransaction($params);

if ($response->isPaid()) {
    // Store in your local database that the transaction was paid successfully
} elseif ($response->isCancelled() || $response->isExpired()) {
    // Store in your local database that the transaction has failed
}

Page where the consumer returns to:

// Check the payment status of your order in your database. If the payment was paid
// successfully, you can display an 'OK' message. If the payment has failed, you
// can show a 'try again' screen.

// Most of the time the webhook will be called before the consumer is returned. For
// some payment methods however the payment state is not known immediately. In
// these cases you can just show a 'payment is pending' screen.


来源:https://stackoverflow.com/questions/43775616/response-object-payment-with-mollie-and-omnipay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!