init
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Test\Unit\Model\Callback;
|
||||
|
||||
use Esun\Oceanpayment\Model\Callback\Parser;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ParserTest extends TestCase
|
||||
{
|
||||
public function testParsesOceanpaymentXmlNotification(): void
|
||||
{
|
||||
$xml = <<<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<response>
|
||||
<response_type>1</response_type>
|
||||
<account>995149</account>
|
||||
<terminal>99514901</terminal>
|
||||
<order_number>100000001</order_number>
|
||||
<order_currency>USD</order_currency>
|
||||
<order_amount>1.00</order_amount>
|
||||
<payment_id>PAY-123</payment_id>
|
||||
<payment_status>1</payment_status>
|
||||
<signValue>ABCDEF</signValue>
|
||||
</response>
|
||||
XML;
|
||||
|
||||
$result = (new Parser())->parse($xml, []);
|
||||
|
||||
self::assertSame('1', $result['response_type']);
|
||||
self::assertSame('100000001', $result['order_number']);
|
||||
self::assertSame('PAY-123', $result['payment_id']);
|
||||
self::assertSame('', $result['payment_risk']);
|
||||
}
|
||||
|
||||
public function testRejectsInvalidXml(): void
|
||||
{
|
||||
$this->expectException(LocalizedException::class);
|
||||
(new Parser())->parse('<response><broken></response>', []);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Test\Unit\Model\Callback;
|
||||
|
||||
use Esun\Oceanpayment\Model\Callback\Processor;
|
||||
use Esun\Oceanpayment\Model\Config;
|
||||
use Esun\Oceanpayment\Model\OrderStatusManager;
|
||||
use Esun\Oceanpayment\Model\Signature;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Sales\Api\OrderRepositoryInterface;
|
||||
use Magento\Sales\Model\Order;
|
||||
use Magento\Sales\Model\Order\Payment;
|
||||
use Magento\Sales\Model\OrderFactory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class ProcessorTest extends TestCase
|
||||
{
|
||||
public function testRejectsCallbackForDifferentPaymentId(): void
|
||||
{
|
||||
$config = $this->createMock(Config::class);
|
||||
$config->method('getSecret')->willReturnMap([
|
||||
['account', 1, Config::METHOD_CODE, 'account'],
|
||||
['terminal', 1, Config::METHOD_CODE, 'terminal'],
|
||||
['secure_code', 1, Config::METHOD_CODE, 'secure-code'],
|
||||
]);
|
||||
|
||||
$signature = $this->createMock(Signature::class);
|
||||
$signature->method('verifyCallback')->willReturn(true);
|
||||
|
||||
$payment = $this->createMock(Payment::class);
|
||||
$payment->method('getMethod')->willReturn(Config::METHOD_CODE);
|
||||
$payment->method('getAdditionalInformation')
|
||||
->with('qianhai_payment_id')
|
||||
->willReturn('expected-payment-id');
|
||||
|
||||
$order = $this->createMock(Order::class);
|
||||
$order->method('loadByIncrementId')->willReturnSelf();
|
||||
$order->method('getEntityId')->willReturn(1);
|
||||
$order->method('getPayment')->willReturn($payment);
|
||||
$order->method('getStoreId')->willReturn(1);
|
||||
$order->method('getOrderCurrencyCode')->willReturn('USD');
|
||||
$order->method('getGrandTotal')->willReturn(10.0);
|
||||
|
||||
$orderFactory = $this->createMock(OrderFactory::class);
|
||||
$orderFactory->method('create')->willReturn($order);
|
||||
|
||||
$processor = new Processor(
|
||||
$config,
|
||||
$signature,
|
||||
$orderFactory,
|
||||
$this->createMock(OrderRepositoryInterface::class),
|
||||
$this->createMock(OrderStatusManager::class),
|
||||
$this->createMock(LoggerInterface::class)
|
||||
);
|
||||
|
||||
$this->expectException(LocalizedException::class);
|
||||
$this->expectExceptionMessage('Oceanpayment callback payment ID verification failed.');
|
||||
|
||||
$processor->process([
|
||||
'account' => 'account',
|
||||
'terminal' => 'terminal',
|
||||
'order_number' => 'ORDER-1',
|
||||
'order_currency' => 'USD',
|
||||
'order_amount' => '10.00',
|
||||
'payment_id' => 'different-payment-id',
|
||||
'payment_status' => '1',
|
||||
], true);
|
||||
}
|
||||
|
||||
public function testSuccessfulCallbackRegistersCaptureTransaction(): void
|
||||
{
|
||||
$config = $this->createMock(Config::class);
|
||||
$config->method('getSecret')->willReturnMap([
|
||||
['account', 1, Config::METHOD_CODE, 'account'],
|
||||
['terminal', 1, Config::METHOD_CODE, 'terminal'],
|
||||
['secure_code', 1, Config::METHOD_CODE, 'secure-code'],
|
||||
]);
|
||||
$config->method('getValue')->willReturn('processing');
|
||||
|
||||
$signature = $this->createMock(Signature::class);
|
||||
$signature->method('verifyCallback')->willReturn(true);
|
||||
|
||||
$payment = $this->createMock(Payment::class);
|
||||
$payment->method('getMethod')->willReturn(Config::METHOD_CODE);
|
||||
$payment->method('getAdditionalInformation')->willReturnCallback(
|
||||
static fn(string $key): string => match ($key) {
|
||||
'qianhai_payment_id' => 'payment-id',
|
||||
default => '',
|
||||
}
|
||||
);
|
||||
$payment->method('setIsTransactionClosed')->willReturnSelf();
|
||||
$payment->expects(self::once())
|
||||
->method('registerCaptureNotification')
|
||||
->with(10.0, true)
|
||||
->willReturnSelf();
|
||||
|
||||
$order = $this->createMock(Order::class);
|
||||
$order->method('loadByIncrementId')->willReturnSelf();
|
||||
$order->method('getEntityId')->willReturn(1);
|
||||
$order->method('getPayment')->willReturn($payment);
|
||||
$order->method('getStoreId')->willReturn(1);
|
||||
$order->method('getOrderCurrencyCode')->willReturn('USD');
|
||||
$order->method('getGrandTotal')->willReturn(10.0);
|
||||
$order->method('getTotalDue')->willReturn(10.0);
|
||||
$order->method('canInvoice')->willReturn(true);
|
||||
|
||||
$orderFactory = $this->createMock(OrderFactory::class);
|
||||
$orderFactory->method('create')->willReturn($order);
|
||||
|
||||
$orderRepository = $this->createMock(OrderRepositoryInterface::class);
|
||||
$orderRepository->expects(self::once())->method('save')->with($order);
|
||||
|
||||
$statusManager = $this->createMock(OrderStatusManager::class);
|
||||
$statusManager->expects(self::once())
|
||||
->method('apply')
|
||||
->with($order, 'processing', Order::STATE_PROCESSING);
|
||||
|
||||
$processor = new Processor(
|
||||
$config,
|
||||
$signature,
|
||||
$orderFactory,
|
||||
$orderRepository,
|
||||
$statusManager,
|
||||
$this->createMock(LoggerInterface::class)
|
||||
);
|
||||
|
||||
self::assertSame(Processor::SUCCESS, $processor->process([
|
||||
'account' => 'account',
|
||||
'terminal' => 'terminal',
|
||||
'methods' => 'Credit Card',
|
||||
'order_number' => 'ORDER-1',
|
||||
'order_currency' => 'USD',
|
||||
'order_amount' => '10.00',
|
||||
'payment_id' => 'payment-id',
|
||||
'payment_status' => '1',
|
||||
'payment_details' => '80000:Transaction Approved',
|
||||
], true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Test\Unit\Model;
|
||||
|
||||
use Esun\Oceanpayment\Model\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ConfigTest extends TestCase
|
||||
{
|
||||
public function testCurrentAndLegacyPaymentMethodsAreSupported(): void
|
||||
{
|
||||
self::assertTrue(Config::isSupportedMethod(Config::METHOD_CODE));
|
||||
self::assertTrue(Config::isSupportedMethod(Config::ONEPAGE_METHOD_CODE));
|
||||
self::assertTrue(Config::isSupportedMethod(Config::LEGACY_METHOD_CODE));
|
||||
self::assertFalse(Config::isSupportedMethod('other_payment_method'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Test\Unit\Model\Gateway;
|
||||
|
||||
use Esun\Oceanpayment\Model\Gateway\ResponseParser;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ResponseParserTest extends TestCase
|
||||
{
|
||||
public function testParsesHostedCheckoutCreationResponse(): void
|
||||
{
|
||||
$xml = <<<'XML'
|
||||
<root>
|
||||
<account>995149</account>
|
||||
<terminal>99514901</terminal>
|
||||
<order_number>100000001</order_number>
|
||||
<order_currency>USD</order_currency>
|
||||
<order_amount>1.00</order_amount>
|
||||
<payment_id>PAY-123</payment_id>
|
||||
<pay_url>https://test-secure.oceanpayment.com/pay/123</pay_url>
|
||||
<pay_results>1</pay_results>
|
||||
<pay_details>Success</pay_details>
|
||||
<signValue>ABCDEF</signValue>
|
||||
</root>
|
||||
XML;
|
||||
|
||||
$result = (new ResponseParser())->parse($xml);
|
||||
|
||||
self::assertSame('PAY-123', $result['payment_id']);
|
||||
self::assertSame('1', $result['pay_results']);
|
||||
self::assertSame('https://test-secure.oceanpayment.com/pay/123', $result['pay_url']);
|
||||
}
|
||||
|
||||
public function testRejectsNonXmlResponse(): void
|
||||
{
|
||||
$this->expectException(LocalizedException::class);
|
||||
(new ResponseParser())->parse('bad gateway');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Oceanpayment\Test\Unit\Model;
|
||||
|
||||
use Esun\Oceanpayment\Model\Signature;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SignatureTest extends TestCase
|
||||
{
|
||||
private Signature $signature;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->signature = new Signature();
|
||||
}
|
||||
|
||||
public function testCreatesHostedCheckoutRequestSignatureInDocumentedOrder(): void
|
||||
{
|
||||
$data = [
|
||||
'account' => '995149',
|
||||
'terminal' => '99514901',
|
||||
'backUrl' => 'https://example.test/payment/back',
|
||||
'order_number' => '100000001',
|
||||
'order_currency' => 'USD',
|
||||
'order_amount' => '1.00',
|
||||
'billing_firstName' => 'Jane',
|
||||
'billing_lastName' => 'Doe',
|
||||
'billing_email' => 'jane@example.test',
|
||||
];
|
||||
|
||||
self::assertSame(
|
||||
'cb5715d1d66a44352b811b1d5c9aa74752244cb7c7304e9d2f0bc7fa18a820bf',
|
||||
$this->signature->createRequest($data, 'test-secure-code')
|
||||
);
|
||||
}
|
||||
|
||||
public function testVerifiesCallbackAndRejectsTampering(): void
|
||||
{
|
||||
$data = [
|
||||
'account' => '995149',
|
||||
'terminal' => '99514901',
|
||||
'order_number' => '100000001',
|
||||
'order_currency' => 'USD',
|
||||
'order_amount' => '1.00',
|
||||
'order_notes' => '',
|
||||
'card_number' => '411111***1111',
|
||||
'payment_id' => 'PAY-123',
|
||||
'payment_authType' => '0',
|
||||
'payment_status' => '1',
|
||||
'payment_details' => '80000:Transaction Approved',
|
||||
'payment_risk' => '',
|
||||
];
|
||||
$data['signValue'] = hash(
|
||||
'sha256',
|
||||
implode('', array_values($data)) . 'test-secure-code'
|
||||
);
|
||||
|
||||
self::assertSame($data['signValue'], $this->signature->createCallback($data, 'test-secure-code'));
|
||||
self::assertTrue($this->signature->verifyCallback($data, 'test-secure-code'));
|
||||
|
||||
$data['order_amount'] = '100.00';
|
||||
self::assertFalse($this->signature->verifyCallback($data, 'test-secure-code'));
|
||||
}
|
||||
|
||||
public function testCreatesEmbeddedRequestSignatureWithoutBackUrl(): void
|
||||
{
|
||||
$data = [
|
||||
'account' => '995149',
|
||||
'terminal' => '99514901',
|
||||
'backUrl' => 'https://example.test/payment/back',
|
||||
'order_number' => '100000001',
|
||||
'order_currency' => 'USD',
|
||||
'order_amount' => '1.00',
|
||||
'billing_firstName' => 'Jane',
|
||||
'billing_lastName' => 'Doe',
|
||||
'billing_email' => 'jane@example.test',
|
||||
];
|
||||
|
||||
self::assertSame(
|
||||
hash('sha256', '99514999514901100000001USD1.00JaneDoejane@example.testtest-secure-code'),
|
||||
$this->signature->createEmbeddedRequest($data, 'test-secure-code')
|
||||
);
|
||||
}
|
||||
|
||||
public function testSanitizesCharactersThatBreakOceanpaymentSignatures(): void
|
||||
{
|
||||
self::assertSame('John Test ', $this->signature->sanitize(' John <Test>\'" '));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user