Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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());
}
}
126 changes: 126 additions & 0 deletions tests/Message/PurchaseRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Omnipay\WindcaveHpp\Message;

use Omnipay\Tests\TestCase;

class PurchaseRequestTest extends TestCase
{
/** @var PurchaseRequest */
private $request;

public function setUp(): void
{
$this->options = [
'apiUsername' => 'merchant',
'apiKey' => 'apikey',
'amount' => '1.45',
'currency' => 'NZD',
'returnUrl' => 'https://example.com/return',
'cancelUrl' => 'https://example.com/cancel',
'notifyUrl' => 'https://example.com/notify',
'transactionId' => 'transaction123',
'testMode' => true,
];

$this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize($this->options);
}

public function testApiKeys(): void
{
$this->request->setApiUsername('user123');
$this->request->setApiKey('key123');

$this->assertSame('user123', $this->request->getApiUsername());
$this->assertSame('key123', $this->request->getApiKey());
}

public function testRequiredParameters(): void
{
$data = $this->request->getData();

$this->assertArrayHasKey('amount', $data);
$this->assertArrayHasKey('currency', $data);
}

public function testRedirect(): void
{
$this->setMockHttpResponse('PurchaseSuccess.txt');

$response = $this->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 testReturnUrls(): void
{
$this->request->setReturnUrl('https://example.com/return');
$this->request->setDeclineUrl('https://example.com/decline');
$this->request->setCancelUrl('https://example.com/cancel');
$this->request->setNotifyUrl('https://example.com/notify');

$data = $this->request->getData();

$this->assertSame('https://example.com/return', $data['callbackUrls']['approved']);
$this->assertSame('https://example.com/decline', $data['callbackUrls']['declined']);
$this->assertSame('https://example.com/cancel', $data['callbackUrls']['cancelled']);
$this->assertSame('https://example.com/notify', $data['notificationUrl']);
}

public function testCreateTokenSetsStoreCardTrue(): void
{
$this->request->setCreateToken(true);
$data = $this->request->getData();
$this->assertArrayHasKey('storeCard', $data);
$this->assertTrue($data['storeCard']);
}

public function testStoredCardIndicator(): void
{
$this->request->setStoredCardIndicator('credentialonfileinitial');
$data = $this->request->getData();
$this->assertSame('credentialonfileinitial', $data['storedCardIndicator']);
}

public function testRecurringFields(): void
{
$this->request->setRecurringFrequency('monthly');
$this->request->setRecurringExpiry('9999-12-31');
$data = $this->request->getData();
$this->assertSame('monthly', $data['recurringFrequency']);
$this->assertSame('9999-12-31', $data['recurringExpiry']);
}

public function testCardIdFromToken(): void
{
$this->request->setToken('tok_abc');
$data = $this->request->getData();
$this->assertSame('tok_abc', $data['cardId']);
}

public function testCardIdFromCardReference(): void
{
$this->request->setToken(null);
$this->request->setCardReference('card_ref_123');
$data = $this->request->getData();
$this->assertSame('card_ref_123', $data['cardId']);
}

public function testPaymentMethodsAndCardTypesAndMetadata(): void
{
$this->request->setPaymentMethods(['card', 'alipay']);
$this->request->setCardTypes(['visa', 'mastercard']);
$this->request->setMetadata(['key' => 'value']);

$data = $this->request->getData();

$this->assertSame(['card', 'alipay'], $data['methods']);
$this->assertSame(['visa', 'mastercard'], $data['cardTypes']);
$this->assertSame(['key' => 'value'], $data['metaData']);
}
}
36 changes: 36 additions & 0 deletions tests/Message/PurchaseResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Omnipay\WindcaveHpp\Message;

use Omnipay\Tests\TestCase;
use Omnipay\Common\Message\RequestInterface;

class PurchaseResponseTest extends TestCase
{
public function testGetRedirectUrlFindsHppLink(): void
{
$response = new PurchaseResponse($this->getMockRequest(), (object) [
'links' => [
(object) ['rel' => 'self', 'href' => 'https://uat.windcave.com/api/v1/sessions/session1234', 'method' => 'GET'],
(object) ['rel' => 'hpp', 'href' => 'https://uat.windcave.com/pxmi3/session1234', 'method' => 'REDIRECT'],
(object) ['rel' => 'hpp', 'href' => 'https://uat.windcave.com/api/v1/sessions/session1234', 'method' => 'PATCH'],
],
]);

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

public function testGetRedirectUrlThrowsWhenMissing(): void
{
$this->expectException(\Omnipay\Common\Exception\InvalidResponseException::class);

$resp = new PurchaseResponse($this->getMockRequest(), (object) ['links' => [(object) ['rel' => 'self', 'href' => 'x']]]);

$resp->getRedirectUrl();
}
}
63 changes: 63 additions & 0 deletions tests/Message/RefundRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Omnipay\WindcaveHpp\Message;
use Omnipay\WindcaveHpp\Gateway;
use Omnipay\Tests\TestCase;

class RefundRequestTest extends TestCase
{
protected $request;
protected $gateway;

protected function setUp(): void
{
$this->options = [
'apiUsername' => 'merchant',
'apiKey' => 'apikey',
'amount' => '1.45',
'currency' => 'NZD',
];

$this->request = new RefundRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize($this->options);
$this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());
}

public function testApiKeys(): void
{
$this->request->setApiUsername('user123');
$this->request->setApiKey('key123');

$this->assertSame('user123', $this->request->getApiUsername());
$this->assertSame('key123', $this->request->getApiKey());
}

public function testRequiredParameters(): void
{
$this->expectException(\Omnipay\Common\Exception\InvalidRequestException::class);
$data = $this->request->getData();

$this->assertArrayHasKey('amount', $data);
$this->assertArrayHasKey('currency', $data);
}

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

$request = $this->gateway->refund([
'apiUsername' => 'merchant',
'apiKey' => 'apikey',
'amount' => '1.45',
'currency' => 'NZD',
'transactionReference' => 'refundtransaction123',
'testMode' => true,
]);

$response = $request->send();

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