-
Notifications
You must be signed in to change notification settings - Fork 69
/
class-woopay-order-status-sync.php
215 lines (184 loc) · 5.8 KB
/
class-woopay-order-status-sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Class WooPay_Webhooks
*
* @package WooCommerce\Payments
*/
namespace WCPay\WooPay;
use WC_Payments_Account;
use WC_Payments_API_Client;
use WCPay\Exceptions\API_Exception;
defined( 'ABSPATH' ) || exit;
/**
* This class introduces webhooks to delivery order updates to the associated
* orders in the woopay.
*
* WooPay Webhooks are enqueued to their associated actions, delivered, and logged.
*/
class WooPay_Order_Status_Sync {
const WCPAY_WEBHOOK_WOOPAY_ORDER_STATUS_CHANGED = 'wcpay_webhook_platform_checkout_order_status_changed';
/**
* WC_Payments_Account instance to get information about the account
*
* @var WC_Payments_Account
*/
private $account;
/**
* Client for making requests to the WooCommerce Payments API
*
* @var WC_Payments_API_Client
*/
protected $payments_api_client;
/**
* Setup webhook for the WooPay Order Status Sync.
*
* @param WC_Payments_API_Client $payments_api_client - WooCommerce Payments API client.
* @param WC_Payments_Account $account - WooCommerce Payments account.
*/
public function __construct( WC_Payments_API_Client $payments_api_client, WC_Payments_Account $account ) {
$this->payments_api_client = $payments_api_client;
$this->account = $account;
add_filter( 'woocommerce_webhook_topic_hooks', [ __CLASS__, 'add_topics' ], 20, 2 );
add_filter( 'woocommerce_webhook_payload', [ __CLASS__, 'create_payload' ], 10, 4 );
add_filter( 'woocommerce_valid_webhook_resources', [ __CLASS__, 'add_resource' ], 10, 1 );
add_filter( 'woocommerce_valid_webhook_events', [ __CLASS__, 'add_event' ], 10, 1 );
add_action( 'woocommerce_order_status_changed', [ __CLASS__, 'send_webhook' ], 10, 3 );
add_action( 'admin_init', [ $this, 'maybe_create_woopay_order_webhook' ], 10 );
}
/**
* Return the webhook name.
*
* @return string
*/
private static function get_webhook_name() {
return __( 'WCPay woopay order status sync', 'woocommerce-payments' );
}
/**
* Maybe create the WooPay webhook under certain conditions.
*/
public function maybe_create_woopay_order_webhook() {
if ( ! current_user_can( 'manage_woocommerce' ) || self::is_webhook_created() ) {
return;
}
if ( ! $this->account->is_stripe_account_valid() || $this->account->is_account_under_review() || $this->account->is_account_rejected() ) {
return;
}
$this->register_webhook();
}
/**
* Return true if webhook was already created.
*
* @return bool
*/
private static function is_webhook_created() {
return ! empty( self::get_webhook() );
}
/**
* Return array with the webhook id for the woopay order status sync.
*
* @return array
*/
public static function get_webhook() {
$data_store = \WC_Data_Store::load( 'webhook' );
$args = [
'search' => self::get_webhook_name(),
'status' => 'active',
'limit' => 1,
];
$webhooks = $data_store->search_webhooks( $args );
return $webhooks;
}
/**
* Register the webhook on WooCommerce.
*
* @return void
*/
private function register_webhook() {
$webhook = new \WC_Webhook();
$webhook->set_name( self::get_webhook_name() );
$webhook->set_user_id( get_current_user_id() );
$webhook->set_topic( 'order.status_changed' );
$webhook->set_secret( wp_generate_password( 50, false ) );
$webhook->set_delivery_url( WooPay_Utilities::get_woopay_rest_url( 'merchant-notification' ) );
$webhook->set_status( 'active' );
$webhook->save();
try {
$this->payments_api_client->update_woopay( [ 'webhook_secret' => $webhook->get_secret() ] );
} catch ( API_Exception $e ) {
$webhook->delete();
}
}
/**
* Add order webhook topic
*
* @param array $topic_hooks List of WooCommerce's standard webhook topics and hooks.
*/
public static function add_topics( $topic_hooks ) {
$topic_hooks['order.status_changed'][] = self::WCPAY_WEBHOOK_WOOPAY_ORDER_STATUS_CHANGED;
return $topic_hooks;
}
/**
* Setup payload for the webhook delivery.
*
* @param array $payload Data to be sent out by the webhook.
* @param string $resource_name Type/name of the resource.
* @param integer $resource_id ID of the resource.
* @param integer $id ID of the webhook.
*/
public static function create_payload( $payload, $resource_name, $resource_id, $id ) {
$webhook = wc_get_webhook( $id );
if ( 0 !== strpos( $webhook->get_delivery_url(), WooPay_Utilities::get_woopay_rest_url( 'merchant-notification' ) ) ) {
// This is not a WooPay webhook, so we don't need to modify the payload.
return $payload;
}
return [
'blog_id' => \Jetpack_Options::get_option( 'id' ),
'order_id' => $resource_id,
'order_status' => $payload['status'],
];
}
/**
* Add webhook resource for order.
*
* @param array $resources List of available resources.
*/
public static function add_resource( $resources ) {
$resources[] = 'order';
return $resources;
}
/**
* Undocumented function
*
* @param array $topic_events List of available topic events.
*/
public static function add_event( $topic_events ) {
$topic_events[] = 'status_changed';
return $topic_events;
}
/**
* Trigger webhook delivery.
*
* @param int $order_id Order id.
* @param string $previous_status the old WooCommerce order status.
* @param string $next_status the new WooCommerce order status.
* @return void
*/
public static function send_webhook( $order_id, $previous_status, $next_status ) {
$order = wc_get_order( $order_id );
if ( $order->get_meta( 'is_woopay' ) ) {
do_action( self::WCPAY_WEBHOOK_WOOPAY_ORDER_STATUS_CHANGED, $order_id, $next_status );
}
}
/**
* Removes the webhook if woopay is disabled.
*
* @return void
*/
public static function remove_webhook() {
if ( self::is_webhook_created() ) {
$webhook_id = self::get_webhook()[0];
$webhook = new \WC_Webhook( $webhook_id );
$webhook->delete();
}
}
}