How to process Rapid Eway API Response

Eway provides a Rapid API response "Transparent Redirect" to complete payment on their site. This article is based on how the response from eway can be processed in order to update the transaction on our site. From the eway docs -

  • Transparent Redirect - Using this method, the customer enters their payment information into a form on the merchant's website. This is then submitted directly and securely to eWAY, protecting the merchant's systems from handling any card data. Transparent Redirect also supports 3D Secure, PayPal, MasterPass, Visa Checkout and AMEX Express Checkout.

So a common workflow that needs to be followed on your site while making an Eway payment is -

  • Create a Pending payment on your site.
  • Navigate user to Eway to enter their payment details and complete the process.
  • It redirects back to your site where you need to handle the Eway response and complete the payment created in Step1.

During the last step, eway provides an AccessCode in the URL while sending you back to your site. Use this accessCode to return the status of the payment on eway and set it to complete if it is a success. If a failure is returned, update the payment details to failed on your site too.

Check the returned response using AccessCode.

Below code gives an example of how api response can be processed using the "AccessCode" parameter returned from eway.

$eway_api_key =  // eWAY Api Key
$eway_api_password =  // eWAY Api Password
$eway_end_point = (0 or 1);
$eway_client = \Eway\Rapid::createClient($eway_api_key, $eway_api_password, $eway_end_point);

//Get response using the access_code present in the URL.
$access_code = $_GET['AccessCode'];

$failed_payment = FALSE;
$eway_response = $eway_client->queryTransaction($access_code);
if (!empty($eway_response->Transactions[0])) {
  $eway_response = $eway_response->Transactions[0];

  //Check for any errors in the response.
  $response_errors = $eway_response->getErrors();
  if (count($response_errors)) {
    //There is an error, update the payment to failed.
    $transactionErrors = array();
    foreach ($response_errors as $error) {
      $errorMessage = \Eway\Rapid::getMessage($error);
      $transactionErrors[] = $errorMessage;
    }
    $failed_payment = TRUE;
  }
  else {
    $eway_response = $eway_response->Transactions[0];
    //Check transaction status of the response returned from eway.
    if (!$eway_response->TransactionStatus) {
      $failed_payment = TRUE;
    }
    
    if ($failed_payment) {
      //Update the status of the payment to failed and return.
      return;
    }
    else {
      //If we reach here, it means the payment was successful on eway, hence we can safely set it to completed on our site too.
      //Complete the payment and return.
      return; 
    }
  }
}