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

Force decimals with JPY always to be 0 #279

Merged
merged 3 commits into from
Jul 25, 2024
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
19 changes: 19 additions & 0 deletions includes/class-wc-amazon-payments-advanced-api-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,25 @@ function( $v ) {

}

/**
* Format the charge amount, make sure that decimals with JPY is 0.
*
* @param array $charge_amount The charge amount.
*/
public static function format_charge_amount( $charge_amount ) {

if ( empty( $charge_amount['currencyCode'] ) || empty( $charge_amount['amount'] ) ) {
return $charge_amount;
}

switch ( $charge_amount['currencyCode'] ) {
case 'JPY':
$charge_amount['amount'] = WC_Amazon_Payments_Advanced::format_amount( $charge_amount['amount'], 0 );
}

return $charge_amount;
}

/**
* Validate API Keys signature
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,13 @@ public function recurring_checkout_session( $payload ) {
);

if ( 1 === $subscriptions_in_cart ) {
$first_recurring = reset( WC()->cart->recurring_carts );
$payload['recurringMetadata']['amount'] = array(
$first_recurring = reset( WC()->cart->recurring_carts );
$recurring_charge_amount = array(
'amount' => WC_Amazon_Payments_Advanced::format_amount( $first_recurring->get_total( 'edit' ) ),
'currencyCode' => get_woocommerce_currency(),
);

$payload['recurringMetadata']['amount'] = WC_Amazon_Payments_Advanced_API::format_charge_amount( $recurring_charge_amount );
}
} elseif ( $cart_contains_renewal || $change_payment_for_subscription ) {
if ( $cart_contains_renewal ) {
Expand All @@ -328,12 +330,14 @@ public function recurring_checkout_session( $payload ) {

$payload['chargePermissionType'] = 'Recurring';

$recurring_charge_amount = array(
'amount' => WC_Amazon_Payments_Advanced::format_amount( $subscription->get_total() ),
'currencyCode' => wc_apa_get_order_prop( $subscription, 'order_currency' ),
);

$payload['recurringMetadata'] = array(
'frequency' => $this->parse_interval_to_apa_frequency( $subscription->get_billing_period( 'edit' ), $subscription->get_billing_interval( 'edit' ) ),
'amount' => array(
'amount' => WC_Amazon_Payments_Advanced::format_amount( $subscription->get_total() ),
'currencyCode' => wc_apa_get_order_prop( $subscription, 'order_currency' ),
),
'amount' => WC_Amazon_Payments_Advanced_API::format_charge_amount( $recurring_charge_amount ),
);
}

Expand Down Expand Up @@ -386,10 +390,12 @@ public function recurring_checkout_session_update( $payload, $checkout_session_i
$recurring_total = wc_format_decimal( $recurring_total, '' );

if ( 1 === $subscriptions_in_cart ) {
$payload['recurringMetadata']['amount'] = array(
$recurring_charge_amount = array(
'amount' => WC_Amazon_Payments_Advanced::format_amount( $recurring_total ),
'currencyCode' => wc_apa_get_order_prop( $order, 'order_currency' ),
);

$payload['recurringMetadata']['amount'] = WC_Amazon_Payments_Advanced_API::format_charge_amount( $recurring_charge_amount );
}

if ( 0 >= (float) WC()->cart->get_total( 'edit' ) ) {
Expand Down Expand Up @@ -435,6 +441,8 @@ public function recurring_complete_checkout_session_update( $payload ) {

$payload['chargeAmount']['amount'] = WC_Amazon_Payments_Advanced::format_amount( $recurring_total );

$payload['chargeAmount'] = WC_Amazon_Payments_Advanced_API::format_charge_amount( $payload['chargeAmount'] );

return $payload;
}

Expand Down Expand Up @@ -545,16 +553,18 @@ public function scheduled_subscription_payment( $amount_to_charge, $order ) {

$currency = wc_apa_get_order_prop( $order, 'order_currency' );

$charge_amount = array(
'amount' => WC_Amazon_Payments_Advanced::format_amount( $amount_to_charge ),
'currencyCode' => $currency,
);

$response = WC_Amazon_Payments_Advanced_API::create_charge(
$charge_permission_id,
array(
'merchantMetadata' => WC_Amazon_Payments_Advanced_API::get_merchant_metadata( $order_id ),
'captureNow' => $capture_now,
'canHandlePendingAuthorization' => $can_do_async,
'chargeAmount' => array(
'amount' => WC_Amazon_Payments_Advanced::format_amount( $amount_to_charge ),
'currencyCode' => $currency,
),
'chargeAmount' => WC_Amazon_Payments_Advanced_API::format_charge_amount( $charge_amount ),
)
);

Expand Down
30 changes: 18 additions & 12 deletions includes/class-wc-gateway-amazon-payments-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -1422,16 +1422,18 @@ public function process_payment( $order_id ) {
$can_do_async = true;
}

$charge_amount = array(
'amount' => $order_total,
'currencyCode' => $currency,
);

$payload['paymentDetails'] = array_merge(
isset( $payload['paymentDetails'] ) && is_array( $payload['paymentDetails'] ) ? $payload['paymentDetails'] : array(),
array(
'paymentIntent' => $payment_intent,
'canHandlePendingAuthorization' => $can_do_async,
// "softDescriptor" => "Descriptor", // TODO: Implement setting, if empty, don't set this. ONLY FOR AuthorizeWithCapture
'chargeAmount' => array(
'amount' => $order_total,
'currencyCode' => $currency,
),
'chargeAmount' => WC_Amazon_Payments_Advanced_API::format_charge_amount( $charge_amount ),
)
);

Expand Down Expand Up @@ -1553,15 +1555,17 @@ public function handle_return( $checkout_session_id = '' ) {

$this->get_lock_for_order( $order_id, true );

$charge_amount = array(
'amount' => $order_total,
'currencyCode' => $currency,
);

$response = WC_Amazon_Payments_Advanced_API::complete_checkout_session(
$checkout_session_id,
apply_filters(
'woocommerce_amazon_pa_update_complete_checkout_session_payload',
array(
'chargeAmount' => array(
'amount' => $order_total,
'currencyCode' => $currency,
),
'chargeAmount' => WC_Amazon_Payments_Advanced_API::format_charge_amount( $charge_amount ),
),
$checkout_session_id,
$order
Expand Down Expand Up @@ -2465,16 +2469,18 @@ public function perform_authorization( $order, $capture_now = true, $id = null )

$currency = wc_apa_get_order_prop( $order, 'order_currency' );

$charge_amount = array(
'amount' => WC_Amazon_Payments_Advanced::format_amount( $order->get_total() ),
'currencyCode' => $currency,
);

$charge = WC_Amazon_Payments_Advanced_API::create_charge(
$id,
array(
'merchantMetadata' => WC_Amazon_Payments_Advanced_API::get_merchant_metadata( $order_id ),
'captureNow' => $capture_now,
'canHandlePendingAuthorization' => $can_do_async,
'chargeAmount' => array(
'amount' => WC_Amazon_Payments_Advanced::format_amount( $order->get_total() ),
'currencyCode' => $currency,
),
'chargeAmount' => WC_Amazon_Payments_Advanced_API::format_charge_amount( $charge_amount ),
)
);

Expand Down
2 changes: 1 addition & 1 deletion woocommerce-gateway-amazon-payments-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public static function get_site_description() {
public static function format_amount( $num, $decimals = null, $decimals_sep = '.', $thousands_sep = '' ) {
/* Amazon won't accept any decimals more than 2. */
$decimals = $decimals > 2 ? null : $decimals;
$decimals = $decimals ? $decimals : min( wc_get_price_decimals(), 2 );
$decimals = null !== $decimals ? $decimals : min( wc_get_price_decimals(), 2 );
return number_format( $num, $decimals, $decimals_sep, $thousands_sep );
}

Expand Down
Loading