$is_test ? get_option( 'aimslms_razorpay_test_key_id' ) : get_option( 'aimslms_razorpay_live_key_id' ), 'secret' => $is_test ? get_option( 'aimslms_razorpay_test_key_secret' ) : get_option( 'aimslms_razorpay_live_key_secret' ), ); } /** * TEST CONNECTION (ADMIN) * Tests Razorpay 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 $key_id Razorpay key ID to test. * @param string $secret Razorpay key secret to test. * @return array|WP_Error Array with 'mode' on success, or WP_Error on failure. */ public static function test_connection( $mode, $key_id = '', $secret = '' ) { if ( empty( $key_id ) || empty( $secret ) ) { return new WP_Error( 'razorpay_credentials_missing', ucfirst( $mode ) . ' Razorpay credentials are missing', array( 'mode' => $mode ) ); } try { $api = new Api( $key_id, $secret ); // Lightweight auth check. $api->payment->all( array( 'count' => 1 ) ); update_option( 'aimslms_razorpay_connection_status_' . $mode, 'connected' ); return array( 'mode' => $mode ); } catch ( Exception $e ) { update_option( 'aimslms_razorpay_connection_status_' . $mode, 'not_connected' ); return new WP_Error( 'razorpay_auth_failed', $e->getMessage(), array( 'mode' => $mode ) ); } } /** * CREATE ORDER * Creates a Razorpay order for the given amount. * * @param float|int $amount Order amount, in the smallest currency unit expected by Razorpay. * @param string $currency Currency code (currently unused; order is always created in INR). * @param string $order_id Local order ID used to build the Razorpay receipt. * @param string $mode Either 'sandbox' or 'live'. * @return array Razorpay order data. * @throws Exception If the Razorpay keys for the given mode are missing. */ public static function create_order( $amount, $currency, $order_id, $mode ) { $keys = self::get_keys( $mode ); if ( empty( $keys['key_id'] ) || empty( $keys['secret'] ) ) { throw new Exception( 'Razorpay keys missing' ); } $api = new Api( $keys['key_id'], $keys['secret'] ); return $api->order->create( array( 'receipt' => 'order_' . $order_id, 'amount' => $amount, 'currency' => 'INR', 'payment_capture' => 1, ) ); } }