Omnipay Eway Tutorial Example

By Ralph Vugts on March 11, 2014

Tagged in:

EWay LogoSetting up payment gateways for your project is always good times! (Cough, cough, choke). Not only is it time consuming, it can also be extremely frustrating when the documentation just plain sucks! Although the people who write these docs might be awesome coders, being able to understand what the heck they’re taking about another story. Quite often they will skip over vital steps and assume you know bits in the process and leave you scratching you head as to why it’s not working.

This happened to me recently while trying to implement Omnipay (a framework agnostic, multi-gateway payment processing library for PHP 5.3) with Eway. When I first came across this and noticed that it supported Eway I thought ‘praise the lord’ this is going to make my life easier! Far from it however, as there was only basic documentation and none on the Eway implementation.

Anyway, thought I’d share how I finally got it to work for me with Eway in the hopes it will save you a bit of time 🙂

Step 1: Install Omnipay using composer. This is pretty straight forward it downloads all the files you need into a folder called Vendor (see instructions here)

Step 2: Once installed you will notice a /Vendor folder and been installed with all the goodies you need. You now need to pull this into your app. If your using Codeignter use this line a the top of your index.php file:

require __DIR__.'/../Vendor/autoload.php';

Otherwise include it as you usually would using php.

Step 3: Now you have access to all of Omnipays goodness, now we can start using it. At the start of your PHP script add these lines:

use OmnipayCommonCreditCard;  use OmnipayOmnipay;

Step 4: We now need to get a EWAY_ACCESSCODE from the eway gateway to add to the credit card details before we can push the card details and complete the transaction. Note the “returnUrl” once the transaction has been processed Eway will redirect to this page on your site so you can display success or fail messages.

$gateway = Omnipay::create('Eway_Rapid');    //$settings = $gateway->getDefaultParameters();       $gateway->setApiKey('C3AB9FDSF7RbICiZ25S6PC7gHOYn2v+Vo1d23cuDzroX6ms4kfiCWrMqEH7MN9c2UeD0g');  $gateway->setPassword('yourpass');  $gateway->setTestMode(true);  $formData = array(  'firstName'     =>  'Bob',  'lastName'      =>  'Smith'      );     //Payment            $card = new CreditCard($formData);    $PayArray = array(            'amount' => '10.00',      'transactionId' => '999',      'description' => 'new car',      'currency' => 'AUD',      'clientIp' => '127.0.0.1',      'returnUrl' => 'https://www.YOURDOMAIN.com/shop/pay/1234',  'card' =>     $card      );    $response = $gateway->purchase($PayArray)->send();    if ($response->isRedirect()) {      // redirect to offsite payment gateway    //echo print_r($response->getRedirectData());    $Ewayaccesscode = $response->getRedirectData();    echo $Ewayaccesscode['EWAY_ACCESSCODE'];      //Display form here to submit credit card info with Access Code   //Display form here to submit credit card info with Access Code   //Display form here to submit credit card info with Access Code   //Display form here to submit credit card info with Access Code Download my full example file to see working example.    ";          }    //var_dump ($array);  } else {      // payment failed: display message to customer      echo $response->getMessage();  }

Step 5: The code below needs to be on your returnUrl page/controller so you can process the response from Eway and mark the order off as paid and do what ever else you require.

$request->AccessCode = $_GET['AccessCode'];  //$AccessCode    $gateway = Omnipay::create('Eway_Rapid');  $gateway->setApiKey('C3AB9CdyJKlkICiZ25S6PC7gHOYn2v+Vo1d23cuDzroX6ms4kfiCWrMqEH7MN9c2UeD0g');  $gateway->setPassword('Your_Password');  $gateway->setTestMode(true);      //$settings = $gateway->getDefaultParameters();  //print_r($settings);  $formData = array('AccessCode' => $_GET['AccessCode']);    $response = $gateway->completePurchase($formData)->send();    $data = $response->getData(); // this is the raw response object    print_r($data);

Don’t forget to swap out your sandbox details for your own. Once your ready to go live change test mode to false and replace sandbox creds with the live ones.

Hopefully I have not gone full nerd and made a mess of explaining the above… But I think it’s a pretty easy to follow. Leave a comment below if you’re sturggling with any parts of it.

Ralph has been developing websites and systems for nearly 20 years. Passionate and curious, he’s an a-typical developer who understands how users interact with the systems he builds. He understands that systems need to be human-friendly. Ralph loves working in an industry that is constantly changing and disrupting itself.

Comments (2)

Add a comment
  • sbkrish   1 Jun 2019
    Thank you so much. Finally I got the result after 8 days of struggle. Thanks a lot. Keep up the good work.
    • Ralph Vugts   28 Jun 2019
      Nice thanks!

Add a comment

Your email address will not be published. Required fields are marked *

*