register_store_api_callbacks(); } else { add_action( 'woocommerce_blocks_loaded', [ $this, 'register_store_api_callbacks' ] ); } } /** * Register every QR Pay gateway with the Checkout Block. * * @param \Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $registry Registry. */ public function register_block_payment_methods( $registry ): void { if ( ! function_exists( 'WC' ) || ! WC()->payment_gateways() ) { return; } foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) { if ( $gateway instanceof \AQRPFW\Gateway\AbstractQRPay ) { $registry->register( new \AQRPFW\Blocks\QRPayBlocks( $gateway ) ); } } } /** * Let the block checkout tell the server which payment method is selected, * so the processing fee is shown in cart totals before the order is placed. */ public function register_store_api_callbacks(): void { if ( ! function_exists( 'woocommerce_store_api_register_update_callback' ) ) { return; } woocommerce_store_api_register_update_callback( [ 'namespace' => 'aqrpfw', 'callback' => function ( $data ) { if ( ! WC()->session ) { return; } $method = isset( $data['payment_method'] ) ? sanitize_key( $data['payment_method'] ) : ''; // Only accept our own gateway IDs (or empty to clear). if ( '' !== $method && 0 !== strpos( $method, 'aqrpfw_' ) ) { return; } WC()->session->set( 'chosen_payment_method', $method ); }, ] ); } /** * Register payment gateways */ public function registerGateway( array $gateways ): array { $gateways[] = \AQRPFW\Gateway\bKash::class; $gateways[] = \AQRPFW\Gateway\Nagad::class; $gateways[] = \AQRPFW\Gateway\Upay::class; $gateways[] = \AQRPFW\Gateway\Rocket::class; // Optional international providers (safe) $gateways[] = \AQRPFW\Gateway\Wise::class; $gateways[] = \AQRPFW\Gateway\PayPal::class; $gateways[] = \AQRPFW\Gateway\GooglePay::class; $gateways[] = \AQRPFW\Gateway\ApplePay::class; $gateways[] = \AQRPFW\Gateway\Payoneer::class; $gateways[] = \AQRPFW\Gateway\NexusPay::class; $gateways[] = \AQRPFW\Gateway\Stripe::class; $gateways[] = \AQRPFW\Gateway\AmazonPay::class; $gateways[] = \AQRPFW\Gateway\MCash::class; $gateways[] = \AQRPFW\Gateway\SureCash::class; $gateways[] = \AQRPFW\Gateway\Klarna::class; $gateways[] = \AQRPFW\Gateway\Afterpay::class; $gateways[] = \AQRPFW\Gateway\Paytm::class; $gateways[] = \AQRPFW\Gateway\PhonePe::class; $gateways[] = \AQRPFW\Gateway\eSewa::class; $gateways[] = \AQRPFW\Gateway\Khalti::class; $gateways[] = \AQRPFW\Gateway\Fonepay::class; $gateways[] = \AQRPFW\Gateway\GCash::class; $gateways[] = \AQRPFW\Gateway\JazzCash::class; $gateways[] = \AQRPFW\Gateway\Easypaisa::class; $gateways[] = \AQRPFW\Gateway\Maya::class; $gateways[] = \AQRPFW\Gateway\Alipay::class; $gateways[] = \AQRPFW\Gateway\WeChatPay::class; $gateways[] = \AQRPFW\Gateway\MPesa::class; return $gateways; } /** * Resolve payer verification value + display label from order meta. * * Prefers new `_aqrpfw_payer_ref` meta; falls back to legacy `_aqrpfw_payer_last4`. * * @param \WC_Order $order Order object. * @return array{value:string,label:string} */ protected function get_payer_ref_display( $order ): array { $value = (string) $order->get_meta( '_aqrpfw_payer_ref' ); $type = (string) $order->get_meta( '_aqrpfw_payer_ref_type' ); $label = (string) $order->get_meta( '_aqrpfw_payer_ref_label' ); if ( '' === $value ) { $value = (string) $order->get_meta( '_aqrpfw_payer_last4' ); if ( $value && '' === $type ) { $type = 'phone_last4'; } } if ( '' === $label && $value ) { switch ( $type ) { case 'email': $label = __( 'Payer email', 'all-qr-pay-for-woocommerce' ); break; case 'account': $label = __( 'Payer account ID', 'all-qr-pay-for-woocommerce' ); break; case 'phone_last4': default: $label = __( 'Payer phone (last 4)', 'all-qr-pay-for-woocommerce' ); break; } } return [ 'value' => $value, 'label' => $label, ]; } /** * Frontend – Thank You page & My Account */ public function show_payment_details( $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $provider = $order->get_meta( '_aqrpfw_provider' ); $txn = $order->get_meta( '_aqrpfw_txn_id' ); $payer_ref = $this->get_payer_ref_display( $order ); if ( ! $provider && ! $txn && ! $payer_ref['value'] ) { return; } echo '
'; echo '

' . esc_html__( 'QR Payment Details', 'all-qr-pay-for-woocommerce' ) . '

'; if ( $provider ) { echo esc_html__( 'Provider:', 'all-qr-pay-for-woocommerce' ) . ' ' . esc_html( $provider ) . '
'; } if ( $txn ) { echo esc_html__( 'Transaction ID:', 'all-qr-pay-for-woocommerce' ) . ' ' . esc_html( $txn ) . '
'; } if ( $payer_ref['value'] ) { echo esc_html( $payer_ref['label'] ) . ': ' . esc_html( $payer_ref['value'] ) . ''; } echo '

'; } /** * Email output */ public function show_payment_details_email( $order, $sent_to_admin, $plain_text, $email ) { $provider = $order->get_meta( '_aqrpfw_provider' ); $txn = $order->get_meta( '_aqrpfw_txn_id' ); $payer_ref = $this->get_payer_ref_display( $order ); if ( ! $provider && ! $txn && ! $payer_ref['value'] ) { return; } if ( $plain_text ) { echo "\n" . esc_html__( 'QR Payment Details', 'all-qr-pay-for-woocommerce' ) . "\n"; if ( $provider ) { echo esc_html__( 'Provider:', 'all-qr-pay-for-woocommerce' ) . ' ' . esc_html( $provider ) . "\n"; } if ( $txn ) { echo esc_html__( 'Transaction ID:', 'all-qr-pay-for-woocommerce' ) . ' ' . esc_html( $txn ) . "\n"; } if ( $payer_ref['value'] ) { echo esc_html( $payer_ref['label'] ) . ': ' . esc_html( $payer_ref['value'] ) . "\n"; } return; } echo '

' . esc_html__( 'QR Payment Details', 'all-qr-pay-for-woocommerce' ) . '

'; echo ''; if ( $provider ) { echo ''; } if ( $txn ) { echo ''; } if ( $payer_ref['value'] ) { echo ''; } echo '
' . esc_html__( 'Provider', 'all-qr-pay-for-woocommerce' ) . '' . esc_html( $provider ) . '
' . esc_html__( 'Transaction ID', 'all-qr-pay-for-woocommerce' ) . '' . esc_html( $txn ) . '
' . esc_html( $payer_ref['label'] ) . '' . esc_html( $payer_ref['value'] ) . '
'; } /** * Frontend styles */ public function enqueueFrontendAssets(): void { self::register_frontend_style(); // Only needed on checkout, order-pay and order-received pages. if ( function_exists( 'is_checkout' ) && is_checkout() ) { wp_enqueue_style( 'aqrpfw-frontend' ); } } /** * Register (not enqueue) the frontend stylesheet. */ public static function register_frontend_style(): void { if ( ! wp_style_is( 'aqrpfw-frontend', 'registered' ) ) { wp_register_style( 'aqrpfw-frontend', AQRPFW_ASSETS . 'css/frontend.css', [], AQRPFW_VERSION ); } } /** * Admin order card (HPOS + legacy) */ public function render_admin_card_in_billing( $order ) { if ( ! $order instanceof \WC_Order ) { return; } $provider = $order->get_meta( '_aqrpfw_provider' ); $txn = $order->get_meta( '_aqrpfw_txn_id' ); $payer_ref = $this->get_payer_ref_display( $order ); $status = $order->get_status(); $status_label = wc_get_order_status_name( $status ); if ( ! $provider && ! $txn && ! $payer_ref['value'] ) { return; } // Badge text $status_text = ( 'on-hold' === $status ) ? sprintf( '%s (%s)', $status_label, __( 'manual verify', 'all-qr-pay-for-woocommerce' ) ) : $status_label; // Map statuses to colors switch ( $status ) { case 'pending': $status_class = 'aqrpfw-badge-gray'; break; case 'on-hold': $status_class = 'aqrpfw-badge-orange'; break; case 'processing': $status_class = 'aqrpfw-badge-blue'; break; case 'completed': $status_class = 'aqrpfw-badge-green'; break; case 'refunded': $status_class = 'aqrpfw-badge-purple'; break; case 'failed': $status_class = 'aqrpfw-badge-red'; break; default: $status_class = 'aqrpfw-badge-gray'; break; } // Provider logos $map = [ 'bKash' => ['img' => AQRPFW_ASSETS . 'img/bkash.png', 'class' => 'aqrpfw-badge-pink'], 'Nagad' => ['img' => AQRPFW_ASSETS . 'img/nagad.png', 'class' => 'aqrpfw-badge-orange'], 'Upay' => ['img' => AQRPFW_ASSETS . 'img/upay.png', 'class' => 'aqrpfw-badge-green'], 'Rocket' => ['img' => AQRPFW_ASSETS . 'img/rocket.png', 'class' => 'aqrpfw-badge-purple'], // All other logos default to blue ]; $img = isset( $map[ $provider ]['img'] ) ? $map[ $provider ]['img'] : ''; $badge = isset( $map[ $provider ]['class'] ) ? $map[ $provider ]['class'] : 'aqrpfw-badge-gray'; echo '
'; echo '
'; if ( $img ) { echo ''; } echo '
'; echo '' . esc_html( $provider ? $provider . ' QR Pay' : __( 'QR Pay', 'all-qr-pay-for-woocommerce' ) ) . ''; echo '' . esc_html( $status_text ) . ''; echo '
'; echo '
'; // Table echo ''; if ( $provider ) { echo ''; } if ( $txn ) { echo ''; } if ( $payer_ref['value'] ) { echo ''; } echo '
' . esc_html__( 'Provider', 'all-qr-pay-for-woocommerce' ) . '' . esc_html( $provider ) . '
' . esc_html__( 'Transaction ID', 'all-qr-pay-for-woocommerce' ) . '' . esc_html( $txn ) . '
' . esc_html( $payer_ref['label'] ) . '' . esc_html( $payer_ref['value'] ) . '
'; echo '

' . esc_html__( 'Verify in wallet dashboard, then mark order as Processing or Completed.', 'all-qr-pay-for-woocommerce' ) . '

'; echo '
'; } /** * Admin CSS */ public function enqueue_admin_assets( $hook ) { $screen = get_current_screen(); if ( empty( $screen->id ) ) { return; } // Legacy: shop_order // HPOS: screens containing wc-orders if ( 'shop_order' !== $screen->id && strpos( $screen->id, 'wc-orders' ) === false ) { return; } wp_enqueue_style( 'aqrpfw-admin', AQRPFW_ASSETS . 'css/admin.css', [], AQRPFW_VERSION ); } }