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