class-wc-rest-payments-reader-controller.php
11.3 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* Class WC_REST_Payments_Reader_Charges
*
* @package WooCommerce\Payments\Admin
*/
use WCPay\Constants\Payment_Intent_Status;
use WCPay\Exceptions\API_Exception;
defined( 'ABSPATH' ) || exit;
require_once WCPAY_ABSPATH . 'includes/in-person-payments/class-wc-payments-printed-receipt-sample-order.php';
/**
* REST controller for reader charges.
*/
class WC_REST_Payments_Reader_Controller extends WC_Payments_REST_Controller {
const STORE_READERS_TRANSIENT_KEY = 'wcpay_store_terminal_readers';
const PREVIEW_RECEIPT_CHARGE_DATA = [
'amount_captured' => 0,
'payment_method_details' => [
'card_present' => [
'brand' => 'Sample',
'last4' => '0000',
'receipt' => [
'application_preferred_name' => 'Sample, Receipts preview',
'dedicated_file_name' => '0000',
'account_type' => 'Sample',
],
],
],
];
/**
* Endpoint path.
*
* @var string
*/
protected $rest_base = 'payments/readers';
/**
* Instance of WC_Payment_Gateway_WCPay.
*
* @var WC_Payment_Gateway_WCPay
*/
private $wcpay_gateway;
/**
* Instance of WC_Payments_In_Person_Payments_Receipts_Service.
*
* @var WC_Payments_In_Person_Payments_Receipts_Service
*/
private $receipts_service;
/**
* WC_REST_Payments_Reader_Controller
*
* @param WC_Payments_API_Client $api_client WC_Payments_API_Client.
* @param WC_Payment_Gateway_WCPay $wcpay_gateway WC_Payment_Gateway_WCPay.
* @param WC_Payments_In_Person_Payments_Receipts_Service $receipts_service WC_Payments_In_Person_Payments_Receipts_Service.
* @return void
*/
public function __construct( WC_Payments_API_Client $api_client, WC_Payment_Gateway_WCPay $wcpay_gateway, WC_Payments_In_Person_Payments_Receipts_Service $receipts_service ) {
parent::__construct( $api_client );
$this->wcpay_gateway = $wcpay_gateway;
$this->receipts_service = $receipts_service;
}
/**
* Configure REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_all_readers' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'register_reader' ],
'permission_callback' => [ $this, 'check_permission' ],
'args' => [
'location' => [
'type' => 'string',
'required' => true,
],
'registration_code' => [
'type' => 'string',
'required' => true,
],
'label' => [
'type' => 'string',
],
'metadata' => [
'type' => 'object',
],
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/charges/(?P<transaction_id>\w+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_summary' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/receipts/preview',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'preview_print_receipt' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/receipts/(?P<payment_intent_id>\w+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'generate_print_receipt' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
}
/**
* Retrieve payment readers charges to respond with via API.
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_Error|WP_HTTP_Response|WP_REST_Response
*/
public function get_summary( $request ) {
$transaction_id = $request->get_param( 'transaction_id' );
try {
// retrieve transaction details to get the charge date.
$transaction = $this->api_client->get_transaction( $transaction_id );
if ( empty( $transaction ) ) {
return rest_ensure_response( [] );
}
$summary = $this->api_client->get_readers_charge_summary( gmdate( 'Y-m-d', $transaction['created'] ) );
} catch ( API_Exception $e ) {
return rest_ensure_response( new WP_Error( 'wcpay_get_summary', $e->getMessage() ) );
}
return rest_ensure_response( $summary );
}
/**
* Proxies the get all readers request to the server.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error
*/
public function get_all_readers( $request ) {
try {
return rest_ensure_response( $this->fetch_readers() );
} catch ( API_Exception $e ) {
return rest_ensure_response( new WP_Error( $e->get_error_code(), $e->getMessage() ) );
}
}
/**
* Links a card reader to an account and terminal location.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error
*/
public function register_reader( $request ) {
try {
$response = $this->api_client->register_terminal_reader(
$request->get_param( 'location' ),
$request->get_param( 'registration_code' ),
$request->get_param( 'label' ),
$request->get_param( 'metadata' )
);
$reader = wp_array_slice_assoc( $response, [ 'id', 'livemode', 'device_type', 'label', 'location', 'metadata', 'status' ] );
return rest_ensure_response( $reader );
} catch ( API_Exception $e ) {
return rest_ensure_response(
new WP_Error(
$e->get_error_code(),
$e->getMessage(),
[ 'status' => $e->get_http_code() ]
)
);
}
}
/**
* Check if the reader status is active
*
* @param array $readers The readers charges object.
* @param string $id The reader ID.
* @return bool
*/
private function is_reader_active( $readers, $id ) {
foreach ( $readers as $reader ) {
if ( $reader['reader_id'] === $id && 'active' === $reader['status'] ) {
return true;
}
}
return false;
}
/**
* Attempts to read readers from transient and re-populates it if needed.
*
* @return array Terminal readers.
* @throws API_Exception If request to server fails.
*/
private function fetch_readers(): array {
$readers = get_transient( static::STORE_READERS_TRANSIENT_KEY );
if ( ! $readers ) {
// Retrieve terminal readers.
$readers_data = $this->api_client->get_terminal_readers();
// Retrieve the readers by charges.
$reader_by_charges = $this->api_client->get_readers_charge_summary( gmdate( 'Y-m-d', time() ) );
$readers = [];
foreach ( $readers_data as $reader ) {
$readers[] = [
'id' => $reader['id'],
'livemode' => $reader['livemode'],
'device_type' => $reader['device_type'],
'label' => $reader['label'],
'location' => $reader['location'],
'metadata' => $reader['metadata'],
'status' => $reader['status'],
'is_active' => $this->is_reader_active( $reader_by_charges, $reader['id'] ),
];
}
set_transient( static::STORE_READERS_TRANSIENT_KEY, $readers, 2 * HOUR_IN_SECONDS );
}
return $readers;
}
/**
* Renders HTML for a print receipt
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_HTTP_Response|WP_Error
* @throws \RuntimeException Error collecting data.
*/
public function generate_print_receipt( $request ) {
try {
/* Collect the data, available on the server side. */
$payment_intent = $this->api_client->get_intent( $request->get_param( 'payment_intent_id' ) );
if ( Payment_Intent_Status::SUCCEEDED !== $payment_intent->get_status() ) {
throw new \RuntimeException( __( 'Invalid payment intent', 'woocommerce-payments' ) );
}
$charge = $payment_intent->get_charge();
$charge_id = $charge ? $charge->get_id() : null;
$charge_array = $this->api_client->get_charge( $charge_id );
/* Collect receipt data, stored on the store side. */
$order = wc_get_order( $charge_array['order']['number'] );
if ( false === $order ) {
throw new \RuntimeException( __( 'Order not found', 'woocommerce-payments' ) );
}
// Retrieve branding logo file ID.
$branding_logo = $this->wcpay_gateway->get_option( 'account_branding_logo', '' );
/* Collect merchant settings */
$settings = [
'branding_logo' => ( ! empty( $branding_logo ) ) ? $this->api_client->get_file_contents( $branding_logo, false ) : [],
'business_name' => $this->wcpay_gateway->get_option( 'account_business_name' ),
'support_info' => [
'address' => $this->wcpay_gateway->get_option( 'account_business_support_address' ),
'phone' => $this->wcpay_gateway->get_option( 'account_business_support_phone' ),
'email' => $this->wcpay_gateway->get_option( 'account_business_support_email' ),
],
];
/* Generate receipt */
$response = [ 'html_content' => $this->receipts_service->get_receipt_markup( $settings, $order, $charge_array ) ];
} catch ( \Throwable $e ) {
$error_status_code = $e instanceof API_Exception ? $e->get_http_code() : 500;
$response = new WP_Error( 'generate_print_receipt_error', $e->getMessage(), [ 'status' => $error_status_code ] );
}
return rest_ensure_response( $response );
}
/**
* Returns HTML to preview a print receipt
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_HTTP_Response|WP_Error
* @throws \RuntimeException Error collecting data.
*/
public function preview_print_receipt( WP_REST_Request $request ) {
$preview = $this->receipts_service->get_receipt_markup(
$this->create_print_preview_receipt_settings_data( $request->get_json_params() ),
new WC_Payments_Printed_Receipt_Sample_Order(),
self::PREVIEW_RECEIPT_CHARGE_DATA
);
return rest_ensure_response( [ 'html_content' => $preview ] );
}
/**
* Creates settings data to be used on the printed receipt preview. Defaults to stored settings if one parameter is not provided.
*
* @param array $receipt_settings Array of settings to use to create the receipt preview.
* @return array
*/
private function create_print_preview_receipt_settings_data( array $receipt_settings ): array {
$support_address = empty( $receipt_settings['accountBusinessSupportAddress'] ) ? $this->wcpay_gateway->get_option( 'account_business_support_address' ) : $receipt_settings['accountBusinessSupportAddress'];
return [
'business_name' => empty( $receipt_settings['accountBusinessName'] ) ? $this->wcpay_gateway->get_option( 'account_business_name' ) : $receipt_settings['accountBusinessName'],
'support_info' => [
'address' => [
'line1' => $support_address['line1'],
'line2' => $support_address['line2'],
'city' => $support_address['city'],
'state' => $support_address['state'],
'postal_code' => $support_address['postal_code'],
'country' => $support_address['country'],
],
'phone' => empty( $receipt_settings['accountBusinessSupportPhone'] ) ? $this->wcpay_gateway->get_option( 'account_business_support_phone' ) : $receipt_settings['accountBusinessSupportPhone'],
'email' => empty( $receipt_settings['accountBusinessSupportEmail'] ) ? $this->wcpay_gateway->get_option( 'account_business_support_email' ) : $receipt_settings['accountBusinessSupportEmail'],
],
];
}
}