PluginProbe
MStore API – Create Native Android & iOS Apps On The Cloud / trunk
MStore API – Create Native Android & iOS Apps On The Cloud vtrunk
4.21.3 4.21.2 4.21.1 trunk 1.1.5 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 All 192 releases
mstore-api / controllers / flutter-paystack.php

flutter-paystack.php in MStore API – Create Native Android & iOS Apps On The Cloud trunk, at controllers/flutter-paystack.php

168 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6 require_once(__DIR__ . '/flutter-base.php');
7
8 /*
9 * Base REST Controller for flutter
10 *
11 * @since 1.4.0
12 *
13 * @package PayStack
14 */
15
16 class FlutterPayStack extends FlutterBaseController
17 {
18 /**
19 * Endpoint namespace
20 *
21 * @var string
22 */
23 protected $namespace = 'api/flutter_paystack';
24
25 /**
26 * Register all routes related with stores
27 *
28 * @return void
29 */
30 public function __construct()
31 {
32 add_action('rest_api_init', array($this, 'register_flutter_paystack_routes'));
33 }
34
35 public function register_flutter_paystack_routes()
36 {
37 register_rest_route($this->namespace, '/initialize_transaction', array(
38 array(
39 'methods' => "POST",
40 'callback' => array($this, 'initialize_transaction'),
41 'permission_callback' => function () {
42 return parent::checkApiPermission();
43 }
44 ),
45 ));
46
47 register_rest_route($this->namespace, '/verify_paystack_transaction', array(
48 array(
49 'methods' => "POST",
50 'callback' => array($this, 'verify_paystack_transaction'),
51 'permission_callback' => function () {
52 return parent::checkApiPermission();
53 }
54 ),
55 ));
56
57 }
58
59 public function initialize_transaction($request)
60 {
61 if (!is_plugin_active('woo-paystack/woo-paystack.php')) {
62 return parent::send_invalid_plugin_error("You need to install Paystack WooCommerce Payment Gateway plugin to use this api");
63 }
64 $json = file_get_contents('php://input');
65 $body = json_decode($json, TRUE);
66 $order_id = sanitize_text_field($body['order_id']);
67
68 $payStack = new WC_Gateway_Paystack();
69
70 $order = wc_get_order( $order_id );
71 if ( ! $order ) {
72 return new WP_Error('order_not_found', 'Order not found.', array('status' => 404));
73 }
74
75 // Unauthenticated route taking an order id from the caller: bind the request
76 // to the order before building a payment for it.
77 $key_check = mstore_api_check_payment_order_key($order, $body, true);
78 if (is_wp_error($key_check)) {
79 return $key_check;
80 }
81
82 $email = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_email() : $order->billing_email;
83 $amount = $order->get_total() * 100;
84 $txnref = $order_id . '_' . time();
85 $currency = method_exists( $order, 'get_currency' ) ? $order->get_currency() : $order->order_currency;
86 $callback_url = WC()->api_request_url( 'WC_Gateway_Paystack' );
87
88 $paystack_params = array(
89 'amount' => (int)$amount,
90 'email' => $email,
91 'currency' => $currency,
92 'reference' => $txnref,
93 'callback_url' => $callback_url,
94 );
95
96 if ( $payStack->split_payment ) {
97
98 $paystack_params['subaccount'] = $payStack->subaccount_code;
99 $paystack_params['bearer'] = $payStack->charges_account;
100
101 if ( empty( $payStack->transaction_charges ) ) {
102 $paystack_params['transaction_charge'] = '';
103 } else {
104 $paystack_params['transaction_charge'] = $payStack->transaction_charges * 100;
105 }
106 }
107
108 $paystack_params['metadata']['custom_fields'] = $payStack->get_custom_fields( $order_id );
109 $paystack_params['metadata']['cancel_action'] = wc_get_cart_url();
110
111 update_post_meta( $order_id, '_paystack_txn_ref', $txnref );
112
113 $paystack_url = 'https://api.paystack.co/transaction/initialize/';
114
115 $headers = array(
116 'Authorization' => 'Bearer ' . $payStack->secret_key,
117 'Content-Type' => 'application/json',
118 );
119
120 $args = array(
121 'headers' => $headers,
122 'timeout' => 60,
123 'body' => json_encode( $paystack_params ),
124 );
125
126 $request = wp_remote_post( $paystack_url, $args );
127 $paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
128
129 if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) {
130 return $paystack_response->data;
131 } else {
132 return parent::sendError("invalid_data", $paystack_response->message, 400);
133 }
134 }
135
136 public function verify_paystack_transaction($request)
137 {
138 if (!is_plugin_active('woo-paystack/woo-paystack.php')) {
139 return parent::send_invalid_plugin_error("You need to install Paystack WooCommerce Payment Gateway plugin to use this api");
140 }
141 $json = file_get_contents('php://input');
142 $body = json_decode($json, TRUE);
143 $_REQUEST['reference'] = sanitize_text_field($body['reference']);
144 $_REQUEST['paystack_txnref'] = sanitize_text_field($body['reference']);
145
146 if (defined('WC_ABSPATH')) {
147 // WC 3.6+ - Cart and other frontend functions are not included for REST requests.
148 include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
149 include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
150 include_once WC_ABSPATH . 'includes/wc-template-hooks.php';
151 }
152 if (null === WC()->session) {
153 $session_class = apply_filters('woocommerce_session_handler', 'WC_Session_Handler');
154
155 WC()->session = new $session_class();
156 WC()->session->init();
157 }
158 if (null === WC()->cart) {
159 WC()->cart = new WC_Cart();
160 }
161
162 $payStack = new WC_Gateway_Paystack();
163 return $payStack->verify_paystack_transaction();
164 }
165 }
166
167 new FlutterPayStack;
168