phplaravelpaymentomnipaymollie

Response object - Payment with Mollie and Omnipay


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?


Solution

  • A typical setup using Mollie consists of three separate pages:

    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.