Become an eWAY merchant today.
With 24/7 support, over 250 integrations and 20+ years experience – the team at eWAY are here to provide you with the leading all-in-one payments solution.
Accept payments quickly and easily with eWAY using the eWAY Rapid PHP SDK. Using the PHP SDK provides convenient access to all the features of eWAY’s Rapid API, so you can create transactions, process refunds, query transactions, create token customers, and more!
The eWAY PHP SDK requires PHP version 5.4.0 or greater with the curl, json and openssl extensions. We recommend installing using the Composer Package Manager.
To make development and testing with eWAY easy, get a free partner account which provides access to the eWAY Sandbox. To find out more about activating Sandbox, check out the Success Community article here.
If you have Composer, adding the eWAY PHP SDK is as simple as running the following on the command line:
1 | $ composer require eway/eway-rapid-php |
Once installed, the eWAY PHP SDK can be loaded in your project using Composer’s autoloader:
1 2 3 | <?php require('vendor/autoload.php'); |
If you don’t have access to Composer, you can add the eWAY PHP SDK to your project by:
1 2 3 | <?php require_once 'lib/eway-rapid-php-master/include_eway.php'; |
Now you are all set to build your eWAY integration! You can find code samples for integrating various eWAY functions as part of the eWAY Rapid API reference.
To demonstrate using the eWAY PHP SDK, this example will show how to accept a payment with the Responsive Shared Page.
This example assumes that Composer is already installed and a new project is being started.
1 | $ composer require eway/eway-rapid-php |
Once completed, the directory should now have a “vendor” folder, along with a couple of composer files.
1 2 3 | <?php require('vendor/autoload.php'); |
Now the eWAY client can be created with your Rapid API key & password. These can be found in Sandbox or Live MYeWAY – remember that different credentials are needed for Sandbox Live.
1 2 3 4 5 6 7 | // eWAY Credentials $apiKey = '60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR'; $apiPassword = 'API-P4ss'; $apiEndpoint = 'Sandbox'; // Create the eWAY Client $client = \Eway\Rapid::createClient($apiKey, $apiPassword, $apiEndpoint); |
In order to send a customer to the Responsive Shared Page, a URL must be generated using thecreateTransaction function. This accepts a PaymentMethod and an array of transaction data.
The PaymentMethod determines how the card data will be accepted (in this case ResponsiveShared) while the transaction array contains customer and invoice details – more details of the variables that can be passed can be found in the Rapid API reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Transaction details - these would usually come from the application $transaction = [ 'Customer' => [ 'FirstName' => 'John', 'LastName' => 'Smith', 'Street1' => 'Level 5', 'Street2' => '369 Queen Street', 'City' => 'Sydney', 'State' => 'NSW', 'PostalCode' => '2000', 'Country' => 'au', 'Email' => 'demo@example.org', ], // These should be set to your actual website (on HTTPS of course) 'RedirectUrl' => "http://$_SERVER[HTTP_HOST]" . dirname($_SERVER['REQUEST_URI']) . '/response.php', 'CancelUrl' => "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", 'TransactionType' => \Eway\Rapid\Enum\TransactionType::PURCHASE, 'Payment' => [ 'TotalAmount' => 1000, ] ]; // Submit data to eWAY to get a Shared Page URL $response = $client->createTransaction(\Eway\Rapid\Enum\ApiMethod::RESPONSIVE_SHARED, $transaction) |
To make sure everything went well, check that the Errors list is empty. If it isn’t, UserDisplayMessagecan be used to convert any error codes to human readable messages:
1 2 3 4 5 6 7 8 9 | // Check for any errors if (!$response->getErrors()) { $sharedURL = $response->SharedPaymentUrl; } else { foreach ($response->getErrors() as $error) { echo "Error: ".\Eway\Rapid::getMessage($error)."<br>"; } die(); } |
Usually once the shared page URL has been created, the customer can be immediately redirected. For this example we’ll just display a link:
1 | echo '<a href="'.$sharedURL.'">Pay with our secure payment page</a>'; |
You should now be able to visit the index.php file in your web browser – it will display a link which goes to the Responsive Shared Page created with the details submitted in the previous step.
Once the customer has gone to the Responsive Shared Page and submitted their payment information, they will be redirected back to the RedirectUrl specified in the request. This was set to response.php – so now create that file and insert the following to fetch the transaction result:
1 2 3 4 5 6 7 8 9 10 11 12 | require('vendor/autoload.php'); // eWAY Credentials $apiKey = '60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR'; $apiPassword = 'API-P4ss'; $apiEndpoint = \Eway\Rapid\Client::MODE_SANDBOX; // Create the eWAY Client $client = \Eway\Rapid::createClient($apiKey, $apiPassword, $apiEndpoint); // Query the transaction result. $response = $client->queryTransaction($_GET['AccessCode']); |
Key information such as the success of the transaction and the transaction ID can be found in the TransactionStatus of the response. If the transaction wasn’t successful, the reason can be found in the ResponseMessage. An example of handling this information would be
1 2 3 4 5 6 7 8 9 10 11 12 13 | $transactionResponse = $response->Transactions[0]; // Display the transaction result if ($transactionResponse->TransactionStatus) { echo 'Payment successful! ID: ' . $transactionResponse->TransactionID; } else { $errors = split(', ', $transactionResponse->ResponseMessage); foreach ($errors as $error) { echo "Payment failed: " . \Eway\Rapid::getMessage($error)."<br>"; } } |
With 24/7 support, over 250 integrations and 20+ years experience – the team at eWAY are here to provide you with the leading all-in-one payments solution.