30, 'headers' => array( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Standard HTTP Basic Auth header encoding for PayPal OAuth, not obfuscation. 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret ), 'Content-Type' => 'application/x-www-form-urlencoded', ), 'body' => array( 'grant_type' => 'client_credentials', ), ) ); if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['access_token'] ) ) { return new WP_Error( 'paypal_auth_failed', $body['message'] ?? 'PayPal authentication failed' ); } return $body['access_token']; } /** * Creates a PayPal checkout order for the given amount. * * @param float|string $amount Order amount. * @param string $local_order_id Local order ID used as the PayPal reference/custom ID. * @return array PayPal order data on success, or an array with 'error' => true and * a 'message' (and 'raw' response) on failure. */ public static function create_order( $amount, $local_order_id = '' ) { $token = self::access_token(); if ( is_wp_error( $token ) ) { return array( 'error' => true, 'message' => $token->get_error_message(), ); } $amount = number_format( (float) $amount, 2, '.', '' ); $currency = aimslms_get_currency_code(); $checkout_url = get_permalink( get_option( 'aimslms_checkout_page' ) ); $response = wp_remote_post( self::api_base() . '/v2/checkout/orders', array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, 'PayPal-Request-Id' => uniqid( 'aimslms_', true ), // ✅ prevents duplicate issues ), 'body' => wp_json_encode( array( 'intent' => 'CAPTURE', 'application_context' => array( 'brand_name' => get_bloginfo( 'name' ), 'shipping_preference' => 'NO_SHIPPING', 'return_url' => add_query_arg( 'aimslms_action', 'paypal_success', $checkout_url ), 'cancel_url' => add_query_arg( 'aimslms_action', 'paypal_cancel', $checkout_url ), ), 'purchase_units' => array( array( 'reference_id' => 'ORDER_' . $local_order_id, 'custom_id' => (string) $local_order_id, 'amount' => array( 'currency_code' => $currency, 'value' => $amount, ), ), ), ) ), ) ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['name'] ) ) { return array( 'error' => true, 'message' => $body['message'] ?? 'PayPal order creation failed', 'raw' => $body, ); } return $body; } /** * Captures a previously created and approved PayPal order. * * @param string $order_id PayPal order ID to capture. * @return array Decoded PayPal capture response body. */ public static function capture_order( $order_id ) { $token = self::access_token(); $response = wp_remote_post( self::api_base() . "/v2/checkout/orders/{$order_id}/capture", array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), ) ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( isset( $body['name'] ) && defined( 'WP_DEBUG' ) && WP_DEBUG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r -- only logs when WP_DEBUG is enabled, to aid diagnosing a failed capture. error_log( 'PayPal Capture Error: ' . print_r( $body, true ) ); } return $body; } /** * Fetches the current status/details of a PayPal order. * * @param string $paypal_order_id PayPal order ID to look up. * @return array Decoded PayPal order data, or an empty array if no access token could be obtained. */ public static function get_order( $paypal_order_id ) { $token = self::access_token(); if ( ! $token ) { return array(); } $response = wp_remote_get( self::api_base() . "/v2/checkout/orders/{$paypal_order_id}", array( 'headers' => array( 'Authorization' => "Bearer {$token}", 'Content-Type' => 'application/json', ), ) ); return json_decode( wp_remote_retrieve_body( $response ), true ); } /** * Verifies an incoming PayPal webhook request's signature via PayPal's * verify-webhook-signature API. * * @param array $headers Request headers (case-insensitive keys expected as sent by PayPal). * @param string $raw_body Raw JSON request body, exactly as received. * @return bool True if PayPal confirms the signature is valid, false otherwise. */ public static function verify_webhook_signature( $headers, $raw_body ) { $webhook_id = get_option( 'aimslms_paypal_webhook_id' ); if ( empty( $webhook_id ) ) { return false; } $transmission_id = $headers['paypal-transmission-id'] ?? ''; $transmission_time = $headers['paypal-transmission-time'] ?? ''; $transmission_sig = $headers['paypal-transmission-sig'] ?? ''; $cert_url = $headers['paypal-cert-url'] ?? ''; $auth_algo = $headers['paypal-auth-algo'] ?? ''; if ( ! $transmission_id || ! $transmission_time || ! $transmission_sig || ! $cert_url || ! $auth_algo ) { return false; } $token = self::access_token(); if ( is_wp_error( $token ) ) { return false; } $event = json_decode( $raw_body, true ); if ( null === $event ) { return false; } $response = wp_remote_post( self::api_base() . '/v1/notifications/verify-webhook-signature', array( 'timeout' => 30, 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), 'body' => wp_json_encode( array( 'transmission_id' => $transmission_id, 'transmission_time' => $transmission_time, 'transmission_sig' => $transmission_sig, 'cert_url' => $cert_url, 'auth_algo' => $auth_algo, 'webhook_id' => $webhook_id, 'webhook_event' => $event, ) ), ) ); if ( is_wp_error( $response ) ) { return false; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); return isset( $body['verification_status'] ) && 'SUCCESS' === $body['verification_status']; } /** * Tests PayPal API connectivity for the given mode and credentials, and records the * connection status for that mode. * * @param string $mode Either 'sandbox' or 'live'. * @param string $client_id PayPal client ID to test. * @param string $secret PayPal client secret to test. * @return array|WP_Error Array with 'mode' on success, or WP_Error on failure. */ public static function test_connection( $mode, $client_id = '', $secret = '' ) { // ✅ force mode $is_sandbox = ( 'sandbox' === $mode ); if ( empty( $client_id ) || empty( $secret ) ) { return new WP_Error( 'paypal_credentials_missing', ucfirst( $mode ) . ' PayPal credentials are missing', array( 'mode' => $mode ) ); } $api_base = $is_sandbox ? 'https://api-m.sandbox.paypal.com' : 'https://api-m.paypal.com'; // ✅ real authentication test $response = wp_remote_post( $api_base . '/v1/oauth2/token', array( 'timeout' => 30, 'headers' => array( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Standard HTTP Basic Auth header encoding for PayPal OAuth, not obfuscation. 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret ), 'Content-Type' => 'application/x-www-form-urlencoded', ), 'body' => array( 'grant_type' => 'client_credentials', ), ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'paypal_request_failed', $response->get_error_message(), array( 'mode' => $mode ) ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['access_token'] ) ) { update_option( 'aimslms_paypal_connection_status_' . $mode, 'not_connected' ); return new WP_Error( 'paypal_auth_failed', $body['message'] ?? ucfirst( $mode ) . ' PayPal authentication failed', array( 'mode' => $mode ) ); } // ✅ save per-mode status update_option( 'aimslms_paypal_connection_status_' . $mode, 'connected' ); return array( 'mode' => $mode, ); } }