PluginProbe
HyperPay Payments / trunk
HyperPay Payments vtrunk
6.6.0 trunk 1.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.1.0 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.5 4.0.0 4.0.2 All 34 releases
hyperpay-gateways / src / App / Webhook.php

Webhook.php in HyperPay Payments trunk, at src/App/Webhook.php

103 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hyperpay\Gateways\App;
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 use WP_REST_Response;
7
8 class Webhook
9 {
10
11
12 public static function getallheaders()
13 {
14 foreach ($_SERVER as $name => $value) {
15 /* RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. */
16 if (strtolower(substr($name, 0, 5)) == 'http_') {
17 $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
18 }
19 }
20 return $headers;
21 }
22
23 public static function hyperpay_rest_orders($request)
24 {
25 register_rest_route('hyperpay/v1/Hyperpay/Gateways/Brands', '/(?P<method>[a-zA-Z0-9_]+)', array(
26 'methods' => 'POST',
27 'callback' => [self::class, 'hyperpay_handel_order_status'],
28 'permission_callback' => [self::class, 'hyperpay_auth_chech']
29
30 ));
31 }
32
33
34 public static function hyperpay_handel_order_status($request)
35 {
36
37 $payment_method = "\\Hyperpay\\Gateways\\Brands\\" . $request['method'];
38 $gateway = new $payment_method();
39
40 $secret = $gateway->get_option('secret');
41 $header = self::getallheaders();
42 $initialization_vector = $header['X-Initialization-Vector'];
43 $authentication_tag = $header['X-Authentication-Tag'];
44
45 $key = hex2bin($secret);
46 $iv = hex2bin($initialization_vector);
47 $auth_tag = hex2bin($authentication_tag);
48 $cipher_text = hex2bin($request->get_body());
49
50 if(!$key || !$iv || !$auth_tag){
51 return new WP_REST_Response('auth failed', 403);
52 exit;
53 }
54
55 $result = openssl_decrypt($cipher_text, "aes-256-gcm", $key, OPENSSL_RAW_DATA, $iv, $auth_tag);
56 $result = json_decode($result, true);
57
58 if ($result['type'] == 'test') {
59 return new WP_REST_Response('test success', 200);
60 }
61
62 $order_id = $result['payload']['merchantTransactionId'];
63 $order_id = \explode("I",$order_id)[0] ?? 0;
64
65 $order = wc_get_order($order_id);
66
67 if (!$order) {
68 return new WP_REST_Response("order not Found", 404);
69 }
70
71 if ($order->get_payment_method() == 'hyperpay_zoodpay' && $result['payload']['result']['code'] == '100.396.103') {
72 $order->update_status('on-hold');
73 return new WP_REST_Response('updated to on-hold', 200);
74 }
75
76 if (!in_array($order->get_status(), ['on-hold', 'pending']) || $order->get_payment_method() != $gateway->id) {
77 return new WP_REST_Response(__("Sorry, you are not allowed to do that." , 'hyperpay-gateways'), 401);
78 }
79
80 if (preg_match($gateway->successCodePattern, $result['payload']['result']['code'])) {
81 $uniqueId = $result['payload']['id'];
82 $order_final_status = $result['payload']['paymentType'] == 'PA' ? 'on-hold' : $gateway->get_option('order_status');
83 $order->add_order_note("Updated by webhook " . __("Transaction ID: ", "hyperpay-gateways") . esc_html($uniqueId));
84 if ($result['payload']['paymentType'] == 'CP')
85 $order->add_order_note("Captured by webhook ");
86 $order->update_status($order_final_status);
87 $order->save();
88 }
89
90
91 return new WP_REST_Response('updated to success', 200);
92 }
93
94
95 public static function hyperpay_auth_chech($request)
96 {
97 if (!class_exists("\\Hyperpay\\Gateways\\Brands\\" . $request['method'])) {
98 return false;
99 }
100 return true;
101 }
102 }
103