| 1 |
<?php |
| 2 |
|
| 3 |
require_once __DIR__.'/../woocommerce-cashfree.php'; |
| 4 |
|
| 5 |
class Cashfree_Response |
| 6 |
{ |
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
$this->cashfree = new WC_Gateway_cashfree(false); |
| 10 |
|
| 11 |
$this->msg['message'] = ""; |
| 12 |
|
| 13 |
$this->msg['class'] = ""; |
| 14 |
} |
| 15 |
|
| 16 |
function process($postArgs) |
| 17 |
{ |
| 18 |
global $woocommerce, $wpdb; |
| 19 |
|
| 20 |
$orderId = sanitize_text_field($postArgs["orderId"]); |
| 21 |
list($orderId) = explode('_', $orderId); |
| 22 |
$order = new WC_Order($orderId); |
| 23 |
|
| 24 |
if ($order->has_status( array( 'processing', 'completed' ) ) ) |
| 25 |
{ |
| 26 |
$this->redirectUser($order); |
| 27 |
} |
| 28 |
|
| 29 |
$success = false; |
| 30 |
|
| 31 |
if ($order && !empty($postArgs['referenceId'])) |
| 32 |
{ |
| 33 |
|
| 34 |
if ($postArgs["txStatus"] == 'SUCCESS') |
| 35 |
{ |
| 36 |
if($postArgs["orderAmount"] == $order->get_total()) |
| 37 |
{ |
| 38 |
$success = $this->verifySignature($postArgs); |
| 39 |
} |
| 40 |
else |
| 41 |
{ |
| 42 |
$postArgs["txMsg"] = "Order amount is mismatched"; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
if($success == true){ |
| 47 |
|
| 48 |
$this->updateOrder($order, $success, $postArgs["txMsg"], $postArgs['referenceId']); |
| 49 |
|
| 50 |
$this->redirectUser($order); |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
$this->updateOrder($order, $success, $postArgs['txMsg'], null); |
| 55 |
|
| 56 |
wp_redirect(wc_get_checkout_url()); |
| 57 |
exit; |
| 58 |
} |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
else |
| 63 |
{ |
| 64 |
$this->updateOrder($order, $success, $postArgs['txMsg'], null); |
| 65 |
|
| 66 |
wp_redirect(wc_get_checkout_url()); |
| 67 |
exit; |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
protected function redirectUser($order) |
| 73 |
{ |
| 74 |
$redirectUrl = $this->cashfree->get_return_url($order); |
| 75 |
|
| 76 |
wp_redirect($redirectUrl); |
| 77 |
exit; |
| 78 |
} |
| 79 |
|
| 80 |
protected function verifySignature($postArgs) |
| 81 |
{ |
| 82 |
$cashfree_response = array(); |
| 83 |
$cashfree_response["orderId"] = $postArgs['orderId']; |
| 84 |
$cashfree_response["orderAmount"] = sanitize_text_field($postArgs["orderAmount"]); |
| 85 |
$cashfree_response["txStatus"] = sanitize_text_field($postArgs["txStatus"]); |
| 86 |
$cashfree_response["referenceId"] = sanitize_text_field($postArgs["referenceId"]); |
| 87 |
$cashfree_response["txTime"] = sanitize_text_field($postArgs["txTime"]); |
| 88 |
$cashfree_response["txMsg"] = sanitize_text_field($postArgs["txMsg"]); |
| 89 |
$cashfree_response["paymentMode"] = sanitize_text_field($postArgs["paymentMode"]); |
| 90 |
$cashfree_response["signature"] = sanitize_text_field($postArgs["signature"]); |
| 91 |
|
| 92 |
$secret_key = $this->cashfree->settings['secret_key']; |
| 93 |
$data = "{$cashfree_response['orderId']}{$cashfree_response['orderAmount']}{$cashfree_response['referenceId']}{$cashfree_response['txStatus']}{$cashfree_response['paymentMode']}{$cashfree_response['txMsg']}{$cashfree_response['txTime']}"; |
| 94 |
$hash_hmac = hash_hmac('sha256', $data, $secret_key, true) ; |
| 95 |
$computedSignature = base64_encode($hash_hmac); |
| 96 |
if ($cashfree_response["signature"] != $computedSignature) |
| 97 |
{ |
| 98 |
return false; |
| 99 |
} |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Modifies existing order and handles success case |
| 105 |
* |
| 106 |
* @param $success, & $order |
| 107 |
*/ |
| 108 |
public function updateOrder(& $order, $success, $txMsg, $referenceId = null) |
| 109 |
{ |
| 110 |
global $woocommerce; |
| 111 |
|
| 112 |
if ($success === true) |
| 113 |
{ |
| 114 |
$order->payment_complete(); |
| 115 |
$order->set_transaction_id($referenceId); |
| 116 |
$order->add_order_note('Cashfree payment successful. Reference id ' . $referenceId); |
| 117 |
$order->add_order_note($txMsg); |
| 118 |
$this->msg['message'] = "Thank you for shopping with us. Your payment has been confirmed. Cashfree reference id is: <b>".$referenceId."</b>."; |
| 119 |
$this->msg['class'] = 'success'; |
| 120 |
|
| 121 |
if (isset($woocommerce->cart) === true) |
| 122 |
{ |
| 123 |
$woocommerce->cart->empty_cart(); |
| 124 |
} |
| 125 |
} |
| 126 |
else |
| 127 |
{ |
| 128 |
$order->update_status( 'failed', __( $txMsg, 'woocommerce' )); |
| 129 |
$this->msg['class'] = 'error'; |
| 130 |
$this->msg['message'] = $txMsg. ". Please try again."; |
| 131 |
} |
| 132 |
|
| 133 |
$this->cashfree->add_notice($this->msg['message'], $this->msg['class']); |
| 134 |
|
| 135 |
} |
| 136 |
} |