Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 65 additions & 31 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,37 @@ public function getData()
$data['type'] = $this->getType();
$data['amount'] = $this->getAmount();
$data['currency'] = $this->getCurrency();
$data['callbackUrls'] = [];

$data = array_merge($data, $this->getStoredCardData());
$data = array_merge($data, $this->getRecurringOptionsData());
$data = array_merge($data, $this->getMiscOptionsData());
$data = array_merge($data, $this->getCallbackUrlsData());

return $data;
}

public function sendData($data)
{
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . $this->getAuthorization(),
];

$httpResponse = $this->httpClient->request('POST', $this->getEndpoint('sessions'), $headers, json_encode($data));

try {
$responseData = json_decode($httpResponse->getBody()->getContents());
} catch (\Exception $exception) {
$responseData = [];
}

return $this->response = new PurchaseResponse($this, $responseData ?? []);
}

protected function getStoredCardData()
{
$data = [];

if ( (bool) $this->getCreateToken() ) {
$data['storeCard'] = true;
Expand All @@ -33,36 +63,38 @@ public function getData()
$data['storedCardIndicator'] = $this->getStoredCardIndicator();
}

if ( $this->getRecurringExpiry() ) {
$data['recurringExpiry'] = $this->getRecurringExpiry();
}

if ( $this->getRecurringFrequency() ) {
$data['recurringFrequency'] = $this->getRecurringFrequency();
}

if ( $this->getToken() || $this->getCardReference() ) {
$data['cardId'] = $this->getToken() ?? $this->getCardReference();
}

if ( is_array($this->getPaymentMethods()) ) {
$data['methods'] = $this->getPaymentMethods();
}

if ( is_array($this->getCardTypes()) ) {
$data['cardTypes'] = $this->getCardTypes();
}

if ( is_array($this->getMetadata()) ) {
$data['metaData'] = $this->getMetadata();
}
return $data;
}

$merchantReference = $this->getMerchantReference() ?? $this->getDescription();
protected function getRecurringOptionsData()
{
$data = [];

if ( $merchantReference ) {
$data['merchantReference'] = $merchantReference;
if ( $this->getRecurringExpiry() ) {
$data['recurringExpiry'] = $this->getRecurringExpiry();
}

if ( $this->getRecurringFrequency() ) {
$data['recurringFrequency'] = $this->getRecurringFrequency();
}

return $data;
}

protected function getCallbackUrlsData()
{
$data = [
'callbackUrls' => []
];

if ( $this->getReturnUrl() ) {
$data['callbackUrls']['approved'] = $this->getReturnUrl();
}
Expand All @@ -82,22 +114,24 @@ public function getData()
return $data;
}

public function sendData($data)
protected function getMiscOptionsData()
{
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . $this->getAuthorization(),
];
$data = [];

$httpResponse = $this->httpClient->request('POST', $this->getEndpoint('sessions'), $headers, json_encode($data));
if ( is_array($this->getPaymentMethods()) ) {
$data['methods'] = $this->getPaymentMethods();
}

try {
$responseData = json_decode($httpResponse->getBody()->getContents());
} catch (\Exception $exception) {
$responseData = [];
if ( is_array($this->getMetadata()) ) {
$data['metaData'] = $this->getMetadata();
}

return $this->response = new PurchaseResponse($this, $responseData ?? []);
$merchantReference = $this->getMerchantReference() ?? $this->getDescription();

if ( $merchantReference ) {
$data['merchantReference'] = $merchantReference;
}

return $data;
}
}
120 changes: 117 additions & 3 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Omnipay\WindcaveHpp;

use Omnipay\Tests\GatewayTestCase;
use Omnipay\Common\Message\NotificationInterface;

class GatewayTest extends GatewayTestCase
{
Expand All @@ -16,15 +17,128 @@ public function setUp(): void
$this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());

