Skip to content

Commit

Permalink
Added promptpay QR pyament instructions (#447)
Browse files Browse the repository at this point in the history
* add promptpay qr process info

* add test for charge expiry date

* fix m2 coding standard

* add test for toHtml

* remove disableOriginalConstructor from interface mock

* add test for payment dtetail hanldler

* fix m2 coding standard

* remove full stop

* remove payment mock

* EOF
  • Loading branch information
ajzkk authored Sep 29, 2023
1 parent 43b6f7a commit a65ace8
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Block/Checkout/Onepage/Success/PromptpayAdditionalInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@ protected function _toHtml()
}
$data['order_amount'] = $this->getOrderAmount();
$data['image_code'] = $this->getPaymentAdditionalInformation('image_code');
$data['charge_expires_at'] = $this->getChargeExpiryDate();
$this->addData($data);
return parent::_toHtml();
}

/**
* get expiry date
* @return string
*/
public function getChargeExpiryDate()
{
// Making sure that timezone is Thailand
date_default_timezone_set("Asia/Bangkok");
$timestamp = strtotime($this->getPaymentAdditionalInformation('charge_expires_at'));
return date("M d, Y h:i A", $timestamp);
}
}
1 change: 1 addition & 0 deletions Gateway/Response/PaymentDetailsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function handle(array $handlingSubject, array $response)
$payment->setAdditionalInformation('charge_id', $response['charge']->id);
$payment->setAdditionalInformation('charge_authorize_uri', $response['charge']->authorize_uri);
$payment->setAdditionalInformation('payment_type', $paymentType);
$payment->setAdditionalInformation('charge_expires_at', $response['charge']->expires_at);

$transaction = $this->transactionBuilder
->setPayment($payment)
Expand Down
75 changes: 75 additions & 0 deletions Test/Unit/PaymentDetailsHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Omise\Payment\Test\Unit;

use Omise\Payment\Gateway\Response\PaymentDetailsHandler;
use Omise\Payment\Helper\OmiseHelper;
use PHPUnit\Framework\TestCase;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface;
use Mockery as m;

class PaymentDetailsHandlerTest extends TestCase
{

private $omiseHelperMock;
private $curlMock;
private $builderMock;
private $paymentMock;
private $infoMock;
private $chargeMock;
private $orderMock;
private $currencyMock;

protected function setUp(): void
{
$this->omiseHelperMock = m::mock(OmiseHelper::class);
$this->curlMock = m::mock(Curl::class);
$this->builderMock = m::mock(BuilderInterface::class);
$this->paymentMock = m::mock(PaymentDataObjectInterface::class);
$this->infoMock = m::mock(OrderPaymentInterface::class);
$this->orderMock = m::mock(OrderInterface::class);
$this->currencyMock = m::mock();

$this->chargeMock = m::mock();
$this->chargeMock->id = 'charge_xxx';
$this->chargeMock->authorize_uri = 'https://omise.co/authorized';
$this->chargeMock->expires_at = '2023-09-29T06:49:35Z';
}

/**
* @covers Omise\Payment\Gateway\Response\PaymentDetailsHandler
*/
public function testHandler()
{
$this->omiseHelperMock->shouldReceive('isPayableByImageCode')->once();
$this->builderMock->shouldReceive('setOrder')->once()->andReturn($this->builderMock);
$this->builderMock->shouldReceive('setPayment')->once()->andReturn($this->builderMock);
$this->builderMock->shouldReceive('setTransactionId')->once()->andReturn($this->builderMock);
$this->builderMock->shouldReceive('setAdditionalInformation')->once()->andReturn($this->builderMock);
$this->builderMock->shouldReceive('setFailSafe')->once()->andReturn($this->builderMock);
$this->builderMock->shouldReceive('build')->once()->andReturn($this->builderMock);

$this->currencyMock->shouldReceive('formatTxt')->once();

$this->orderMock->shouldReceive('getBaseCurrency')->once()->andReturn($this->currencyMock);
$this->orderMock->shouldReceive('getTotalDue')->once()->andReturn(1000);

$this->infoMock->shouldReceive('getMethod')->once()->andReturn('promptpay');
$this->infoMock->shouldReceive('getOrder')->once()->andReturn($this->orderMock);
$this->infoMock->shouldReceive('setAdditionalInformation')->times(4);
$this->infoMock->shouldReceive('prependMessage')->once();
$this->infoMock->shouldReceive('addTransactionCommentsToOrder')->once();

$this->paymentMock->shouldReceive('getPayment')->andReturn($this->infoMock);

$model = new PaymentDetailsHandler($this->omiseHelperMock, $this->curlMock, $this->builderMock);
$model->handle(['payment' => $this->paymentMock], [
'charge' => $this->chargeMock
]);
$this->expectNotToPerformAssertions();
}
}
65 changes: 65 additions & 0 deletions Test/Unit/PromptpayAdditionalInformationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Omise\Payment\Test\Unit;

