Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v5.5.0 #409

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

### [v5.5.0 _(Oct 17, 2023)_](https://github.com/omise/omise-woocommerce/releases/tag/v5.5.0)
- Added dynamic webhook. (PR [#407](https://github.com/omise/omise-woocommerce/pull/407))
- Add QR expires countdown for PromptPay. (PR [#408](https://github.com/omise/omise-woocommerce/pull/408))

### [v5.4.1 _(Oct 3, 2023)_](https://github.com/omise/omise-woocommerce/releases/tag/v5.4.1)
- Added payment instructions in thank you page for PromptPay payment. (PR [#401](https://github.com/omise/omise-woocommerce/pull/404))

Expand Down
43 changes: 43 additions & 0 deletions assets/javascripts/omise-promptpay-count-down.js
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)
})()
2 changes: 1 addition & 1 deletion includes/admin/class-omise-page-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function render() {

$settings = $page->get_settings();

// This variable is used in the view.
// Although this variable looks like unsed, it will be available in the view.
$available_payment_methods = [];
$capabilities = Omise_Capabilities::retrieve();

Expand Down
46 changes: 34 additions & 12 deletions includes/admin/views/omise-page-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,47 @@

<table class="form-table">
<tbody>
<tr>
<th scope="row"><label><?php _e( 'Enable Dynamic Webhook', 'omise' ); ?></label></th>
<td>
<fieldset>
<select class="regular-text" name="dynamic_webhook" id="dynamic_webhook">
<option
value="0"
<?php echo $settings['dynamic_webhook'] ? 'selected' : '' ?>
>
No
</option>
<option
value="1"
<?php echo $settings['dynamic_webhook'] ? 'selected' : '' ?>
>
Yes
</option>
</select>
<p class="description">
<?php
echo __( 'If enabled, charge and refund events will be automatically set to be received at the URL below. This can be useful when you need multiple webhook endpoints on the same account. ' );
?>
</fieldset>
</td>
</tr>
<tr>
<th scope="row"><label><?php _e( 'Webhook endpoint', 'omise' ); ?></label></th>
<td>
<fieldset>
<code><?php echo get_rest_url( null, 'omise/webhooks' ); ?></code>
<p class="description">
<?php
echo sprintf(
wp_kses(
__( 'To enable <a href="%s">WebHooks</a> feature, you must setup an endpoint at <a href="%s"><strong>Opn Payments dashboard</strong></a> by using the above url <em>(HTTPS only)</em>.', 'omise' ),
array(
'a' => array( 'href' => array() ),
'em' => array(),
'strong' => array()
)
),
esc_url( 'https://www.omise.co/api-webhooks' ),
esc_url( 'https://dashboard.omise.co/v2/settings/webhooks' )
);
echo sprintf(
wp_kses(
__( 'Unless dynamic webhooks are enabled, you must add the URL below as a new endpoint on your <a href="%s">Opn Payments dashboard</a> (HTTPS only).', 'omise' ),
[
'a' => ['href' => []],
],
),
esc_url( 'https://dashboard.omise.co/v2/settings/webhooks' )
);
?>
</fieldset>
</td>
Expand Down
8 changes: 7 additions & 1 deletion includes/class-omise-setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function get_default_settings() {
'test_private_key' => '',
'live_public_key' => '',
'live_private_key' => '',
'dynamic_webhook' => 0,
'backends' => null,
);
}
Expand Down Expand Up @@ -138,7 +139,6 @@ public function update_settings( $data ) {
*/
public function is_test() {
$sandbox = $this->settings['sandbox'];

return isset( $sandbox ) && $sandbox == 'yes';
}

Expand Down Expand Up @@ -171,4 +171,10 @@ public function secret_key() {

return $this->settings['live_private_key'];
}

public function is_dynamic_webhook_enabled()
{
$dynamic_webhook = $this->settings['dynamic_webhook'];
return (bool)$dynamic_webhook;
}
}
23 changes: 14 additions & 9 deletions includes/gateway/abstract-omise-payment-base-card.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
abstract class Omise_Payment_Base_Card extends Omise_Payment
{
use Charge_Request_Builder;

const PAYMENT_ACTION_AUTHORIZE = 'manual_capture';
const PAYMENT_ACTION_AUTHORIZE_CAPTURE = 'auto_capture';

Expand Down Expand Up @@ -63,19 +65,22 @@ private function prepareChargeData($order_id, $order, $omise_customer_id, $card_
$data = [
'amount' => Omise_Money::to_subunit($order->get_total(), $currency),
'currency' => $currency,
'description' => apply_filters(
'omise_charge_params_description',
'WooCommerce Order id ' . $order_id,
$order
),
'return_uri' => $this->getRedirectUrl('omise_callback', $order_id, $order),
'metadata' => $this->getMetadata(
'description' => 'WooCommerce Order id ' . $order_id,
'return_uri' => $this->get_redirect_url('omise_callback', $order_id, $order),
'metadata' => $this->get_metadata(
$order_id,
$order,
[ 'secure_form_enabled' => $this->getSecureFormState()]
)
),
];

$omise_settings = Omise_Setting::instance();

if ($omise_settings->is_dynamic_webhook_enabled()) {
$data = array_merge($data, [
'webhook_endpoints' => [ Omise_Util::get_webhook_url() ],
]);
}

if (!empty($omise_customer_id) && ! empty($card_id)) {
$data['customer'] = $omise_customer_id;
$data['card'] = $card_id;
Expand Down
22 changes: 8 additions & 14 deletions includes/gateway/abstract-omise-payment-offline.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,22 @@
/**
* @since 4.0
*/
abstract class Omise_Payment_Offline extends Omise_Payment {
abstract class Omise_Payment_Offline extends Omise_Payment
{
use Charge_Request_Builder;

protected $enabled_processing_notification = true;

/**
* @inheritdoc
*/
public function charge( $order_id, $order ) {
$total = $order->get_total();
$currency = $order->get_currency();
$metadata = array_merge(
apply_filters( 'omise_charge_params_metadata', array(), $order ),
array( 'order_id' => $order_id ) // override order_id as a reference for webhook handlers.
public function charge( $order_id, $order )
{
$requestData = $this->build_charge_request(
$order_id, $order, $this->source_type
);

return OmiseCharge::create( array(
'amount' => Omise_Money::to_subunit( $total, $currency ),
'currency' => $currency,
'description' => apply_filters( 'omise_charge_params_description', 'WooCommerce Order id ' . $order_id, $order ),
'source' => array( 'type' => $this->source_type ),
'metadata' => $metadata
) );
return OmiseCharge::create($requestData);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions includes/gateway/abstract-omise-payment-offsite.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
abstract class Omise_Payment_Offsite extends Omise_Payment
{
use Charge_Request_Builder;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -48,4 +50,16 @@ public function check_bank_selected($fields, $errors)
$errors->add('validation', __('Please select bank below', 'omise'));
}
}

/**
* Override charge() method in the child class if the payment method requires
* more data than received from build_charge_request()
*/
public function charge($order_id, $order)
{
$requestData = $this->build_charge_request(
$order_id, $order, $this->source_type, $this->id . "_callback"
);
return OmiseCharge::create($requestData);
}
}
16 changes: 0 additions & 16 deletions includes/gateway/class-omise-payment-alipay.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,4 @@ public function init_form_fields() {
),
);
}

/**
* @inheritdoc
*/
public function charge($order_id, $order)
{
$currency = $order->get_currency();
return OmiseCharge::create([
'amount' => Omise_Money::to_subunit($order->get_total(), $currency),
'currency' => $currency,
'description' => apply_filters('omise_charge_params_description', 'WooCommerce Order id ' . $order_id, $order),
'source' => ['type' => $this->source_type],
'return_uri' => $this->getRedirectUrl('omise_alipay_callback', $order_id, $order),
'metadata' => $this->getMetadata($order_id, $order)
]);
}
}
23 changes: 12 additions & 11 deletions includes/gateway/class-omise-payment-alipayplus.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,19 @@ public function init_form_fields() {
*/
public function charge($order_id, $order)
{
$currency = $order->get_currency();
return OmiseCharge::create([
'amount' => Omise_Money::to_subunit($order->get_total(), $currency),
'currency' => $currency,
'description' => apply_filters('omise_charge_params_description', 'WooCommerce Order id ' . $order_id, $order),
'source' => [
'type' => $this->source_type,
'platform_type' => Omise_Util::get_platform_type(wc_get_user_agent())
],
'return_uri' => $this->getRedirectUrl('omise_' . $this->source_type . '_callback', $order_id, $order),
'metadata' => $this->getMetadata($order_id, $order)
$requestData = $this->get_charge_request($order_id, $order);
return OmiseCharge::create($requestData);
}

public function get_charge_request($order_id, $order)
{
$requestData = $this->build_charge_request(
$order_id, $order, $this->source_type, $this->id . "_callback"
);
$requestData['source'] = array_merge($requestData['source'], [
'platform_type' => Omise_Util::get_platform_type(wc_get_user_agent())
]);
return $requestData;
}
}

Expand Down
37 changes: 19 additions & 18 deletions includes/gateway/class-omise-payment-atome.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,32 @@ private function validateAtomeRequest()
*/
public function charge($order_id, $order)
{
$currency = $order->get_currency();
$requestData = $this->get_charge_request($order_id, $order);
return OmiseCharge::create($requestData);
}

public function get_charge_request($order_id, $order)
{
$requestData = $this->build_charge_request(
$order_id,
$order,
$this->source_type,
$this->id . "_callback"
);

$default_phone_selected = isset($_POST['omise_atome_phone_default']) ?
$_POST['omise_atome_phone_default']
: false;
$phone_number = (bool)$default_phone_selected ?
$order->get_billing_phone()
: sanitize_text_field($_POST['omise_atome_phone_number']);
$requestData['source'] = array_merge($requestData['source'], [
'phone_number' => $phone_number,
'shipping' => $this->getAddress($order),
'items' => $this->getItems($order, $order->get_currency())
]);

return OmiseCharge::create([
'amount' => Omise_Money::to_subunit($order->get_total(), $currency),
'currency' => $currency,
'description' => apply_filters(
'omise_charge_params_description',
'WooCommerce Order id ' . $order_id,
$order
),
'source' => [
'type' => $this->source_type,
'phone_number' => $phone_number,
'shipping' => $this->getAddress($order),
'items' => $this->getItems($order, $currency)
],
'return_uri' => $this->getRedirectUrl('omise_atome_callback', $order_id, $order),
'metadata' => $this->getMetadata($order_id, $order)
]);
return $requestData;
}

private function getAddress($order)
Expand Down
16 changes: 0 additions & 16 deletions includes/gateway/class-omise-payment-boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,6 @@ public function init_form_fields() {
);
}

/**
* @inheritdoc
*/
public function charge($order_id, $order)
{
$currency = $order->get_currency();
return OmiseCharge::create([
'amount' => Omise_Money::to_subunit($order->get_total(), $currency),
'currency' => $currency,
'description' => apply_filters('omise_charge_params_description', 'WooCommerce Order id ' . $order_id, $order),
'source' => ['type' => $this->source_type],
'return_uri' => $this->getRedirectUrl('omise_boost_callback', $order_id, $order),
'metadata' => $this->getMetadata($order_id, $order)
]);
}

/**
* Get icons
*
Expand Down
Loading
Loading