Errors
8 years ago
api
2 months ago
cron
2 years ago
support
5 months ago
debug.php
4 years ago
plugin-instrumentation.php
3 months ago
razorpay-affordability-widget.php
4 weeks ago
razorpay-route-actions.php
2 years ago
razorpay-route.php
4 weeks ago
razorpay-webhook.php
5 months ago
state-map.php
4 years ago
utils.php
3 years ago
razorpay-route-actions.php
320 lines
| 1 | <?php |
| 2 | |
| 3 | use Razorpay\Api\Api; |
| 4 | use Razorpay\Api\Errors; |
| 5 | |
| 6 | require_once __DIR__ .'/razorpay-route.php'; |
| 7 | |
| 8 | class RZP_Route_Action |
| 9 | { |
| 10 | |
| 11 | public function __construct() |
| 12 | { |
| 13 | $this->Wc_Razorpay_Loader = new WC_Razorpay(); |
| 14 | } |
| 15 | |
| 16 | protected function fetchRazorpayApiInstance() |
| 17 | { |
| 18 | return $this->Wc_Razorpay_Loader->getRazorpayApiInstance(); |
| 19 | } |
| 20 | |
| 21 | public function redirect($pageUrl) |
| 22 | { |
| 23 | wp_redirect($pageUrl); |
| 24 | } |
| 25 | |
| 26 | public function authorizeAndAuthenticate($nonce, $action) |
| 27 | { |
| 28 | if(current_user_can('manage_woocommerce') === false) |
| 29 | { |
| 30 | rzpLogError("Authorization Failed"); |
| 31 | wp_die('<div class="error notice"> |
| 32 | <p>RAZORPAY ERROR: User is not Authorized to perform Operation</p> |
| 33 | </div>'); |
| 34 | } |
| 35 | |
| 36 | $verifyReq = wp_verify_nonce($nonce, $action); |
| 37 | |
| 38 | if ($verifyReq === false) |
| 39 | { |
| 40 | rzpLogError("nonce Authentication failed"); |
| 41 | wp_die('<div class="error notice"> |
| 42 | <p>RAZORPAY ERROR: Authentication Failed</p> |
| 43 | </div>'); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | function directTransfer() |
| 48 | { |
| 49 | $trfAccount = sanitize_text_field($_POST['drct_trf_account']); |
| 50 | $trfAmount = sanitize_text_field($_POST['drct_trf_amount']); |
| 51 | $nonce = sanitize_text_field($_POST['nonce']); |
| 52 | |
| 53 | $this->authorizeAndAuthenticate($nonce, 'rzp_direct_transfer'); |
| 54 | |
| 55 | $pageUrl = admin_url('admin.php?page=razorpayRouteWoocommerce'); |
| 56 | try { |
| 57 | $transferData = array( |
| 58 | |
| 59 | 'account' => $trfAccount, |
| 60 | 'amount' => (int)round($trfAmount * 100), |
| 61 | 'currency' => 'INR' |
| 62 | ); |
| 63 | |
| 64 | $this->api = $this->fetchRazorpayApiInstance(); |
| 65 | |
| 66 | $this->api->transfer->create($transferData); |
| 67 | } catch (Exception $e) { |
| 68 | $message = $e->getMessage(); |
| 69 | |
| 70 | wp_die('<div class="error notice"> |
| 71 | <p>RAZORPAY ERROR: Transfers create failed with the following message: ' . $message . '</p> |
| 72 | </div>'); |
| 73 | } |
| 74 | $this->redirect($pageUrl); |
| 75 | } |
| 76 | |
| 77 | function reverseTransfer() |
| 78 | { |
| 79 | $transferId = sanitize_text_field($_POST['transfer_id']); |
| 80 | $reversalAmount = sanitize_text_field($_POST['reversal_amount']); |
| 81 | $nonce = sanitize_text_field($_POST['nonce']); |
| 82 | |
| 83 | $this->authorizeAndAuthenticate($nonce, 'rzp_reverse_transfer'); |
| 84 | |
| 85 | $pageUrl = admin_url('admin.php?page=razorpayTransfers&id=' . $transferId); |
| 86 | try { |
| 87 | $reversalData = array( |
| 88 | 'amount' => (int)round($reversalAmount * 100), |
| 89 | ); |
| 90 | |
| 91 | $this->api = $this->fetchRazorpayApiInstance(); |
| 92 | |
| 93 | $this->api->transfer->fetch($transferId)->reverse($reversalData); |
| 94 | } catch (Exception $e) { |
| 95 | $message = $e->getMessage(); |
| 96 | |
| 97 | wp_die('<div class="error notice"> |
| 98 | <p>RAZORPAY ERROR: Reverse Transfer failed with the following message: ' . $message . '</p> |
| 99 | </div>'); |
| 100 | } |
| 101 | $this->redirect($pageUrl); |
| 102 | } |
| 103 | |
| 104 | function updateTransferSettlement() |
| 105 | { |
| 106 | $transferId = sanitize_text_field($_POST['transfer_id']); |
| 107 | $trfHoldStatus = sanitize_text_field($_POST['on_hold']); |
| 108 | $nonce = sanitize_text_field($_POST['nonce']); |
| 109 | |
| 110 | $this->authorizeAndAuthenticate($nonce, 'rzp_settlement_change'); |
| 111 | |
| 112 | if ($trfHoldStatus == "on_hold_until") { |
| 113 | $trfHoldUntil = sanitize_text_field($_POST['hold_until']); |
| 114 | $unixTime = strtotime($trfHoldUntil); |
| 115 | |
| 116 | $trfHoldStatus = true; |
| 117 | } |
| 118 | |
| 119 | $pageUrl = admin_url('admin.php?page=razorpayTransfers&id=' . $transferId); |
| 120 | try { |
| 121 | $updateData = array( |
| 122 | 'on_hold' => $trfHoldStatus, |
| 123 | 'on_hold_until' => $unixTime, |
| 124 | ); |
| 125 | |
| 126 | $url = "transfers/" . $transferId; |
| 127 | |
| 128 | $this->api = $this->fetchRazorpayApiInstance(); |
| 129 | |
| 130 | $this->api->request->request("PATCH", $url, $updateData); |
| 131 | |
| 132 | } catch (Exception $e) { |
| 133 | $message = $e->getMessage(); |
| 134 | |
| 135 | wp_die('<div class="error notice"> |
| 136 | <p>RAZORPAY ERROR: Change settlement schedule failed with the following message: ' . $message . '</p> |
| 137 | </div>'); |
| 138 | } |
| 139 | $this->redirect($pageUrl); |
| 140 | } |
| 141 | |
| 142 | function createPaymentTransfer() |
| 143 | { |
| 144 | $paymentId = sanitize_text_field($_POST['payment_id']); |
| 145 | $trfAccount = sanitize_text_field($_POST['pay_trf_account']); |
| 146 | $trfAmount = sanitize_text_field($_POST['pay_trf_amount']); |
| 147 | $nonce = sanitize_text_field($_POST['nonce']); |
| 148 | |
| 149 | $this->authorizeAndAuthenticate($nonce, 'rzp_payment_transfer'); |
| 150 | |
| 151 | $pageUrl = admin_url('admin.php?page=razorpayPaymentsView&id=' . $paymentId); |
| 152 | |
| 153 | $trfHoldStatus = sanitize_text_field($_POST['on_hold']); |
| 154 | if ($trfHoldStatus == "on_hold_until") { |
| 155 | $trfHoldUntil = sanitize_text_field($_POST['hold_until']); |
| 156 | $unixTime = strtotime($trfHoldUntil); |
| 157 | |
| 158 | $trfHoldStatus = true; |
| 159 | } |
| 160 | try { |
| 161 | |
| 162 | $data = array( |
| 163 | 'transfers' => array( |
| 164 | array( |
| 165 | 'account' => $trfAccount, |
| 166 | 'amount' => (int)round($trfAmount * 100), |
| 167 | 'currency' => 'INR', |
| 168 | 'on_hold' => $trfHoldStatus, |
| 169 | 'on_hold_until' => $unixTime,) |
| 170 | ) |
| 171 | ); |
| 172 | |
| 173 | $this->api = $this->fetchRazorpayApiInstance(); |
| 174 | |
| 175 | $this->api->payment->fetch($paymentId)->transfer($data); |
| 176 | |
| 177 | } catch (Exception $e) { |
| 178 | $message = $e->getMessage(); |
| 179 | |
| 180 | wp_die('<div class="error notice"> |
| 181 | <p>RAZORPAY ERROR: Transfers create failed with the following message: ' . $message . '</p> |
| 182 | </div>'); |
| 183 | } |
| 184 | $this->redirect($pageUrl); |
| 185 | } |
| 186 | |
| 187 | function getOrderTransferData($orderId){ |
| 188 | $order = wc_get_order($orderId); |
| 189 | |
| 190 | $items = $order->get_items(); |
| 191 | $orderTransferArr = array(); |
| 192 | |
| 193 | foreach ( $items as $item ) { |
| 194 | $productId = $item['product_id']; |
| 195 | $rzpTransferFrom = get_post_meta($productId, 'rzp_transfer_from', true); |
| 196 | |
| 197 | if($rzpTransferFrom == 'from_order'){ |
| 198 | |
| 199 | $LA_number_arr = get_post_meta($productId, 'LA_number', true); |
| 200 | $LA_amount_arr = get_post_meta($productId, 'LA_transfer_amount', true); |
| 201 | $LA_trf_status_arr = get_post_meta($productId, 'LA_transfer_status', true); |
| 202 | |
| 203 | if(isset($LA_number_arr) && is_array($LA_number_arr) && isset($LA_amount_arr) && is_array($LA_amount_arr)) { |
| 204 | $LA_transfer_count = count($LA_number_arr); |
| 205 | for($i=0;$i<$LA_transfer_count;$i++){ |
| 206 | if(!empty($LA_number_arr[$i]) && !empty($LA_amount_arr[$i])){ |
| 207 | $transferArr = array( |
| 208 | |
| 209 | 'account'=> $LA_number_arr[$i], |
| 210 | 'amount'=> (int) round($LA_amount_arr[$i] * 100), |
| 211 | 'currency'=> 'INR', |
| 212 | 'on_hold'=> $LA_trf_status_arr[$i] |
| 213 | ); |
| 214 | |
| 215 | array_push($orderTransferArr, $transferArr); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | return $orderTransferArr; |
| 224 | } |
| 225 | |
| 226 | function transferFromPayment($orderId, $razorpayPaymentId){ |
| 227 | |
| 228 | $order = wc_get_order($orderId); |
| 229 | |
| 230 | $items = $order->get_items(); |
| 231 | $paymentTransferArr = array(); |
| 232 | |
| 233 | foreach ( $items as $item ) { |
| 234 | $productId = $item['product_id']; |
| 235 | $rzp_transfer_from = get_post_meta($productId, 'rzp_transfer_from', true); |
| 236 | |
| 237 | if($rzp_transfer_from == 'from_payment'){ |
| 238 | |
| 239 | $LA_number_arr = get_post_meta($productId, 'LA_number', true); |
| 240 | $LA_amount_arr = get_post_meta($productId, 'LA_transfer_amount', true); |
| 241 | $LA_trf_status_arr = get_post_meta($productId, 'LA_transfer_status', true); |
| 242 | |
| 243 | if(isset($LA_number_arr) && is_array($LA_number_arr) && isset($LA_amount_arr) && is_array($LA_amount_arr)) { |
| 244 | $LA_transfer_count = count($LA_number_arr); |
| 245 | for($i=0;$i<$LA_transfer_count;$i++){ |
| 246 | if(!empty($LA_number_arr[$i]) && !empty($LA_amount_arr[$i])){ |
| 247 | $transferArr = array( |
| 248 | |
| 249 | 'account'=> $LA_number_arr[$i], |
| 250 | 'amount'=> (int) round($LA_amount_arr[$i] * 100), |
| 251 | 'currency'=> 'INR', |
| 252 | 'on_hold'=> $LA_trf_status_arr[$i] |
| 253 | ); |
| 254 | array_push($paymentTransferArr, $transferArr); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | } |
| 261 | |
| 262 | if(isset($paymentTransferArr) && !empty($paymentTransferArr)){ |
| 263 | |
| 264 | $data = array( |
| 265 | |
| 266 | 'transfers' => $paymentTransferArr |
| 267 | ); |
| 268 | |
| 269 | $url = "payments/".$razorpayPaymentId."/transfers"; |
| 270 | |
| 271 | $this->api = $this->fetchRazorpayApiInstance(); |
| 272 | |
| 273 | $this->api->request->request("POST", $url, $data); |
| 274 | |
| 275 | $this->addRouteAnalyticsScript(); |
| 276 | |
| 277 | } |
| 278 | |
| 279 | } |
| 280 | |
| 281 | function addRouteAnalyticsScript() { |
| 282 | |
| 283 | $mod_version = get_plugin_data(PLUGIN_DIR . '/woo-razorpay.php')['Version']; |
| 284 | $Wc_Razorpay_Loader = new WC_Razorpay(); |
| 285 | |
| 286 | $data = array( |
| 287 | 'key' => $Wc_Razorpay_Loader->getSetting('key_id'), |
| 288 | 'name' => get_bloginfo('name'), |
| 289 | '_' => array( |
| 290 | 'x-integration' => 'Woocommerce', |
| 291 | 'x-integration-module' => 'Route', |
| 292 | 'x-integration-version' => $mod_version, |
| 293 | 'x-integration-parent-version' => WOOCOMMERCE_VERSION, |
| 294 | ), |
| 295 | ); |
| 296 | |
| 297 | $Wc_Razorpay_Loader->enqueueCheckoutScripts('routeAnalyticsForm'); |
| 298 | |
| 299 | $url = Api::getFullUrl("checkout/embedded"); |
| 300 | |
| 301 | $formFields = ""; |
| 302 | foreach ($data as $fieldKey => $val) { |
| 303 | if(in_array($fieldKey, array('prefill', '_'))) |
| 304 | { |
| 305 | foreach ($data[$fieldKey] as $field => $fieldVal) { |
| 306 | $formFields .= "<input type='hidden' name='$fieldKey" ."[$field]"."' value='$fieldVal'> \n"; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return '<form method="POST" action="'.$url.'" id="routeAnalyticsForm"> |
| 312 | <input type="hidden" name="key_id" value="'.$data['key'].'"> |
| 313 | <input type="hidden" name="name" value="'.$data['name'].'"> |
| 314 | '. $formFields .' |
| 315 | </form>'; |
| 316 | |
| 317 | } |
| 318 | |
| 319 | } |
| 320 |