Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/wpbr-linepay-tw/includes/admin/meta-boxes/class-linepay-tw-order-meta-boxes.php
+++ b/wpbr-linepay-tw/includes/admin/meta-boxes/class-linepay-tw-order-meta-boxes.php
@@ -83,7 +83,11 @@
echo '<table>';
echo '<tr><th><div id="order-id" data-order-id="' . esc_html( $order->get_id() ) . '">' . esc_html__( 'Transaction ID', 'wpbr-linepay-tw' ) . '</div></th><td>' . esc_html( $order->get_meta( '_linepay_reserved_transaction_id' ) ) . '</td></tr>';
- echo '<tr><th><div>' . esc_html__( 'Payment Status', 'wpbr-linepay-tw' ) . '</div></th><td>' . esc_html( $order->get_meta( '_linepay_payment_status' ) ) . '</td></tr>';
+ $check_button = '';
+ if ( '' !== (string) $order->get_meta( '_linepay_reserved_transaction_id' ) ) {
+ $check_button = ' <button type="button" class="button button-small linepay-check-btn" data-id="' . esc_attr( $order->get_id() ) . '">' . esc_html__( 'Check Status', 'wpbr-linepay-tw' ) . '</button>';
+ }
+ echo '<tr><th><div>' . esc_html__( 'Payment Status', 'wpbr-linepay-tw' ) . '</div></th><td>' . esc_html( $order->get_meta( '_linepay_payment_status' ) ) . $check_button . '</td></tr>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $check_button is escaped above.
if ( $order->get_meta( '_linepay_payment_status' ) === WPBR_LINEPay_Const::PAYMENT_STATUS_AUTHED ) {
echo '<tr id="linepay-action"><th>' . esc_html__( 'Payment Action', 'wpbr-linepay-tw' ) . '</th><td><button class="button linepay-confirm-btn" data-id=' . esc_html( $order->get_id() ) . '>' . esc_html__( 'Confirm Payment', 'wpbr-linepay-tw' ) . '</button></tr>';
--- a/wpbr-linepay-tw/includes/class-wpbr-linepay-tw.php
+++ b/wpbr-linepay-tw/includes/class-wpbr-linepay-tw.php
@@ -138,7 +138,10 @@
add_action( 'admin_enqueue_scripts', array( self::get_instance(), 'linepay_tw_admin_scripts' ), 9 );
add_action( 'wp_ajax_linepay_confirm', array( self::get_instance(), 'linepay_tw_ajax_confirm_payment' ) );
+ add_action( 'wp_ajax_linepay_check', array( self::get_instance(), 'linepay_tw_ajax_check_payment' ) );
add_filter( 'plugin_action_links_' . WPBR_LINEPAY_BASENAME, array( self::get_instance(), 'linepay_tw_add_action_links' ) );
+
+ add_action( WPBR_LINEPay_Const::AS_HOOK_CONFIRM_RETRY, array( self::get_instance(), 'handle_confirm_retry' ), 10, 1 );
}
/**
@@ -158,7 +161,15 @@
wp_send_json( $return );
}
- $order_id = $posted['post_id'];
+ if ( ! current_user_can( 'edit_shop_orders' ) ) {
+ $return = array(
+ 'success' => false,
+ 'message' => __( 'You do not have permission to confirm this payment.', 'wpbr-linepay-tw' ),
+ );
+ wp_send_json( $return );
+ }
+
+ $order_id = isset( $posted['post_id'] ) ? absint( $posted['post_id'] ) : 0;
$order = wc_get_order( $order_id );
if ( ! $order ) {
@@ -175,6 +186,8 @@
try {
if ( $request->confirm( $order->get_id(), false ) ) {
+ // Cancel any pending retry actions for this order.
+ as_unschedule_all_actions( WPBR_LINEPay_Const::AS_HOOK_CONFIRM_RETRY, array( (int) $order->get_id() ), WPBR_LINEPay_Const::AS_GROUP_CONFIRM_RETRY );
$order->add_order_note( __( 'LINE Pay Confirm Succeed!', 'wpbr-linepay-tw' ) );
$return = array(
'success' => true,
@@ -194,6 +207,77 @@
}
}
/**
+ * Handle of check payment status request via wp-admin.
+ *
+ * Queries LINE Pay and syncs the payment status to the order. It never captures the payment.
+ *
+ * @return void
+ */
+ public function linepay_tw_ajax_check_payment() {
+
+ $posted = wp_unslash( $_POST );
+
+ if ( ! array_key_exists( 'security', $posted ) || ! wp_verify_nonce( $posted['security'], 'linepay-check' ) ) {
+ wp_send_json(
+ array(
+ 'success' => false,
+ 'message' => __( 'Unsecure AJAX call', 'wpbr-linepay-tw' ),
+ )
+ );
+ }
+
+ if ( ! current_user_can( 'edit_shop_orders' ) ) {
+ wp_send_json(
+ array(
+ 'success' => false,
+ 'message' => __( 'You do not have permission to check this payment.', 'wpbr-linepay-tw' ),
+ )
+ );
+ }
+
+ $order_id = isset( $posted['post_id'] ) ? absint( $posted['post_id'] ) : 0;
+ $order = wc_get_order( $order_id );
+
+ if ( ! $order || ! array_key_exists( $order->get_payment_method(), self::$allowed_payments ) ) {
+ wp_send_json(
+ array(
+ 'success' => false,
+ 'message' => __( 'No such order id', 'wpbr-linepay-tw' ),
+ )
+ );
+ }
+
+ if ( '' === (string) $order->get_meta( '_linepay_reserved_transaction_id' ) ) {
+ wp_send_json(
+ array(
+ 'success' => false,
+ 'message' => __( 'This order has no LINE Pay transaction ID.', 'wpbr-linepay-tw' ),
+ )
+ );
+ }
+
+ try {
+ $gateway = new LINEPay_TW_Payment();
+ $request = new LINEPay_TW_Request( $gateway );
+ $message = $request->sync_payment_status( $order );
+ wp_send_json(
+ array(
+ 'success' => true,
+ 'message' => $message,
+ )
+ );
+ } catch ( Exception $e ) {
+ self::log( sprintf( '[sync][order_id:%s] check payment status failed: %s', $order_id, $e->getMessage() ), 'error' );
+ wp_send_json(
+ array(
+ 'success' => false,
+ 'message' => $e->getMessage(),
+ )
+ );
+ }
+ }
+
+ /**
* Returns channel information that matches the environment information of LINEPay Gateway.
*
* @return array
@@ -229,7 +313,7 @@
*/
public function linepay_tw_admin_scripts() {
- wp_enqueue_script( 'wpbr-linepay-tw', WPBR_LINEPAY_PLUGIN_URL . 'assets/js/wpbr-linepay-tw-admin.js', array(), '1.0', true );
+ wp_enqueue_script( 'wpbr-linepay-tw', WPBR_LINEPAY_PLUGIN_URL . 'assets/js/wpbr-linepay-tw-admin.js', array(), WPBR_LINEPAY_VERSION, true );
wp_enqueue_style( 'wpbr-linepay-tw', WPBR_LINEPAY_PLUGIN_URL . 'assets/css/wpbr-linepay-tw-admin.css', array(), '1.0' );
wp_localize_script(
@@ -238,6 +322,7 @@
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'confirm_nonce' => wp_create_nonce( 'linepay-confirm' ),
+ 'check_nonce' => wp_create_nonce( 'linepay-check' ),
)
);
}
@@ -287,6 +372,154 @@
}
/**
+ * Handle confirm retry for an order.
+ *
+ * Uses chained single actions: on failure, schedules the next single
+ * action. On success or exhaustion, simply does not schedule — the
+ * chain stops naturally without needing to unschedule anything.
+ *
+ * @param int $order_id The order ID.
+ * @return void
+ */
+ public function handle_confirm_retry( $order_id ) {
+
+ $order = wc_get_order( $order_id );
+
+ if ( ! $order ) {
+ self::log( sprintf( '[confirm-retry][order_id:%s] Order not found, skipping.', $order_id ), 'warning' );
+ return;
+ }
+
+ $payment_status = $order->get_meta( '_linepay_payment_status' );
+ if ( WPBR_LINEPay_Const::PAYMENT_STATUS_AUTHED !== $payment_status ) {
+ self::log( sprintf( '[confirm-retry][order_id:%s] Payment status is "%s", skipping.', $order_id, $payment_status ) );
+ return;
+ }
+
+ $max_retries = apply_filters( 'linepay_tw_max_retry_count', WPBR_LINEPay_Const::DEFAULT_MAX_RETRY_COUNT, $order_id );
+ $retry_count = (int) $order->get_meta( WPBR_LINEPay_Const::META_CONFIRM_RETRY_COUNT );
+
+ if ( $retry_count >= $max_retries ) {
+ self::log( sprintf( '[confirm-retry][order_id:%s] Retries already exhausted (%d/%d), skipping.', $order_id, $retry_count, $max_retries ) );
+ return;
+ }
+
+ $retry_count++;
+ $order->update_meta_data( WPBR_LINEPay_Const::META_CONFIRM_RETRY_COUNT, $retry_count );
+ $order->save();
+
+ self::log( sprintf( '[confirm-retry][order_id:%s] Attempting confirm retry #%d/%d.', $order_id, $retry_count, $max_retries ) );
+
+ $gateway = new LINEPay_TW_Payment();
+ $request = new LINEPay_TW_Request( $gateway );
+
+ try {
+ if ( $request->confirm( $order_id, false ) ) {
+ /* translators: %d is the retry attempt number */
+ $order->add_order_note( sprintf( __( 'LINE Pay Confirm succeeded on auto-retry #%d.', 'wpbr-linepay-tw' ), $retry_count ) );
+ self::log( sprintf( '[confirm-retry][order_id:%s] Confirm succeeded on retry #%d.', $order_id, $retry_count ) );
+ // Success — do not schedule next action, chain stops.
+ return;
+ }
+ } catch ( Exception $e ) {
+ /* translators: %1$d is the retry attempt number, %2$s is the error message */
+ $order->add_order_note( sprintf( __( 'LINE Pay auto-retry #%1$d failed: %2$s', 'wpbr-linepay-tw' ), $retry_count, $e->getMessage() ) );
+ self::log( sprintf( '[confirm-retry][order_id:%s] Retry #%d failed: %s', $order_id, $retry_count, $e->getMessage() ), 'error' );
+ }
+
+ // Decide: schedule next retry or send notification.
+ if ( $retry_count >= $max_retries ) {
+ self::send_retry_failed_notification( $order );
+ /* translators: %d is the max retry count */
+ $order->add_order_note( sprintf( __( 'LINE Pay auto-retry exhausted after %d attempts. Admin has been notified.', 'wpbr-linepay-tw' ), $retry_count ) );
+ self::log( sprintf( '[confirm-retry][order_id:%s] All %d retries exhausted. Notification sent.', $order_id, $retry_count ), 'error' );
+ // Exhausted — do not schedule next action, chain stops.
+ } else {
+ // Schedule next single action.
+ self::schedule_confirm_retry( $order_id );
+ }
+ }
+
+ /**
+ * Schedule a single confirm retry action for an order.
+ *
+ * Uses chained single actions instead of a recurring action.
+ * Each callback schedules the next one if retries remain.
+ *
+ * @param int $order_id The order ID.
+ * @return void
+ */
+ public static function schedule_confirm_retry( $order_id ) {
+
+ $interval = apply_filters( 'linepay_tw_retry_interval', WPBR_LINEPay_Const::DEFAULT_RETRY_INTERVAL, $order_id );
+
+ as_schedule_single_action(
+ time() + ( $interval * MINUTE_IN_SECONDS ),
+ WPBR_LINEPay_Const::AS_HOOK_CONFIRM_RETRY,
+ array( (int) $order_id ),
+ WPBR_LINEPay_Const::AS_GROUP_CONFIRM_RETRY
+ );
+
+ self::log( sprintf( '[confirm-retry][order_id:%s] Scheduled next retry in %d minute(s).', $order_id, $interval ) );
+ }
+
+ /**
+ * Send email notification when all retries are exhausted.
+ *
+ * @param WC_Order $order The order object.
+ * @return void
+ */
+ private static function send_retry_failed_notification( $order ) {
+
+ $to = get_option( 'linepay_tw_retry_notification_email' );
+ if ( empty( $to ) ) {
+ $to = get_option( 'admin_email' );
+ }
+
+ $site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
+ $order_id = $order->get_id();
+ $transaction_id = $order->get_meta( '_linepay_reserved_transaction_id' );
+ $amount = $order->get_total();
+ $retry_count = (int) $order->get_meta( WPBR_LINEPay_Const::META_CONFIRM_RETRY_COUNT );
+ $order_url = admin_url( 'post.php?post=' . $order_id . '&action=edit' );
+
+ if ( function_exists( 'wc_get_container' ) ) {
+ try {
+ $order_url = wc_get_container()->get( AutomatticWooCommerceInternalAdminOrdersPageController::class )->get_edit_url( $order_id );
+ } catch ( Exception $e ) {
+ // fallback to default URL above.
+ }
+ }
+
+ $subject = sprintf(
+ /* translators: %1$s is the site name, %2$s is the order ID */
+ __( '[%1$s] LINE Pay Confirm Failed - Order #%2$s', 'wpbr-linepay-tw' ),
+ $site_name,
+ $order_id
+ );
+
+ $body = sprintf(
+ /* translators: %1$s is order ID, %2$s is transaction ID, %3$s is amount, %4$d is retry count, %5$s is order admin URL */
+ __(
+ "LINE Pay Confirm API has failed after all retry attempts.nn" .
+ "Order ID: #%1$sn" .
+ "Transaction ID: %2$sn" .
+ "Amount: %3$sn" .
+ "Retry Attempts: %4$dnn" .
+ "Please check the order and take manual action:n%5$s",
+ 'wpbr-linepay-tw'
+ ),
+ $order_id,
+ $transaction_id,
+ $amount,
+ $retry_count,
+ $order_url
+ );
+
+ wp_mail( $to, $subject, $body );
+ }
+
+ /**
* Log method.
*
* @param string $message The message to be logged.
@@ -303,6 +536,37 @@
}
/**
+ * Mask a LINE Pay transaction id for the log. Only the last 6 digits are kept.
+ *
+ * @param string|int $transaction_id The transaction id.
+ * @return string
+ */
+ public static function mask_transaction_id( $transaction_id ) {
+ $transaction_id = (string) $transaction_id;
+ if ( strlen( $transaction_id ) <= 6 ) {
+ return $transaction_id;
+ }
+ return '***' . substr( $transaction_id, -6 );
+ }
+
+ /**
+ * Mask the transaction id inside a LINE Pay API url, for the log.
+ * Covers /payments/{id}/..., /payments/requests/{id}/... and ?transactionId={id}.
+ *
+ * @param string $text The url, or a message that contains the url.
+ * @return string
+ */
+ public static function mask_transaction_id_in_url( $text ) {
+ return preg_replace_callback(
+ '#(/payments/(?:requests/)?|transactionId=)(d+)#',
+ function ( $matches ) {
+ return $matches[1] . self::mask_transaction_id( $matches[2] );
+ },
+ (string) $text
+ );
+ }
+
+ /**
* Constructor
*/
public function __construct() {
--- a/wpbr-linepay-tw/includes/gateways/class-linepay-tw-payment.php
+++ b/wpbr-linepay-tw/includes/gateways/class-linepay-tw-payment.php
@@ -215,12 +215,18 @@
return;
}
+ $message = '';
+
if ( $order->get_status() === 'on-hold' ) {
- echo esc_html__( 'We have received your order, but the payment status need to be confirmed. Please contact the support.', 'wpbr-linepay-tw' );
+ $message = __( 'We have received your order, but the payment status need to be confirmed. Please contact the support.', 'wpbr-linepay-tw' );
}
if ( $order->get_status() === 'pending' ) {
- echo esc_html__( 'We have received your order, but the order is awaiting payment. Please pay again.', 'wpbr-linepay-tw' ) . '</div>';
+ $message = __( 'We have received your order, but the order is awaiting payment. Please pay again.', 'wpbr-linepay-tw' );
+ }
+
+ if ( '' !== $message ) {
+ echo '<p class="woocommerce-info">' . esc_html( $message ) . '</p>';
}
}
}
--- a/wpbr-linepay-tw/includes/gateways/class-linepay-tw-request.php
+++ b/wpbr-linepay-tw/includes/gateways/class-linepay-tw-request.php
@@ -71,6 +71,7 @@
array(
'request_type' => WPBR_LINEPay_Const::REQUEST_TYPE_CONFIRM,
'order_id' => $order_id,
+ 'key' => $order->get_order_key(),
),
WC()->api_request_url( 'linepay_payment' )
)
@@ -81,6 +82,7 @@
array(
'request_type' => WPBR_LINEPay_Const::REQUEST_TYPE_CANCEL,
'order_id' => $order_id,
+ 'key' => $order->get_order_key(),
),
WC()->api_request_url( 'linepay_payment' )
)
@@ -105,10 +107,9 @@
$body = array_merge( $body, $product_info, $redirect_urls, $options );
$request_args = $this->build_execute_request_args( $url, $body );
- LINEPay_TW::log( sprintf( '[request][order_id:%s] execute request_args: %s', $order_id, wc_print_r( $request_args, true ) ) );
+ LINEPay_TW::log( sprintf( '[request][order_id:%s] execute request_args: %s', $order_id, wc_print_r( $this->get_loggable_request_args( $request_args, $order ), true ) ) );
$result = $this->execute( $url, $request_args );
- LINEPay_TW::log( '[request] execute result: ' . wc_print_r( $result, true ) );
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( '0000' !== $result->returnCode ) {
@@ -208,7 +209,7 @@
// api call.
$reserved_transaction_id = $order->get_meta( '_linepay_reserved_transaction_id' );
$url = $this->get_request_url( WPBR_LINEPay_Const::REQUEST_TYPE_CONFIRM, array( 'transaction_id' => $reserved_transaction_id ) );
- LINEPay_TW::log( sprintf( '[confirm][order_id:%s] http request url : %s', $order_id, $url ) );
+ LINEPay_TW::log( sprintf( '[confirm][order_id:%s] http request url : %s', $order_id, LINEPay_TW::mask_transaction_id_in_url( $url ) ) );
$body = array(
'amount' => $std_amount,
@@ -216,7 +217,7 @@
);
$request_args = $this->build_execute_request_args( $url, $body );
- LINEPay_TW::log( sprintf( '[confirm][order_id:%s] http_request request_args:', $order_id, wc_print_r( $request_args, true ) ) );
+ LINEPay_TW::log( sprintf( '[confirm][order_id:%s] http_request request_args: %s', $order_id, wc_print_r( $this->get_loggable_request_args( $request_args, $order ), true ) ) );
$this->check_payment_and_update_order_note( $order, 'Check payment status before confirm' );
@@ -314,6 +315,9 @@
$order->update_status( 'on-hold' );
$order->add_order_note( __( 'Order payment is authed, but need to be confirmed。', 'wpbr-linepay-tw' ) );
+ LINEPay_TW::schedule_confirm_retry( $order->get_id() );
+ $order->add_order_note( __( 'Auto-retry for confirm payment has been scheduled.', 'wpbr-linepay-tw' ) );
+
} elseif ( LINEPayStatusCode::CANCELLED_EXPIRED === $check_code ) {
$order->update_meta_data( '_linepay_payment_status', WPBR_LINEPay_Const::PAYMENT_STATUS_CANCELLED );
@@ -328,6 +332,12 @@
$order->update_status( LINEPay_TW::$fail_order_status );
$order->add_order_note( __( 'LINE Pay Transaction failed。', 'wpbr-linepay-tw' ) );
+ } elseif ( LINEPayStatusCode::UNAUTH === $check_code ) {
+
+ // Customer has not authorized the payment yet - keep the order pending so it can still be paid
+ // or be cancelled by WooCommerce's unpaid order timeout.
+ $order->add_order_note( __( 'LINE Pay payment is not authorized yet, order status remains unchanged.', 'wpbr-linepay-tw' ) );
+
} else {
$order->update_status( 'on-hold' );
$order->add_order_note( sprintf( __( 'Payment confirm failed, Status Code: %s。', 'wpbr-linepay-tw' ), $check_code ) );
@@ -361,7 +371,13 @@
// Initialize order stored in session.
WC()->session->set( 'order_awaiting_payment', false );
- LINEPay_TW::log( sprintf( WPBR_LINEPay_Const::LOG_TEMPLATE_PAYMENT_CANCEL, $order_id, $reserved_transaction_id ) );
+ LINEPay_TW::log( sprintf( WPBR_LINEPay_Const::LOG_TEMPLATE_PAYMENT_CANCEL, $order_id, LINEPay_TW::mask_transaction_id( $reserved_transaction_id ) ) );
+
+ // Mark the LINE Pay payment as cancelled so this reserved transaction can't be confirmed or cancelled again.
+ // The order itself stays pending; paying again calls request() and sets the status back to reserved.
+ $order->update_meta_data( '_linepay_payment_status', WPBR_LINEPay_Const::PAYMENT_STATUS_CANCELLED );
+ $order->save();
+ $order->add_order_note( __( 'LINE Pay payment was cancelled by the customer.', 'wpbr-linepay-tw' ) );
// redirect to thank you page, order status is pending.
wp_safe_redirect( $this->get_return_url( $order ) );
@@ -423,10 +439,10 @@
}
$url = $this->get_request_url( WPBR_LINEPay_Const::REQUEST_TYPE_CHECK, array( 'transaction_id' => $reserved_transaction_id ) );
- LINEPay_TW::log( sprintf( '[check][order_id:%s] http request url : %s', $order->get_id(), $url ) );
+ LINEPay_TW::log( sprintf( '[check][order_id:%s] http request url : %s', $order->get_id(), LINEPay_TW::mask_transaction_id_in_url( $url ) ) );
$request_args = $this->build_execute_request_args( $url, null, 20, 'GET' );
- LINEPay_TW::log( sprintf( '[check][order_id:%s] execute request_args: %s', $order->get_id(), wc_print_r( $request_args, true ) ) );
+ LINEPay_TW::log( sprintf( '[check][order_id:%s] execute request_args: %s', $order->get_id(), wc_print_r( $this->get_loggable_request_args( $request_args, $order ), true ) ) );
$check_result = $this->execute( $url, $request_args, 20 );
@@ -434,6 +450,104 @@
}
/**
+ * Query LINE Pay for the current payment status and sync it to the order.
+ *
+ * Used by the admin "Check Payment Status" action. It only syncs the status,
+ * it never calls the Confirm API, so no payment is captured here.
+ *
+ * @param WC_Order $order The order object.
+ * @return string The message describing the result.
+ *
+ * @throws Exception When the check request is failed.
+ */
+ public function sync_payment_status( $order ) {
+
+ $check_status = $this->check( $order );
+ $check_code = $check_status->returnCode;
+ $check_info = sprintf( '[sync][order_id:%s] Check payment status by admin, return code:%s, return message:%s', $order->get_id(), $check_code, $check_status->returnMessage );
+ LINEPay_TW::log( $check_info );
+ $order->add_order_note( $check_info );
+
+ // Only sync while the payment is still in progress. For a finished payment (confirmed, refunded,
+ // cancelled, failed) the check result is shown for reference only, so it never overwrites a
+ // refunded or cancelled status.
+ $payment_status = $order->get_meta( '_linepay_payment_status' );
+ if ( ! in_array( $payment_status, array( WPBR_LINEPay_Const::PAYMENT_STATUS_RESERVED, WPBR_LINEPay_Const::PAYMENT_STATUS_AUTHED ), true ) ) {
+ return sprintf(
+ /* translators: %1$s is the LINE Pay return code, %2$s is the LINE Pay return message */
+ __( 'LINE Pay status: %1$s (%2$s). The order is not changed because the payment is already finished.', 'wpbr-linepay-tw' ),
+ $check_code,
+ $check_status->returnMessage
+ );
+ }
+
+ // Only an order that is still waiting for payment may be moved to the fail status.
+ $awaiting_payment = $order->has_status( array( 'pending', 'on-hold' ) );
+
+ if ( LINEPayStatusCode::UNAUTH === $check_code ) {
+
+ $message = __( 'LINE Pay payment is not authorized yet, order status remains unchanged.', 'wpbr-linepay-tw' );
+
+ } elseif ( LINEPayStatusCode::AUTHED === $check_code ) {
+
+ $order->update_meta_data( '_linepay_payment_status', WPBR_LINEPay_Const::PAYMENT_STATUS_AUTHED );
+ $order->save();
+ $message = __( 'LINE Pay payment is authorized. You can now confirm the payment.', 'wpbr-linepay-tw' );
+
+ } elseif ( LINEPayStatusCode::CANCELLED_EXPIRED === $check_code ) {
+
+ $order->update_meta_data( '_linepay_payment_status', WPBR_LINEPay_Const::PAYMENT_STATUS_CANCELLED );
+ $order->save();
+ if ( $awaiting_payment ) {
+ $order->update_status( LINEPay_TW::$fail_order_status );
+ }
+ $message = __( 'Payment is cancelled or expired。', 'wpbr-linepay-tw' );
+
+ } elseif ( LINEPayStatusCode::FAILED === $check_code ) {
+
+ $order->update_meta_data( '_linepay_payment_status', WPBR_LINEPay_Const::PAYMENT_STATUS_FAILED );
+ $order->save();
+ if ( $awaiting_payment ) {
+ $order->update_status( LINEPay_TW::$fail_order_status );
+ }
+ $message = __( 'LINE Pay Transaction failed。', 'wpbr-linepay-tw' );
+
+ } elseif ( LINEPayStatusCode::COMPLETED === $check_code ) {
+
+ // Payment already captured on LINE Pay - record it on the order.
+ $url = $this->get_request_url( WPBR_LINEPay_Const::REQUEST_TYPE_DETAILS, array( 'transaction_id' => $order->get_meta( '_linepay_reserved_transaction_id' ) ) );
+ $request_args = $this->build_execute_request_args( $url, null, 20, 'GET' );
+ $payment_detail = $this->execute( $url, $request_args, 20 );
+ if ( ! $payment_detail || '0000' !== $payment_detail->returnCode || empty( $payment_detail->info ) ) {
+ throw new Exception( __( 'Unable to get the LINE Pay payment details.', 'wpbr-linepay-tw' ) );
+ }
+
+ // The Payment Details API returns a list of transactions.
+ $detail = is_array( $payment_detail->info ) ? reset( $payment_detail->info ) : $payment_detail->info;
+ $transaction_id = ! empty( $detail->transactionId ) ? $detail->transactionId : $order->get_meta( '_linepay_reserved_transaction_id' );
+ $paid_amount = 0;
+ if ( ! empty( $detail->payInfo ) && is_array( $detail->payInfo ) ) {
+ foreach ( $detail->payInfo as $pay_info ) {
+ $paid_amount += isset( $pay_info->amount ) ? $pay_info->amount : 0;
+ }
+ }
+
+ $order->update_meta_data( '_wpbr_linepay_confirmed', true );
+ $order->update_meta_data( '_linepay_payment_status', WPBR_LINEPay_Const::PAYMENT_STATUS_CONFIRMED );
+ $order->update_meta_data( '_linepay_transaction_balanced_amount', $paid_amount ? $paid_amount : $order->get_total() );
+ $order->save();
+ $order->payment_complete( $transaction_id );
+ $message = __( 'LINE Pay payment is already completed. The order has been marked as paid.', 'wpbr-linepay-tw' );
+
+ } else {
+ /* translators: %s is the LINE Pay return code */
+ $message = sprintf( __( 'Unexpected LINE Pay status code: %s. Order status remains unchanged.', 'wpbr-linepay-tw' ), $check_code );
+ }
+
+ return $message;
+ }
+
+ /**
* Build request arguments for LINE Pay API.
*
* @param string $url The request url.
@@ -479,6 +593,71 @@
}
/**
+ * Return a copy of the request arguments that is safe to write to the log.
+ * The headers carry the channel id and the request signature, so they are dropped.
+ * The order key in confirmUrl / cancelUrl is replaced with ***.
+ *
+ * @param array $request_args The request arguments from build_execute_request_args().
+ * @param WC_Order|null $order The order object, used to mask the order key.
+ * @return array
+ */
+ private function get_loggable_request_args( $request_args, $order = null ) {
+
+ unset( $request_args['headers'] );
+
+ if ( $order && isset( $request_args['body'] ) && is_string( $request_args['body'] ) ) {
+ $order_key = (string) $order->get_order_key();
+ if ( '' !== $order_key ) {
+ $request_args['body'] = str_replace( $order_key, '***', $request_args['body'] );
+ }
+ }
+
+ return $request_args;
+ }
+
+ /**
+ * Return the parts of a LINE Pay response that are safe to write to the log.
+ * Only the return code, return message and transaction ids are kept, and each transaction id
+ * shows only its last 6 digits. The payment url,
+ * payment access token, pay info (masked card number) and product details are dropped.
+ *
+ * @param mixed $response_body The decoded response body. It is null when the body is not valid JSON.
+ * @return array|string
+ */
+ private static function get_loggable_response( $response_body ) {
+
+ if ( ! is_object( $response_body ) ) {
+ return 'unable to decode response body';
+ }
+
+ $loggable = array(
+ 'returnCode' => isset( $response_body->returnCode ) ? $response_body->returnCode : '',
+ 'returnMessage' => isset( $response_body->returnMessage ) ? $response_body->returnMessage : '',
+ );
+
+ if ( empty( $response_body->info ) ) {
+ return $loggable;
+ }
+
+ // Most APIs return info as an object. The Payment Details API returns a list of transactions.
+ $infos = is_array( $response_body->info ) ? $response_body->info : array( $response_body->info );
+
+ foreach ( $infos as $info ) {
+ if ( ! is_object( $info ) ) {
+ continue;
+ }
+ if ( isset( $info->transactionId ) ) {
+ $loggable['transactionId'][] = LINEPay_TW::mask_transaction_id( $info->transactionId );
+ }
+ if ( isset( $info->refundTransactionId ) ) {
+ $loggable['refundTransactionId'][] = LINEPay_TW::mask_transaction_id( $info->refundTransactionId );
+ }
+ }
+
+ return $loggable;
+ }
+
+ /**
* Call the refund API and store the information DB according to the result.
* 1. Save refund information in the form of serialized array
* 2. After refund, the balance of the transaction amount is stored in string form.
@@ -501,7 +680,7 @@
);
$request_args = $this->build_execute_request_args( $url, $body );
- LINEPay_TW::log( sprintf( '[refund][order_id:%s] request_args:%s', $order_id, wc_print_r( $request_args, true ) ) );
+ LINEPay_TW::log( sprintf( '[refund][order_id:%s] request_args:%s', $order_id, wc_print_r( $this->get_loggable_request_args( $request_args, $order ), true ) ) );
try {
$resp = $this->execute( $url, $request_args );
@@ -522,8 +701,8 @@
$refund_ids = array();
}
- LINEPay_TW::log( sprintf( '[refund][order_id:%s] refund transaction ids:%s', $order_id, wc_print_r( $refund_ids, true ) ) );
$refund_ids[] = $resp->info->refundTransactionId;
+ LINEPay_TW::log( sprintf( '[refund][order_id:%s] refund transaction ids:%s', $order_id, wc_print_r( array_map( array( 'LINEPay_TW', 'mask_transaction_id' ), $refund_ids ), true ) ) );
$order->update_meta_data( '_linepay_refund_transaction_id', $refund_ids );
if ( ! $is_partial_refund ) {
@@ -559,17 +738,16 @@
$response = wp_remote_request( $url, $request_args );
if ( is_wp_error( $response ) ) {
- throw new Exception( 'Execute remote request error:' . $response->get_error_message() );
+ throw new Exception( 'Execute remote request error:' . LINEPay_TW::mask_transaction_id_in_url( $response->get_error_message() ) );
}
$http_status = (int) $response['response']['code'];
$response_body = self::json_custom_decode( wp_remote_retrieve_body( $response ) );
- $return_code = $response_body->returnCode;
- LINEPay_TW::log( '[execute] http response code: ' . $http_status . ', response body: ' . wc_print_r( $response_body, true ) );
+ LINEPay_TW::log( '[execute] http response code: ' . $http_status . ', response body: ' . wc_print_r( self::get_loggable_response( $response_body ), true ) );
if ( 200 !== $http_status ) {
- throw new Exception( sprintf( 'Execute API http response not success. http response code: %s. url: $s', $http_status, $url ) );
+ throw new Exception( sprintf( 'Execute API http response not success. http response code: %s. url: %s', $http_status, LINEPay_TW::mask_transaction_id_in_url( $url ) ) );
}
return $response_body;
--- a/wpbr-linepay-tw/includes/gateways/class-linepay-tw-response.php
+++ b/wpbr-linepay-tw/includes/gateways/class-linepay-tw-response.php
@@ -51,36 +51,87 @@
try {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
- $order_id = ( isset( $_GET['order_id'] ) ) ? sanitize_text_field( wp_unslash( $_GET['order_id'] ) ) : '';
+ // The callback is authenticated by the order key below, not by a nonce.
+ $order_id = isset( $_GET['order_id'] ) ? absint( wp_unslash( $_GET['order_id'] ) ) : 0;
+ $request_type = isset( $_GET['request_type'] ) ? sanitize_text_field( wp_unslash( $_GET['request_type'] ) ) : '';
+ $order_key = isset( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : '';
+ $transaction_id = isset( $_GET['transactionId'] ) ? sanitize_text_field( wp_unslash( $_GET['transactionId'] ) ) : '';
+ $received_params = implode(
+ ',',
+ array_map(
+ function ( $name ) {
+ return preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $name );
+ },
+ array_keys( $_GET )
+ )
+ );
+ // phpcs:enable WordPress.Security.NonceVerification.Recommended
+
+ // Record what LINE Pay sent back. Never log the order key value itself.
+ LINEPay_TW::log(
+ sprintf(
+ '[callback][order_id:%s] request_type: %s, params: %s, key: %s, transactionId: %s',
+ $order_id,
+ $request_type,
+ $received_params,
+ '' === $order_key ? 'missing' : 'present',
+ '' === $transaction_id ? 'missing' : LINEPay_TW::mask_transaction_id( $transaction_id )
+ )
+ );
- $order = wc_get_order( $order_id );
+ if ( ! in_array( $request_type, array( WPBR_LINEPay_Const::REQUEST_TYPE_CONFIRM, WPBR_LINEPay_Const::REQUEST_TYPE_CANCEL ), true ) ) {
+ throw new Exception( sprintf( 'invalid request type for order id: %s', $order_id ) );
+ }
+
+ $order = $order_id ? wc_get_order( $order_id ) : false;
- if ( ! $order ) {
+ if ( ! $order || WPBR_LINEPay_Const::ID !== $order->get_payment_method() ) {
throw new Exception( sprintf( WPBR_LINEPay_Const::LOG_TEMPLATE_HANDLE_CALLBANK_NOT_FOUND_ORDER_ID, $order_id, __( 'Unable to process callback.', 'wpbr-linepay-tw' ) ) );
}
- $request_type = ( isset( $_GET['request_type'] ) ) ? sanitize_text_field( wp_unslash( $_GET['request_type'] ) ) : '';
+ // Verify the request comes from the order owner (via LINE Pay redirect) before doing anything else.
+ if ( '' === $order_key || ! hash_equals( (string) $order->get_order_key(), $order_key ) ) {
+ throw new Exception( sprintf( 'order key mismatch for order id: %s', $order_id ) );
+ }
+
+ // LINE Pay always appends transactionId to confirmUrl and cancelUrl; it must match the reserved transaction.
+ if ( '' === $transaction_id || ! hash_equals( (string) $order->get_meta( '_linepay_reserved_transaction_id' ), $transaction_id ) ) {
+ throw new Exception( sprintf( 'transaction id mismatch for order id: %s', $order_id ) );
+ }
+
$payment_status = $order->get_meta( '_linepay_payment_status' );
+ if ( WPBR_LINEPay_Const::PAYMENT_STATUS_RESERVED !== $payment_status ) {
+ // The requester already proved they hold the order key, so sending them to the order page leaks nothing.
+ LINEPay_TW::log( sprintf( 'invalid status: %s to handle callback for order id: %s', $payment_status, $order_id ) );
+ wp_safe_redirect( $order->get_checkout_order_received_url() );
+ exit;
+ }
+
$gateway = new LINEPay_TW_Payment();
$request = new LINEPay_TW_Request( $gateway );
- if ( WPBR_LINEPay_Const::PAYMENT_STATUS_RESERVED === $payment_status ) {
-
- switch ( $request_type ) {
- case WPBR_LINEPay_Const::REQUEST_TYPE_CONFIRM:
- $request->confirm( $order_id );
- break;
- case WPBR_LINEPay_Const::REQUEST_TYPE_CANCEL:
- $request->cancel( $order_id );
- break;
- }
+ if ( WPBR_LINEPay_Const::REQUEST_TYPE_CONFIRM === $request_type ) {
+ $request->confirm( $order_id );
} else {
- LINEPay_TW::log( sprintf( 'invalid status: %s to handle callback for order id: %s', $payment_status, $order_id ) );
+ $request->cancel( $order_id );
}
} catch ( Exception $e ) {
- LINEPay_TW::log( 'receive_payment_response error: ' . $e->getMessage() );
+ LINEPay_TW::log( 'receive_payment_response rejected: ' . $e->getMessage() );
}
+
+ self::reject();
+ }
+
+ /**
+ * Send the same generic response for every rejected callback, so it neither leaks
+ * the order key nor reveals whether an order exists or what state it is in.
+ *
+ * @return void
+ */
+ private static function reject() {
+ wp_safe_redirect( wc_get_cart_url() );
+ exit;
}
/**
--- a/wpbr-linepay-tw/includes/settings/class-linepay-tw-settings-tab.php
+++ b/wpbr-linepay-tw/includes/settings/class-linepay-tw-settings-tab.php
@@ -88,6 +88,13 @@
'id' => 'linepay_tw_detail_status_note_enabled',
),
array(
+ 'title' => __( 'Retry Failed Notification Email', 'wpbr-linepay-tw' ),
+ 'type' => 'email',
+ 'desc' => __( 'Email address to receive notification when all confirm retries are exhausted. Defaults to admin email if empty.', 'wpbr-linepay-tw' ),
+ 'default' => '',
+ 'id' => 'linepay_tw_retry_notification_email',
+ ),
+ array(
'type' => 'sectionend',
'id' => 'linepay_tw_general_setting',
),
--- a/wpbr-linepay-tw/includes/utils/class-wpbr-linepay-const.php
+++ b/wpbr-linepay-tw/includes/utils/class-wpbr-linepay-const.php
@@ -30,7 +30,7 @@
const URI_CONFIRM = '/v3/payments/{transaction_id}/confirm';
const URI_DETAILS = '/v3/payments?transactionId={transaction_id}';
const URI_CHECK = '/v3/payments/requests/{transaction_id}/check';
- // const URI_DETAILS = '/v3/payments?orderId={order_id}';
+ // const URI_DETAILS = '/v2/payments?orderId={order_id}';
const URI_REFUND = '/v3/payments/{transaction_id}/refund';
const URI_CALLBACK_HANDLER = '/wc-api/linepay_payment';
@@ -74,4 +74,15 @@
const AUTH_ALGRO = 'sha256';
const REQUEST_TIME_FORMAT = 'YmdHis';
const CONFIRM_URLTYPE_CLIENT = 'CLIENT';
+
+ // Action Scheduler.
+ const AS_HOOK_CONFIRM_RETRY = 'linepay_tw_confirm_retry';
+ const AS_GROUP_CONFIRM_RETRY = 'linepay-tw-confirm-retry';
+
+ // Order meta keys for retry tracking.
+ const META_CONFIRM_RETRY_COUNT = '_linepay_confirm_retry_count';
+
+ // Retry defaults.
+ const DEFAULT_MAX_RETRY_COUNT = 3;
+ const DEFAULT_RETRY_INTERVAL = 1; // minutes
}
--- a/wpbr-linepay-tw/wpbr-linepay-tw.php
+++ b/wpbr-linepay-tw/wpbr-linepay-tw.php
@@ -5,7 +5,7 @@
* Description: Provides LINE Pay for your WooCommerce store
* Author: WPBrewer
* Author URI: https://wpbrewer.com
- * Version: 1.3.3
+ * Version: 1.4.0
* Requires Plugins: woocommerce
* Text Domain: wpbr-linepay-tw
* Domain Path: /languages
@@ -35,7 +35,7 @@
define( 'WPBR_LINEPAY_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'WPBR_LINEPAY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WPBR_LINEPAY_BASENAME', plugin_basename( __FILE__ ) );
-define( 'WPBR_LINEPAY_VERSION', '1.3.3' );
+define( 'WPBR_LINEPAY_VERSION', '1.4.0' );
/**
* Display a notice if WooCommerce is not installed and activated
@@ -114,3 +114,9 @@
}
add_action( 'plugins_loaded', 'run_wpbr_linepay' );
+
+register_deactivation_hook( __FILE__, function() {
+ if ( function_exists( 'as_unschedule_all_actions' ) ) {
+ as_unschedule_all_actions( 'linepay_tw_confirm_retry', null, 'linepay-tw-confirm-retry' );
+ }
+} );