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

Added dynamic webhook #407

Merged
merged 27 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
053e976
Updated charge method to include webhook. Refactored some codes, adde…
Oct 10, 2023
447b226
Fixed broken tests.
Oct 10, 2023
b71df3a
Refactoring and fixing tests
Oct 10, 2023
a891fb4
Refactored and updated tests.
Oct 10, 2023
3e734d3
Refactored TrueMoney class and added tests.
Oct 10, 2023
bf04a53
Added dynamic webhook option and added tests.
Oct 11, 2023
ebd033c
Added few files and folders under exclusion in Sonar project properties.
Oct 11, 2023
01a0825
Added test for Atome.
Oct 11, 2023
0ed6a9c
Moved charge() method to Omise_Payment_Offsite to avoid repeating sam…
Oct 11, 2023
5953793
Added more tests
Oct 11, 2023
2995684
Added tets for Alipay+.
Oct 11, 2023
7eb6401
Added more test to cover charge method.
Oct 11, 2023
5d3543d
Added charge test for Duitnow OBW.
Oct 11, 2023
6932cd4
Added tests for FPX
Oct 11, 2023
9cd3bf9
Added tests for internet banking.
Oct 11, 2023
a8e07a5
Added tests for mobile banking, rabbit linepay and OCBC PAO
Oct 11, 2023
37483e4
Added missing trait in abstract-omise-payment-base-card.php
Oct 12, 2023
145fb58
removed unnecessary isset check for dynamic_webhook
Oct 12, 2023
b833c0a
Updated tests
Oct 12, 2023
741b0d3
Updated tests.
Oct 12, 2023
bbba4fe
Removed predefined return used for testing
Oct 12, 2023
edc3455
Added test for is_dynamic_webhook_enabled() in Omise_Setting class.
Oct 12, 2023
33619ef
Added test for get_webhook_url method.
Oct 12, 2023
72c52d4
updated tests.
Oct 12, 2023
838d1f6
Added test for Omise_Payment_Base_Card abstract class.
Oct 16, 2023
3ba8a92
Updated test
Oct 16, 2023
c9ab6c8
Fixed EOF
Oct 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'];
ajzkk marked this conversation as resolved.
Show resolved Hide resolved
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(
ajzkk marked this conversation as resolved.
Show resolved Hide resolved
'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(
ajzkk marked this conversation as resolved.
Show resolved Hide resolved
$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
17 changes: 6 additions & 11 deletions includes/gateway/class-omise-payment-duitnow-obw.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,14 @@ public function payment_fields()
*/
public function charge($order_id, $order)
{
$requestData = $this->build_charge_request(
$order_id, $order, $this->source_type, $this->id . "_callback"
);
$source_bank = isset($_POST['source']['bank']) ? $_POST['source']['bank'] : '';
$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,
'bank' => sanitize_text_field($source_bank),
],
'return_uri' => $this->getRedirectUrl('omise_duitnow_obw_callback', $order_id, $order),
'metadata' => $this->getMetadata($order_id, $order)
$requestData['source'] = array_merge($requestData['source'], [
'bank' => sanitize_text_field($source_bank),
]);
return OmiseCharge::create($requestData);
}

/**
Expand Down
16 changes: 0 additions & 16 deletions includes/gateway/class-omise-payment-duitnow-qr.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_duitnow_qr_callback', $order_id, $order),
'metadata' => $this->getMetadata($order_id, $order)
]);
}

/**
* Get icons
*
Expand Down