$this->options = [
'apiUsername' => 'merchant',
'apiKey' => 'apikey',
'amount' => '1.45',
'apiUsername' => 'Test_Merchant',
'apiKey' => 'ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890',
'currency' => 'NZD',
'cancelUrl' => 'https://www.example.com/cancel',
'notifyUrl' => 'https://www.example.com/notify',
'returnUrl' => 'https://www.example.com/return',
'transactionId' => '123abc',
'transactionId' => 'transaction123',
'testMode' => true,
'description' => 'Test Transaction',
'type' => 'purchase'
];
}

public function testPurchaseSuccess()
{
$this->setMockHttpResponse('PurchaseSuccess.txt');

$request = $this->gateway->purchase($this->options);
$response = $request->send();

$this->assertFalse($response->isSuccessful());
$this->assertTrue($response->isRedirect());
$this->assertSame('GET', $response->getRedirectMethod());
$this->assertSame('https://uat.windcave.com/pxmi3/session1234', $response->getRedirectUrl());
$this->assertSame([], $response->getRedirectData());
}

public function testCompletePurchaseSuccess()
{
$this->setMockHttpResponse('CompletePurchaseSuccess.txt');

$request = $this->gateway->completePurchase([
'apiUsername' => $this->options['apiUsername'],
'apiKey' => $this->options['apiKey'],
'sessionId' => 'session123',
'testMode' => true,
]);

$this->getHttpRequest()->request->set('sessionId','session123');

$response = $request->send();

$this->assertTrue($response->isSuccessful());
$this->assertSame('APPROVED', $response->getResponseText());
$this->assertSame('session123', $response->getSessionId());
$this->assertSame('merchantRef123', $response->getTransactionId());
$this->assertSame('transaction123', $response->getTransactionReference());
$this->assertSame('411111......1111', $response->getCard()['cardNumber']);
}

public function testAcceptNotificationPaid()
{
$this->setMockHttpResponse('CompletePurchaseSuccess.txt');

$this->getHttpRequest()->request->set('sessionId','session123');

$notification = $this->gateway->acceptNotification([
'apiUsername' => $this->options['apiUsername'],
'apiKey' => $this->options['apiKey'],
'sessionId' => 'session123',
'testMode' => true,
]);

$this->assertSame(NotificationInterface::STATUS_COMPLETED, $notification->getTransactionStatus());
$this->assertSame('transaction123', $notification->getTransactionReference());
}


public function testAcceptNotificationFailed()
{
$this->setMockHttpResponse('CompletePurchaseFailed.txt');

$this->getHttpRequest()->request->set('sessionId','session123');

$notification = $this->gateway->acceptNotification([
'apiUsername' => $this->options['apiUsername'],
'apiKey' => $this->options['apiKey'],
'sessionId' => 'SESSION123',
'testMode' => true,
]);

$this->assertSame(NotificationInterface::STATUS_FAILED, $notification->getTransactionStatus());
}

public function testRefundSuccess()
{
$this->setMockHttpResponse('RefundSuccess.txt');

$request = $this->gateway->refund([
'apiUsername' => $this->options['apiUsername'],
'apiKey' => $this->options['apiKey'],
'amount' => '1.00',
'currency' => 'GBP',
'transactionReference' => 'refundtransaction123',
'testMode' => true,
]);

$response = $request->send();

$this->assertTrue($response->isSuccessful());
$this->assertSame('refundtransaction123', $response->getTransactionReference());
$this->assertSame('APPROVED', $response->getMessage());
}

public function testRefundFailed()
{
$this->setMockHttpResponse('RefundFailed.txt');

$request = $this->gateway->refund([
'apiUsername' => $this->options['apiUsername'],
'apiKey' => $this->options['apiKey'],
'amount' => '1.00',
'currency' => 'GBP',
'transactionReference' => 'refundtransaction123',
'testMode' => true,
]);

$response = $request->send();

$this->assertFalse($response->isSuccessful());
$this->assertSame('refundtransaction123', $response->getTransactionReference());
$this->assertSame('DECLINED', $response->getMessage());
}
}
Loading