namespace, '/initialize_transaction', [ [ 'methods' => 'POST', 'callback' => [ $this, 'initialize_transaction' ], 'permission_callback' => function () { return parent::checkApiPermission(); }, ], ] ); register_rest_route( $this->namespace, '/verify_paystack_transaction', [ [ 'methods' => 'POST', 'callback' => [ $this, 'verify_paystack_transaction' ], 'permission_callback' => function () { return parent::checkApiPermission(); }, ], ] ); } public function initialize_transaction( $request ) { if ( ! is_plugin_active( 'woo-paystack/woo-paystack.php' ) ) { return parent::sendError( 'invalid_plugin', 'You need to install Paystack WooCommerce Payment Gateway plugin to use this api', 404 ); } $json = file_get_contents( 'php://input' ); $body = json_decode( $json, true ); $order_id = sanitize_text_field( $body['order_id'] ); $payStack = new WC_Gateway_Paystack(); $order = wc_get_order( $order_id ); $email = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_email() : $order->billing_email; $amount = $order->get_total() * 100; $txnref = $order_id . '_' . time(); $currency = method_exists( $order, 'get_currency' ) ? $order->get_currency() : $order->order_currency; $callback_url = WC()->api_request_url( 'WC_Gateway_Paystack' ); $paystack_params = [ 'amount' => $amount, 'email' => $email, 'currency' => $currency, 'reference' => $txnref, 'callback_url' => $callback_url, ]; if ( $payStack->split_payment ) { $paystack_params['subaccount'] = $payStack->subaccount_code; $paystack_params['bearer'] = $payStack->charges_account; if ( empty( $payStack->transaction_charges ) ) { $paystack_params['transaction_charge'] = ''; } else { $paystack_params['transaction_charge'] = $payStack->transaction_charges * 100; } } $paystack_params['metadata']['custom_fields'] = $payStack->get_custom_fields( $order_id ); $paystack_params['metadata']['cancel_action'] = wc_get_cart_url(); update_post_meta( $order_id, '_paystack_txn_ref', $txnref ); $paystack_url = 'https://api.paystack.co/transaction/initialize/'; $headers = [ 'Authorization' => 'Bearer ' . $payStack->secret_key, 'Content-Type' => 'application/json', ]; $args = [ 'headers' => $headers, 'timeout' => 60, 'body' => json_encode( $paystack_params ), ]; $request = wp_remote_post( $paystack_url, $args ); $paystack_response = json_decode( wp_remote_retrieve_body( $request ) ); if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) { return $paystack_response->data; } else { return parent::sendError( 'invalid_data', $paystack_response->message, 400 ); } } public function verify_paystack_transaction( $request ) { if ( ! is_plugin_active( 'woo-paystack/woo-paystack.php' ) ) { return parent::sendError( 'invalid_plugin', 'You need to install Paystack WooCommerce Payment Gateway plugin to use this api', 404 ); } $json = file_get_contents( 'php://input' ); $body = json_decode( $json, true ); $_REQUEST['reference'] = sanitize_text_field( $body['reference'] ); if ( defined( 'WC_ABSPATH' ) ) { // WC 3.6+ - Cart and other frontend functions are not included for REST requests. include_once WC_ABSPATH . 'includes/wc-cart-functions.php'; include_once WC_ABSPATH . 'includes/wc-notice-functions.php'; include_once WC_ABSPATH . 'includes/wc-template-hooks.php'; } if ( null === WC()->session ) { $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' ); WC()->session = new $session_class(); WC()->session->init(); } if ( null === WC()->cart ) { WC()->cart = new WC_Cart(); } $payStack = new WC_Gateway_Paystack(); return $payStack->verify_paystack_transaction(); } } new FlutterPayStack();