| 1 |
<?php |
| 2 |
|
| 3 |
add_action('rest_api_init', 'hyperpay_rest_orders'); |
| 4 |
|
| 5 |
function hyperpay_rest_orders($request) |
| 6 |
{ |
| 7 |
register_rest_route('hyperpay/v1', '/(?P<method>[a-zA-Z0-9_]+)', array( |
| 8 |
'methods' => 'POST', |
| 9 |
'callback' => 'hyperpay_handel_order_status', |
| 10 |
'permission_callback' => 'hyperpay_auth_chech' |
| 11 |
|
| 12 |
)); |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
function hyperpay_handel_order_status($request) |
| 17 |
{ |
| 18 |
|
| 19 |
$payment_method = $request['method']; |
| 20 |
$gateway = new $payment_method(); |
| 21 |
|
| 22 |
$secret = $gateway->get_option('secret'); |
| 23 |
$initialization_vector = getallheaders()['X-Initialization-Vector']; |
| 24 |
$authentication_tag = getallheaders()['X-Authentication-Tag']; |
| 25 |
|
| 26 |
$key = hex2bin($secret); |
| 27 |
$iv = hex2bin($initialization_vector); |
| 28 |
$auth_tag = hex2bin($authentication_tag); |
| 29 |
$cipher_text = hex2bin($request->get_body()); |
| 30 |
|
| 31 |
$result = openssl_decrypt($cipher_text, "aes-256-gcm", $key, OPENSSL_RAW_DATA, $iv, $auth_tag); |
| 32 |
$result = json_decode($result, true); |
| 33 |
|
| 34 |
if ($result['type'] == 'test') { |
| 35 |
return new WP_REST_Response('test success', 200); |
| 36 |
} |
| 37 |
|
| 38 |
$order_id = explode("I",$result['payload']['merchantTransactionId'])[0] ?? null; |
| 39 |
|
| 40 |
$order = wc_get_order($order_id); |
| 41 |
|
| 42 |
if (!$order) { |
| 43 |
return new WP_Error("not_found", "Not Found", array("status" => 404)); |
| 44 |
} |
| 45 |
|
| 46 |
if ($order->get_payment_method() == 'hyperpay_zoodpay' && $result['payload']['result']['code'] == '100.396.103') { |
| 47 |
$order->update_status('on-hold'); |
| 48 |
return new WP_REST_Response('updated to on-hold', 200); |
| 49 |
} |
| 50 |
|
| 51 |
if (!in_array($order->get_status(), ['on-hold', 'pending']) || $order->get_payment_method() != $gateway->id) { |
| 52 |
return new WP_Error("rest_forbidden", __("Sorry, you are not allowed to do that."), array("status" => 401)); |
| 53 |
} |
| 54 |
|
| 55 |
if (preg_match($gateway->successCodePattern, $result['payload']['result']['code'])) { |
| 56 |
$uniqueId = $result['payload']['id']; |
| 57 |
$order_final_status = $result['payload']['paymentType'] == 'PA' ? 'on-hold' : $gateway->get_option('order_status'); |
| 58 |
$order->add_order_note("Updated by webhook " . __("Transaction ID: ", "hyperpay-payments") . esc_html($uniqueId)); |
| 59 |
if ($result['payload']['paymentType'] == 'CP') |
| 60 |
$order->add_order_note("Captured by webhook "); |
| 61 |
$order->update_status($order_final_status); |
| 62 |
$order->save(); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
return new WP_REST_Response('updated to success', 200); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
function hyperpay_auth_chech($request) |
| 71 |
{ |
| 72 |
if (!class_exists($request['method'])) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
if (!function_exists('getallheaders')) { |
| 79 |
foreach ($_SERVER as $name => $value) { |
| 80 |
/* RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. */ |
| 81 |
if (strtolower(substr($name, 0, 5)) == 'http_') { |
| 82 |
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
| 83 |
} |
| 84 |
} |
| 85 |
return $headers; |
| 86 |
} |
| 87 |
|