api.php
2 months ago
auth.php
5 months ago
cart.php
5 months ago
coupon-apply.php
5 months ago
coupon-get.php
5 months ago
giftcard-apply.php
11 months ago
order.php
7 months ago
prepay-cod.php
2 months ago
save-abandonment-data.php
11 months ago
shipping-info.php
11 months ago
api.php
292 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * custom APIs for Razorpay 1cc |
| 5 | */ |
| 6 | |
| 7 | require_once __DIR__ . '/../debug.php'; |
| 8 | require_once __DIR__ . '/../../woo-razorpay.php'; |
| 9 | require_once __DIR__ . '/shipping-info.php'; |
| 10 | require_once __DIR__ . '/coupon-apply.php'; |
| 11 | require_once __DIR__ . '/coupon-get.php'; |
| 12 | require_once __DIR__ . '/order.php'; |
| 13 | require_once __DIR__ . '/cart.php'; |
| 14 | require_once __DIR__ . '/auth.php'; |
| 15 | require_once __DIR__ . '/../state-map.php'; |
| 16 | require_once __DIR__ . '/save-abandonment-data.php'; |
| 17 | require_once __DIR__ . '/giftcard-apply.php'; |
| 18 | require_once __DIR__ . '/prepay-cod.php'; |
| 19 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 20 | |
| 21 | define('RZP_1CC_ROUTES_BASE', '1cc/v1'); |
| 22 | define('RZP_1CC_CART_HASH', 'wc_razorpay_cart_hash_'); |
| 23 | define('RZP_1CC_PLUGIN_FETCH', '1cc/merchant/woocommerce/plugins_list'); |
| 24 | |
| 25 | function rzp1ccInitRestApi() |
| 26 | { |
| 27 | |
| 28 | /** |
| 29 | * coupon APIs required |
| 30 | */ |
| 31 | |
| 32 | // returns applicable coupons for an order |
| 33 | register_rest_route( |
| 34 | RZP_1CC_ROUTES_BASE . '/coupon', |
| 35 | 'list', |
| 36 | array( |
| 37 | 'methods' => 'POST', |
| 38 | 'callback' => 'getCouponList', |
| 39 | 'permission_callback' => 'checkHmacSignature', |
| 40 | ) |
| 41 | ); |
| 42 | |
| 43 | // checks if a coupon can be applied and returns discount amount |
| 44 | register_rest_route( |
| 45 | RZP_1CC_ROUTES_BASE . '/coupon', |
| 46 | 'apply', |
| 47 | array( |
| 48 | 'methods' => 'POST', |
| 49 | 'callback' => 'applyCouponOnCart', |
| 50 | 'permission_callback' => 'checkAuthCredentials', |
| 51 | ) |
| 52 | ); |
| 53 | |
| 54 | /** |
| 55 | * order APIs |
| 56 | */ |
| 57 | |
| 58 | // create new wc order |
| 59 | register_rest_route( |
| 60 | RZP_1CC_ROUTES_BASE . '/order', |
| 61 | 'create', |
| 62 | array( |
| 63 | 'methods' => 'POST', |
| 64 | 'callback' => 'createWcOrder', |
| 65 | 'permission_callback' => 'checkAuthCredentials', |
| 66 | ) |
| 67 | ); |
| 68 | |
| 69 | /** |
| 70 | * shipping APIs |
| 71 | */ |
| 72 | |
| 73 | // list of shipping methods for an order |
| 74 | register_rest_route( |
| 75 | RZP_1CC_ROUTES_BASE . '/shipping', |
| 76 | 'shipping-info', |
| 77 | array( |
| 78 | 'methods' => 'POST', |
| 79 | 'callback' => 'calculateShipping1cc', |
| 80 | 'permission_callback' => 'checkAuthCredentials', |
| 81 | ) |
| 82 | ); |
| 83 | |
| 84 | // save abandoned cart data |
| 85 | register_rest_route( |
| 86 | RZP_1CC_ROUTES_BASE, |
| 87 | 'abandoned-cart', |
| 88 | array( |
| 89 | 'methods' => 'POST', |
| 90 | 'callback' => 'saveCartAbandonmentData', |
| 91 | 'permission_callback' => 'checkAuthCredentials', |
| 92 | ) |
| 93 | ); |
| 94 | |
| 95 | // cart data |
| 96 | register_rest_route( |
| 97 | RZP_1CC_ROUTES_BASE. '/cart', |
| 98 | 'fetch-cart', |
| 99 | array( |
| 100 | 'methods' => 'POST', |
| 101 | 'callback' => 'fetchCartData', |
| 102 | 'permission_callback' => 'checkAuthCredentials', |
| 103 | ) |
| 104 | ); |
| 105 | |
| 106 | register_rest_route( |
| 107 | RZP_1CC_ROUTES_BASE. '/cart', |
| 108 | 'create-cart', |
| 109 | array( |
| 110 | 'methods' => 'POST', |
| 111 | 'callback' => 'createCartData', |
| 112 | 'permission_callback' => 'checkAuthCredentials', |
| 113 | ) |
| 114 | ); |
| 115 | |
| 116 | /** |
| 117 | * Gift Card APIs |
| 118 | */ |
| 119 | |
| 120 | // validate gift card data |
| 121 | register_rest_route( |
| 122 | RZP_1CC_ROUTES_BASE.'/giftcard', |
| 123 | 'apply', |
| 124 | array( |
| 125 | 'methods' => 'POST', |
| 126 | 'callback' => 'validateGiftCardData', |
| 127 | 'permission_callback' => 'checkAuthCredentials', |
| 128 | ) |
| 129 | ); |
| 130 | |
| 131 | // prepay cod order |
| 132 | register_rest_route( |
| 133 | RZP_1CC_ROUTES_BASE.'/cod/order', |
| 134 | 'prepay', |
| 135 | array( |
| 136 | 'methods' => 'POST', |
| 137 | 'callback' => 'prepayCODOrder', |
| 138 | 'permission_callback' => 'checkHmacSignature', |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | add_action('rest_api_init', 'rzp1ccInitRestApi'); |
| 144 | |
| 145 | /** |
| 146 | * Check any prerequisites for our REST request |
| 147 | */ |
| 148 | function initCustomerSessionAndCart() |
| 149 | { |
| 150 | if (defined('WC_ABSPATH')) { |
| 151 | // WC 3.6+ - Cart and other frontend functions are not included for REST requests. |
| 152 | include_once WC_ABSPATH . 'includes/wc-notice-functions.php'; // nosemgrep: file-inclusion |
| 153 | include_once WC_ABSPATH . 'includes/wc-template-hooks.php'; // nosemgrep: file-inclusion |
| 154 | } |
| 155 | |
| 156 | initCartCommon(); |
| 157 | } |
| 158 | |
| 159 | function initCartCommon() |
| 160 | { |
| 161 | if (defined('WC_ABSPATH')) { |
| 162 | // WC 3.6+ - Cart and other frontend functions are not included for REST requests. |
| 163 | include_once WC_ABSPATH . 'includes/wc-cart-functions.php'; // nosemgrep: file-inclusion |
| 164 | } |
| 165 | |
| 166 | if (null === WC()->session) { |
| 167 | $session_class = apply_filters('woocommerce_session_handler', 'WC_Session_Handler'); |
| 168 | WC()->session = new $session_class(); |
| 169 | WC()->session->init(); |
| 170 | } |
| 171 | |
| 172 | if (null === WC()->customer) { |
| 173 | WC()->customer = new WC_Customer(get_current_user_id(), true); |
| 174 | } |
| 175 | |
| 176 | if (null === WC()->cart) { |
| 177 | WC()->cart = new WC_Cart(); |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
| 182 | add_action('setup_extra_setting_fields', 'addMagicCheckoutSettingFields'); |
| 183 | |
| 184 | function addMagicCheckoutSettingFields(&$defaultFormFields) |
| 185 | { |
| 186 | $magicCheckoutConfigFields = array( |
| 187 | |
| 188 | 'enable_1cc' => array( |
| 189 | 'title' => __('Activate Magic Checkout'), |
| 190 | 'type' => 'checkbox', |
| 191 | 'description' => "", |
| 192 | 'label' => __('Activate Magic Checkout'), |
| 193 | 'default' => 'no', |
| 194 | ), |
| 195 | 'enable_1cc_test_mode' => array( |
| 196 | 'title' => __('Activate test mode'), |
| 197 | 'type' => 'checkbox', |
| 198 | 'description' => 'When test mode is active, only logged-in admin users will see the Razorpay Magic Checkout button', |
| 199 | 'label' => __('Activate test mode for Magic Checkout'), |
| 200 | 'default' => 'no', |
| 201 | ), |
| 202 | 'enable_1cc_pdp_checkout' => array( |
| 203 | 'title' => __('Activate Buy Now Button'), |
| 204 | 'type' => 'checkbox', |
| 205 | 'description' => 'By enabling the Buy Now button, user will be able to see the Razorpay Magic Checkout button on Product display page. ', |
| 206 | 'label' => __('Activate Buy Now for Magic Checkout'), |
| 207 | 'default' => 'yes', |
| 208 | ), |
| 209 | 'enable_1cc_mini_cart_checkout' => array( |
| 210 | 'title' => __('Activate Mini Cart Checkout'), |
| 211 | 'type' => 'checkbox', |
| 212 | 'description' => 'By enabling the Mini Cart checkout button, user will be able to see the Razorpay Magic Checkout on click of checkout button. ', |
| 213 | 'label' => __('Activate Mini Cart for Magic Checkout'), |
| 214 | 'default' => 'yes', |
| 215 | ), |
| 216 | '1cc_min_cart_amount' => array( |
| 217 | 'title' => __('Set minimum cart amount (INR)'), |
| 218 | 'type' => 'number', |
| 219 | 'description' => 'Enter a minimum cart amount required to place an order via Magic Checkout.', |
| 220 | 'default' => 0, |
| 221 | 'css' => 'width: 120px;', |
| 222 | 'custom_attributes' => array( |
| 223 | 'min' => 0, |
| 224 | 'step' => 1, |
| 225 | ), |
| 226 | ), |
| 227 | '1cc_min_COD_slab_amount' => array( |
| 228 | 'title' => __('Set minimum amount (INR) for COD'), |
| 229 | 'type' => 'number', |
| 230 | 'description' => 'Enter a minimum amount required to place an order via COD (if enabled)', |
| 231 | 'default' => 0, |
| 232 | 'css' => 'width: 120px;', |
| 233 | 'custom_attributes' => array( |
| 234 | 'min' => 0, |
| 235 | 'step' => 1, |
| 236 | ), |
| 237 | ), |
| 238 | '1cc_max_COD_slab_amount' => array( |
| 239 | 'title' => __('Set maximum amount (INR) for COD'), |
| 240 | 'type' => 'number', |
| 241 | 'description' => 'Enter a maximum amount allowed to place an order via COD (if enabled)', |
| 242 | 'default' => 0, |
| 243 | 'css' => 'width: 120px;', |
| 244 | 'custom_attributes' => array( |
| 245 | 'min' => 0, |
| 246 | 'step' => 1, |
| 247 | ), |
| 248 | ), |
| 249 | 'enable_1cc_ga_analytics' => array( |
| 250 | 'title' => __('Activate Google Analytics'), |
| 251 | 'type' => 'checkbox', |
| 252 | 'description' => "To track orders using Google Analytics", |
| 253 | 'label' => __('Activate Magic Checkout Google Analytics'), |
| 254 | 'default' => 'no', |
| 255 | ), |
| 256 | 'enable_1cc_fb_analytics' => array( |
| 257 | 'title' => __('Activate Facebook Analytics'), |
| 258 | 'type' => 'checkbox', |
| 259 | 'description' => "To track orders using Facebook Pixel", |
| 260 | 'label' => __('Activate Magic Checkout Facebook Analytics'), |
| 261 | 'default' => 'no', |
| 262 | ), |
| 263 | '1cc_account_creation' => array( |
| 264 | 'title' => __('Allow customers to create store Account'), |
| 265 | 'type' => 'checkbox', |
| 266 | 'description' => 'Allow customers to create store Account', |
| 267 | 'label' => __('Allow customers to create store Account'), |
| 268 | 'default' => 'No', |
| 269 | ), |
| 270 | ); |
| 271 | |
| 272 | $defaultFormFields = array_merge($defaultFormFields, $magicCheckoutConfigFields); |
| 273 | |
| 274 | } |
| 275 | |
| 276 | //To handle rest cookies invalid issue |
| 277 | add_filter("nonce_user_logged_out", function ($uid, $action) { |
| 278 | if ($uid === 0 && $action === 'createWcOrder') { |
| 279 | return null; |
| 280 | } |
| 281 | return $uid; |
| 282 | }, 10, 2); |
| 283 | |
| 284 | add_filter('rest_authentication_errors', function ($maybe_error) { |
| 285 | $action = 'createWcOrder'; |
| 286 | if (doing_action($action)) { |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | return $maybe_error; |
| 291 | }); |
| 292 |