Skip to content

Commit

Permalink
Merge pull request #435 from omise/release-v3.1.1
Browse files Browse the repository at this point in the history
Release v3.1.1
  • Loading branch information
ajzkk authored Jun 12, 2023
2 parents 51a24f0 + 2616c8e commit 5649dbf
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## [v3.1.1 _(Jun, 12, 2023)_](https://github.com/omise/omise-magento/releases/tag/v3.1.1)
- Bug fixed: Atome failed to create charge with bundle product. (PR: [#433](https://github.com/omise/omise-magento/pull/433))

## [v3.1.0 _(May 15, 2023)_](https://github.com/omise/omise-magento/releases/tag/v3.1.0)
- Added PayPay payment method. (PR: [#428](https://github.com/omise/omise-magento/pull/428))
- Get installment minimum from capability API. (PR: [#427](https://github.com/omise/omise-magento/pull/427))
Expand Down
25 changes: 16 additions & 9 deletions Gateway/Request/APMBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,23 @@ private function getOrderItems($order)
$itemArray = [];
$items = $order->getItems();
$currency = $order->getCurrencyCode();
foreach ($items as $itemObject) {
$item = $itemObject->toArray();

foreach ($items as $item) {
$price = $item->getPrice();
// if item has parent item, it mean it's sub product
if ($item->getParentItem()) {
continue;
}
// since core-api validation failed for item with price zero,
// removing item with price zero
if ((float) $price === 0.0) {
continue;
}
$itemArray[] = [
'sku' => $item['sku'],
'name' => $item['name'],
'amount' => $this->money->setAmountAndCurrency(
$item['base_original_price'],
$currency
)->toSubunit(),
'quantity' => $item['qty_ordered'],
'sku' => $item->getSku(),
'name' => $item->getName(),
'amount' => $this->money->setAmountAndCurrency($price, $currency)->toSubunit(),
'quantity' => $item->getQtyOrdered(),
];
}
return $itemArray;
Expand Down
13 changes: 9 additions & 4 deletions Gateway/Validator/APMRequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public function build(array $buildSubject)
$paymentDataObject = SubjectReader::readPayment($buildSubject);
$order = $paymentDataObject->getOrder();
$paymentInfo = $paymentDataObject->getPayment();
$subTotal = $paymentInfo->getOrder()->getSubTotal();

switch ($paymentInfo->getMethod()) {
case Atome::CODE:
$this->validateAtomePhoneNumber($order, $paymentInfo);
$this->validateAtomeAmount($order);
$this->validateAtomePhoneNumber($paymentInfo);
$this->validateAtomeAmount($order, $subTotal);
break;
default:
break;
Expand All @@ -41,7 +42,7 @@ public function build(array $buildSubject)
*
* @return void
*/
private function validateAtomePhoneNumber($order, $info)
private function validateAtomePhoneNumber($info)
{
$number = $info->getAdditionalInformation(AtomeDataAssignObserver::PHONE_NUMBER);
$phonePattern = "/(\+)?([0-9]{10,13})/";
Expand All @@ -61,7 +62,7 @@ private function validateAtomePhoneNumber($order, $info)
*
* @return void
*/
private function validateAtomeAmount($order)
private function validateAtomeAmount($order, $subTotal)
{
$limits = [
'THB' => [
Expand All @@ -81,6 +82,10 @@ private function validateAtomeAmount($order)
$currency = strtoupper($order->getCurrencyCode());
$amount = $order->getGrandTotalAmount();

if ((float) $subTotal === 0.0) {
throw new LocalizedException(__('Complimentary products cannot be billed'));
}

if (!isset($limits[$currency])) {
throw new LocalizedException(__('Currency not supported'));
}
Expand Down
4 changes: 4 additions & 0 deletions Test/Mock/InfoMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public function getMethod()
public function getPayment()
{
}

public function getOrder()
{
}
}
52 changes: 52 additions & 0 deletions Test/Mock/OrderMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Omise\Payment\Test\Mock;

use Magento\Payment\Gateway\Data\OrderAdapterInterface;

class OrderMock implements OrderAdapterInterface
{
public function getCurrencyCode()
{
}

public function getOrderIncrementId()
{
}

public function getCustomerId()
{
}

public function getBillingAddress()
{
}

public function getShippingAddress()
{
}

public function getStoreId()
{
}

public function getId()
{
}

public function getGrandTotalAmount()
{
}

public function getItems()
{
}

public function getRemoteIp()
{
}

public function getSubTotal()
{
}
}
65 changes: 35 additions & 30 deletions Test/Unit/APMRequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use PHPUnit\Framework\TestCase;
use Omise\Payment\Model\Config\Atome;
use Omise\Payment\Test\Mock\InfoMock;
use Magento\Customer\Api\Data\AddressInterface;
use Omise\Payment\Test\Mock\OrderMock;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Payment\Gateway\Data\OrderAdapterInterface;
use Omise\Payment\Gateway\Validator\APMRequestValidator;
use Magento\Payment\Gateway\Data\AddressAdapterInterface;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;

class APMRequestValidatorTest extends TestCase
{
Expand All @@ -19,30 +19,42 @@ class APMRequestValidatorTest extends TestCase

private $addressMock;

private $paymentDataObject;
private $paymentDataObjectMock;

private $model;

protected function setUp(): void
{
$this->addressMock = $this->getMockBuilder(AddressInterface::class)->getMock();
$this->addressMock = $this->getMockBuilder(AddressAdapterInterface::class)->getMock();
$this->addressMock->method('getCountryId')->willReturn('TH');

$this->orderMock = $this->getMockBuilder(OrderAdapterInterface::class)
->getMockForAbstractClass();
$this->orderMock = $this->getMockBuilder(OrderMock::class)->getMock();
$this->orderMock->method('getShippingAddress')->willReturn($this->addressMock);

$this->infoMock = $this->getMockBuilder(InfoMock::class)->getMock();
$this->infoMock->method('getMethod')->willReturn(Atome::CODE);
$this->infoMock->method('getOrder')->willReturn($this->orderMock);

$this->paymentDataObject = new PaymentDataObject(
$this->orderMock,
$this->infoMock
);
$this->paymentDataObjectMock = $this->getMockBuilder(PaymentDataObjectInterface::class)->getMock();
$this->paymentDataObjectMock->method('getOrder')->willReturn($this->orderMock);
$this->paymentDataObjectMock->method('getPayment')->willReturn($this->infoMock);

$this->model = new APMRequestValidator();
}

/**
* @covers Omise\Payment\Gateway\Validator\APMRequestValidator
*/
public function testComplimentaryProducts()
{
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('Complimentary products cannot be billed');
$this->orderMock->method('getSubTotal')->willReturn(0.0);
$this->orderMock->method('getCurrencyCode')->willReturn("THB");
$this->infoMock->method('getAdditionalInformation')->willReturn('0987654321');
$this->model->build(['payment' => $this->paymentDataObjectMock]);
}

/**
* @covers Omise\Payment\Gateway\Validator\APMRequestValidator
*/
Expand All @@ -51,11 +63,9 @@ public function testCurrencyNotSupported()
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('Currency not supported');
$this->orderMock->method('getCurrencyCode')->willReturn("USD");
$this->orderMock->method('getGrandTotalAmount')->willReturn(100);
$this->orderMock->method('getSubTotal')->willReturn(100);
$this->infoMock->method('getAdditionalInformation')->willReturn('0987654321');
$this->model->build([
'payment' => $this->paymentDataObject,
]);
$this->model->build(['payment' => $this->paymentDataObjectMock]);
}

/**
Expand All @@ -67,10 +77,9 @@ public function testMinAmountShouldThrowError()
$this->expectExceptionMessage('Amount must be greater than 20.00 THB');
$this->orderMock->method('getCurrencyCode')->willReturn("THB");
$this->orderMock->method('getGrandTotalAmount')->willReturn(10);
$this->orderMock->method('getSubTotal')->willReturn(10);
$this->infoMock->method('getAdditionalInformation')->willReturn('0987654321');
$this->model->build([
'payment' => $this->paymentDataObject,
]);
$this->model->build(['payment' => $this->paymentDataObjectMock]);
}

/**
Expand All @@ -81,10 +90,9 @@ public function testValidAmountShouldNotThrowError()
$this->expectNotToPerformAssertions();
$this->orderMock->method('getCurrencyCode')->willReturn("THB");
$this->orderMock->method('getGrandTotalAmount')->willReturn(20);
$this->orderMock->method('getSubTotal')->willReturn(20);
$this->infoMock->method('getAdditionalInformation')->willReturn('0987654321');
$this->model->build([
'payment' => $this->paymentDataObject,
]);
$this->model->build(['payment' => $this->paymentDataObjectMock]);
}

/**
Expand All @@ -96,10 +104,9 @@ public function testMaxAmountShouldThrowError()
$this->expectExceptionMessage('Amount must be less than 150,000.00 THB');
$this->orderMock->method('getCurrencyCode')->willReturn("THB");
$this->orderMock->method('getGrandTotalAmount')->willReturn(200000);
$this->orderMock->method('getSubTotal')->willReturn(200000);
$this->infoMock->method('getAdditionalInformation')->willReturn('0987654321');
$this->model->build([
'payment' => $this->paymentDataObject,
]);
$this->model->build(['payment' => $this->paymentDataObjectMock]);
}

/**
Expand All @@ -112,9 +119,8 @@ public function testInvalidAtomePhoneNumberValidation()
$this->infoMock->method('getAdditionalInformation')->willReturn('0987');
$this->orderMock->method('getCurrencyCode')->willReturn("THB");
$this->orderMock->method('getGrandTotalAmount')->willReturn(100);
$this->model->build([
'payment' => $this->paymentDataObject,
]);
$this->orderMock->method('getSubTotal')->willReturn(100);
$this->model->build(['payment' => $this->paymentDataObjectMock]);
}

/**
Expand All @@ -126,8 +132,7 @@ public function testValidAtomePhoneNumberValidation()
$this->infoMock->method('getAdditionalInformation')->willReturn('+66987654321');
$this->orderMock->method('getCurrencyCode')->willReturn("THB");
$this->orderMock->method('getGrandTotalAmount')->willReturn(100);
$this->model->build([
'payment' => $this->paymentDataObject,
]);
$this->orderMock->method('getSubTotal')->willReturn(100);
$this->model->build(['payment' => $this->paymentDataObjectMock]);
}
}
Loading

0 comments on commit 5649dbf

Please sign in to comment.