4

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

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?

nielsv
  • 6,540
  • 35
  • 111
  • 215
  • Can you update your question to make more clear what flow you are using? From your other questions I understand that this php script is called as an AJAX call. This code creates the payment. You would need another script to call after the payment was completed successfully. – Daan May 04 '17 at 11:17
  • @Daan, I'm not doing it anymore with an AJAX request. It's just a normal POST request now. – nielsv May 04 '17 at 17:32
  • Is the payment done by a redirect to the Mollie gateway? – delatbabel May 05 '17 at 07:10
  • yes, it's done by a redirect. – nielsv May 05 '17 at 07:25

1 Answers1

3

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.
Daan
  • 327
  • 3
  • 10