PluginProbe
Route ‑ Shipping Protection / 2.2.6
Route ‑ Shipping Protection v2.2.6
2.4.17 2.4.16 2.4.15 2.4.14 2.4.13 2.4.12 2.4.11 2.4.10 2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.2.4 2.2.5 2.2.6 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 151 releases
routeapp / admin / class-routeapp-webhooks.php

class-routeapp-webhooks.php in Route ‑ Shipping Protection 2.2.6, at admin/class-routeapp-webhooks.php

151 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Handle webhook upserts.
5 *
6 *
7 * @package Routeapp
8 * @subpackage Routeapp/admin
9 * @author Route App <support@route.com>
10 */
11 class Routeapp_Webhooks {
12
13 const API_GATEWAY_ENDPOINT = 'https://distributed-api.route.com';
14 const API_GATEWAY_ENDPOINT_STAGING = 'https://distributed-api-stage.route.com';
15
16 const webhooksList = [
17 // order.updated will trigger both order update and order create events
18 'RouteApp Order Upsert' => [
19 'name' => 'RouteApp Order Upsert',
20 'status' => 'active',
21 'topic' => 'order.updated',
22 'delivery_url' => '/webhooks/',
23 'update' => false,
24 ],
25 'RouteApp Order Deleted' => [
26 'name' => 'RouteApp Order Deleted',
27 'status' => 'active',
28 'topic' => 'order.deleted',
29 'delivery_url' => '/webhooks/',
30 'update' => false,
31 ],
32 ];
33 private $_webhooksList;
34
35 public function __construct() {
36 $this->buildWebhookList();
37 }
38
39 private function buildWebhookList()
40 {
41 $webhooksList = self::webhooksList;
42 foreach ($webhooksList as $webhookName => $values) {
43 $webhooksList[$webhookName]['delivery_url'] = $this->generate_delivery_url($values['delivery_url']);
44 }
45 $this->_webhooksList = $webhooksList;
46 }
47
48 private function generate_delivery_url($url) {
49 $custom_env = getenv('ROUTEAPP_ENVIRONMENT_ENDPOINT');
50 if (is_null($custom_env) || !$custom_env) {
51 $custom_env = isset($_SERVER['ROUTEAPP_ENVIRONMENT_ENDPOINT']) ? $_SERVER['ROUTEAPP_ENVIRONMENT_ENDPOINT'] : '';
52 }
53
54 $delivery_url = $custom_env == 'stage' ? self::API_GATEWAY_ENDPOINT_STAGING : self::API_GATEWAY_ENDPOINT;
55 $delivery_url.= $url . get_option('routeapp_merchant_id');
56 return $delivery_url;
57 }
58
59 private function generate_default_secret() {
60 $merchant_id = get_option('routeapp_merchant_id');
61 $secret_token = get_option('routeapp_secret_token');
62 $string = $merchant_id . '-' . $secret_token;
63 return hash('sha256', $string);
64 }
65
66 public function set_secret() {
67 $secret = $this->generate_default_secret();
68 update_option( '_routeapp_webhooks_secret', $secret);
69 return $secret;
70 }
71
72 public function get_secret() {
73 $secret = get_option('_routeapp_webhooks_secret', false);
74 if (!$secret) {
75 $secret = $this->set_secret();
76 }
77 return $secret;
78 }
79
80 private function _is_routedata_complete()
81 {
82 $merchant_id = get_option('routeapp_merchant_id', false);
83 $secret_token = get_option('routeapp_secret_token', false);
84 return $merchant_id && $secret_token;
85 }
86
87 public function upsert_webhooks() {
88 //rebuild webhooks list to avoid issues with merchant_id on construct
89 $this->buildWebhookList();
90
91 if ( class_exists( 'WC_Data_Store' ) && $this->_is_routedata_complete() ) {
92 //check webhook secret on database
93 $secret = $this->get_secret();
94
95 $data_store = \WC_Data_Store::load( 'webhook' );
96 $webhooksIds = $data_store->search_webhooks();
97 if (is_array($webhooksIds)) {
98 $webhooks = array_map( 'wc_get_webhook', $webhooksIds );
99 //loop all existing webhooks
100 foreach ($webhooks as $webhook) {
101 if (array_key_exists($webhook->get_name(), $this->_webhooksList)) {
102 $this->_webhooksList[$webhook->get_name()]['id'] = $webhook->get_id();
103 //check configuration
104 if (
105 $this->_webhooksList[$webhook->get_name()]['status'] != $webhook->get_status() ||
106 $this->_webhooksList[$webhook->get_name()]['topic'] != $webhook->get_topic() ||
107 $this->_webhooksList[$webhook->get_name()]['delivery_url'] != $webhook->get_delivery_url() ||
108 $secret != $webhook->get_secret()
109 ) {
110 // difference found, mark for update
111 $this->_webhooksList[$webhook->get_name()]['update'] = true;
112 }
113 }
114 }
115 }
116 //loop through the ones we need to add
117 foreach ($this->_webhooksList as $name => $values) {
118 if (isset($values['id'])) {
119 if ($values['update']) {
120 //remove actual webhook and re-create with the info we have
121 $this->_remove_webhook($values['id']);
122 $this->_create_webhook($values);
123 }
124 } else {
125 //create webhook
126 $this->_create_webhook($values);
127 }
128 }
129
130 update_option( '_routeapp_webhooks_created', 1);
131 }
132 }
133
134 private function _create_webhook($webhookData) {
135 $webhook = new \WC_Webhook();
136 $webhook->set_name($webhookData['name']);
137 $webhook->set_user_id(get_current_user_id()); // User ID used while generating the webhook payload.
138 $webhook->set_topic( $webhookData['topic'] ); // Event used to trigger a webhook.
139 $webhook->set_secret( $this->get_secret() ); // Secret to validate webhook when received.
140 $webhook->set_delivery_url( $webhookData['delivery_url'] ); // URL where webhook should be sent.
141 $webhook->set_status( $webhookData['status'] ); // Webhook status.
142 $webhook->save();
143 }
144
145 private function _remove_webhook($hookId) {
146 $wh = new \WC_Webhook();
147 $wh->set_id($hookId);
148 $wh->delete();
149 }
150 }
151