-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QR expires count down for promptpay (#408)
* add expires in count down for promptpay QR * add test for promptpay qr expires at * separate class for file get contents * fix error on test * remove unused function
- Loading branch information
Showing
7 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(function () { | ||
let countDownInterval; | ||
|
||
function calculateCountdown() { | ||
const currentDateTime = new Date(); | ||
const expiresAtDateTime = new Date(omise.qr_expires_at); | ||
const difference = expiresAtDateTime - currentDateTime; | ||
const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | ||
const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)); | ||
const seconds = Math.floor((difference % (1000 * 60)) / 1000); | ||
return { hours, minutes, seconds }; | ||
} | ||
|
||
function padZero(num) { | ||
return num.toString().padStart(2, '0'); | ||
} | ||
|
||
function updateCountdown(fromInterval = true) { | ||
const countdownDisplay = document.getElementById('countdown'); | ||
if(!countdownDisplay) { | ||
return; | ||
} | ||
|
||
const { hours, minutes, seconds } = calculateCountdown(); | ||
|
||
if (hours + minutes + seconds < 0) { | ||
// To prevent infinite loading, we need to reload and clear interval | ||
// only when it is from setInterval function. | ||
if (fromInterval) { | ||
clearInterval(countDownInterval) | ||
window.location.reload() | ||
} | ||
return; | ||
} | ||
|
||
countdownDisplay.innerHTML = `${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`; | ||
if (!countDownInterval) { | ||
countDownInterval = setInterval(updateCountdown, 1000); | ||
} | ||
} | ||
|
||
updateCountdown(false) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
includes/libraries/omise-plugin/helpers/file_get_contents_wrapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
/** | ||
* file_get_contents wrapper class | ||
* | ||
* Since we cannot mock global function, | ||
* we have to create a wrapper for file_get_contents. | ||
*/ | ||
class File_Get_Contents_Wrapper | ||
{ | ||
public static function get_contents($url) | ||
{ | ||
return file_get_contents($url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
tests/unit/includes/gateway/class-omise-payment-promptpay-test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
use Mockery\Mock; | ||
|
||
class Omise_Payment_Promptpay_Test extends Omise_Payment_Offline_Test | ||
{ | ||
public $mockOrder; | ||
public $mockWcDateTime; | ||
public $mockLocalizeScript; | ||
public $mockOmisePluginHelper; | ||
public $mockOmisePaymentOffline; | ||
public $mockOmiseCharge; | ||
public $mockFileGetContent; | ||
|
||
public function setUp(): void | ||
{ | ||
function wc_timezone_offset() {} | ||
function wp_create_nonce() {} | ||
function admin_url() {} | ||
|
||
$this->mockOrder = Mockery::mock(); | ||
$this->mockLocalizeScript = Mockery::mock(); | ||
$this->mockWcDateTime = Mockery::mock('overload:WC_DateTime'); | ||
$this->mockOmisePluginHelper = Mockery::mock('overload:OmisePluginHelperWcOrder')->shouldIgnoreMissing(); | ||
$this->mockOmisePaymentOffline = Mockery::mock('overload:Omise_Payment_Offline'); | ||
$this->mockOmiseCharge = Mockery::mock('overload:OmiseCharge'); | ||
$this->mockFileGetContent = Mockery::mock('overload:File_Get_Contents_Wrapper'); | ||
|
||
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-promptpay.php'; | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function textExpiresAtFieldIsPassedToJavascript() | ||
{ | ||
$expiresAt = '2023-11-22T14:48:00.000Z'; | ||
|
||
$this->mockFileGetContent->shouldReceive('get_contents')->once()->andReturn('<svg></svg>'); | ||
|
||
$this->mockOmisePaymentOffline->shouldReceive('init_settings'); | ||
$this->mockOmisePaymentOffline->shouldReceive('get_option'); | ||
$this->mockOmisePaymentOffline->shouldReceive('load_order')->andReturn(true); | ||
$this->mockOmisePaymentOffline->shouldReceive('get_charge_id_from_order')->andReturn('charge_xxx'); | ||
$this->mockOmisePaymentOffline->shouldReceive('get_pending_status')->andReturn('pending'); | ||
$this->mockOmisePaymentOffline->shouldReceive('file_get_contents')->andReturn(''); | ||
|
||
$this->mockWcDateTime->shouldReceive('set_utc_offset'); | ||
$this->mockWcDateTime->shouldReceive('format')->with('c')->andReturn($expiresAt); | ||
|
||
$this->mockOmiseCharge->shouldReceive('retrieve')->andReturn([ | ||
'status' => 'pending', | ||
'expires_at' => $expiresAt, | ||
'source' => [ | ||
'scannable_code' => [ | ||
'image' => [ | ||
'id' => 1, | ||
'download_uri' => '', | ||
] | ||
] | ||
] | ||
]); | ||
|
||
// check that qr_expires_at is passed to `omise-promptpay-count-down` script with omise object | ||
$this->mockLocalizeScript->shouldReceive('call') | ||
->with('omise-promptpay-count-down', 'omise', [ | ||
'qr_expires_at' => $expiresAt | ||
]); | ||
|
||
$GLOBALS['mock_wp_localize_script'] = $this->mockLocalizeScript; | ||
|
||
function wp_localize_script($scriptName, $object, $params) { | ||
return $GLOBALS['mock_wp_localize_script']->call($scriptName, $object, $params); | ||
} | ||
|
||
$obj = new Omise_Payment_Promptpay(); | ||
$result = $obj->display_qrcode($this->mockOrder, 'view'); | ||
$this->assertNull($result); | ||
} | ||
} |