Skip to content

Commit

Permalink
Calculate test mode match in PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinksi committed Jan 5, 2024
1 parent d8da2fc commit f570657
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
15 changes: 7 additions & 8 deletions client/order/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ jQuery( function ( $ ) {
const disableManualRefunds = getConfig( 'disableManualRefunds' ) ?? false;
const manualRefundsTip = getConfig( 'manualRefundsTip' ) ?? '';
const chargeId = getConfig( 'chargeId' );
const orderTestMode = getConfig( 'testMode' );
const testMode = getConfig( 'testMode' );
// Order and site are both in test mode, or both in live mode.
// '1' = true, '' = false, null = the order was created before the test mode meta was added, so we assume it matches.
const orderTestModeMatch = getConfig( 'orderTestModeMatch' ) !== '';

maybeShowOrderNotices();

Expand Down Expand Up @@ -175,20 +178,16 @@ jQuery( function ( $ ) {
'#wcpay-order-payment-details-container'
);

// Check if the order's test mode meta matches the site's current test mode setting.
// E.g. order and site are both in test mode, or both in live mode.
const isOrderTestModeMatch = orderTestMode === wcpaySettings.testMode;

// If the container doesn't exist (WC < 7.9) don't render the notice.
// If the container doesn't exist (WC < 7.9) don't render notices.
if ( ! container ) {
return;
}

ReactDOM.render(
<>
{ orderTestMode && <TestModeNotice /> }
{ testMode && <TestModeNotice /> }

{ isOrderTestModeMatch && chargeId && (
{ chargeId && orderTestModeMatch && (
<DisputedOrderNoticeHandler
chargeId={ chargeId }
onDisableOrderRefund={ disableWooOrderRefundButton }
Expand Down
18 changes: 18 additions & 0 deletions includes/admin/class-wc-payments-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,23 @@ public function enqueue_payments_scripts() {

if ( $order && WC_Payment_Gateway_WCPay::GATEWAY_ID === $order->get_payment_method() ) {
$refund_amount = $order->get_remaining_refund_amount();

// Check if the order's test mode meta matches the site's current test mode state.
// E.g. order and site are both in test mode, or both in live mode.
$order_mode = $order->get_meta( WC_Payments_Order_Service::WCPAY_MODE_META_KEY );
if ( '' === $order_mode ) {
// If the order doesn't have a mode set, assume it was created before the order mode meta was added and return null.
$order_test_mode_match = null;
} else {
$order_test_mode_match = (
\WCPay\Constants\Order_Mode::PRODUCTION === $order_mode &&
WC_Payments::mode()->is_live()
) || (
\WCPay\Constants\Order_Mode::TEST === $order_mode &&
WC_Payments::mode()->is_test()
);
}

wp_localize_script(
'WCPAY_ADMIN_ORDER_ACTIONS',
'wcpay_order_config',
Expand All @@ -736,6 +753,7 @@ public function enqueue_payments_scripts() {
'chargeId' => $this->order_service->get_charge_id_for_order( $order ),
'hasOpenAuthorization' => $this->order_service->has_open_authorization( $order ),
'testMode' => \WCPay\Constants\Order_Mode::TEST === $order->get_meta( WC_Payments_Order_Service::WCPAY_MODE_META_KEY ),
'orderTestModeMatch' => $order_test_mode_match,
]
);
wp_localize_script(
Expand Down

0 comments on commit f570657

Please sign in to comment.