use Magento\Sales\Model\Order;
use PHPUnit\Framework\TestCase;
use Magento\Checkout\Model\Session;
use Magento\Framework\View\Element\Template\Context;
use Omise\Payment\Block\Checkout\Onepage\Success\PromptpayAdditionalInformation;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency;
use Mockery as m;

class PromptpayAdditionalInformationTest extends TestCase
{
private $contextMock;
private $checkoutSessionMock;
private $orderMock;
private $paymentMock;
private $eventManagerMock;
private $scopeConfigMock;
private $currencyMock;

protected function setUp(): void
{
$this->contextMock = m::mock(Context::class)->makePartial();
$this->checkoutSessionMock = m::mock(Session::class);
$this->orderMock = m::mock(Order::class);
$this->eventManagerMock = m::mock(ManagerInterface::class);
$this->scopeConfigMock = m::mock(ScopeConfigInterface::class);
$this->currencyMock = m::mock(Currency::class)->makePartial();
$this->paymentMock = m::mock();
}

/**
* @covers Omise\Payment\Block\Checkout\Onepage\Success\PromptpayAdditionalInformation
* @covers Omise\Payment\Block\Checkout\Onepage\Success\AdditionalInformation
*/
public function testPromptpayAdditionalInformation()
{
$this->paymentMock->shouldReceive('getData')->andReturn([
"amount_ordered" => 1000,
"additional_information" => [
"charge_expires_at" => "2023-09-29T06:49:35Z",
"payment_type" => "promptpay"
]
]);
$this->eventManagerMock->shouldReceive('dispatch')->times(2);
$this->scopeConfigMock->shouldReceive('getValue')->once();

$this->contextMock->shouldReceive('getEventManager')->andReturn($this->eventManagerMock);
$this->contextMock->shouldReceive('getScopeConfig')->andReturn($this->scopeConfigMock);

$this->orderMock->shouldReceive('getPayment')->andReturn($this->paymentMock);
$this->orderMock->shouldReceive('getOrderCurrency')->andReturn($this->currencyMock);
$this->checkoutSessionMock->shouldReceive('getLastRealOrder')->andReturn($this->orderMock);
$model = new PromptpayAdditionalInformation($this->contextMock, $this->checkoutSessionMock, []);

$this->assertEquals("Sep 29, 2023 01:49 PM", $model->getChargeExpiryDate());

$html = $model->toHtml();
$this->assertNotNull($html);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@
</strong>
<p style="margin:.5rem 0"><?= $block->escapeHtml(__('PromptPay QR Code.')); ?></p>
<div><img id="img_payment_code" src="<?= $block->escapeHtml($block->getImageCode()) ?>" /></div>
<p>
Payment expires at: <?= $block->escapeHtml($block->getChargeExpiresAt()); ?>
</p>
<p>
<button class="action secondary" onclick="window.print ? window.print() : alert('Print not available')">
<?= $block->escapeHtml(__('Print'))?></button>
</p>

<div style="padding: 15px; margin: 20px 0; border: 1px solid black">
<p><strong>To make payment:</strong></p>
<ul style="list-style: auto; margin-bottom: 0px; padding: 0px 15px;">
<li>Download the QR code or open your preferred bank app to scan it</li>
<li>Check that the payment details are correct</li>
<li>Import the QR code image into your bank app or scan the QR code with your bank app to pay</li>
<li>Share the payment slip from your bank app to the seller</li>
</ul>
</div>

0 comments on commit a65ace8

Please sign in to comment.