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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user