1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Payments result controller |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2019 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\Adyen |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Adyen; |
12
|
|
|
|
13
|
|
|
use JsonSchema\Exception\ValidationException; |
14
|
|
|
use Pronamic\WordPress\Pay\Core\Statuses as PaymentStatus; |
15
|
|
|
use Pronamic\WordPress\Pay\Plugin; |
16
|
|
|
use WP_Error; |
17
|
|
|
use WP_REST_Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Payments result controller |
21
|
|
|
* |
22
|
|
|
* @link https://docs.adyen.com/developers/checkout/web-sdk/customization/logic#beforecomplete |
23
|
|
|
* |
24
|
|
|
* @author Remco Tolsma |
25
|
|
|
* @version 1.0.3 |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
*/ |
28
|
|
|
class PaymentsResultController { |
29
|
|
|
/** |
30
|
|
|
* Setup. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
9 |
|
public function setup() { |
35
|
9 |
|
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
36
|
9 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* REST API init. |
40
|
|
|
* |
41
|
|
|
* @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
42
|
|
|
* @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
10 |
|
public function rest_api_init() { |
47
|
10 |
|
register_rest_route( |
48
|
10 |
|
Integration::REST_ROUTE_NAMESPACE, |
49
|
10 |
|
'/payments/result/(?P<config_id>\d+)', |
50
|
|
|
array( |
51
|
10 |
|
'methods' => 'POST', |
52
|
10 |
|
'callback' => array( $this, 'rest_api_adyen_payments_result' ), |
53
|
|
|
'args' => array( |
54
|
|
|
'config_id' => array( |
55
|
10 |
|
'description' => __( 'Gateway configuration ID.', 'pronamic_ideal' ), |
56
|
10 |
|
'type' => 'integer', |
57
|
|
|
), |
58
|
|
|
'payload' => array( |
59
|
10 |
|
'description' => __( 'Payload.', 'pronamic_ideal' ), |
60
|
10 |
|
'type' => 'string', |
61
|
|
|
), |
62
|
|
|
'resultCode' => array( |
63
|
10 |
|
'description' => __( 'Result code.', 'pronamic_ideal' ), |
64
|
10 |
|
'type' => 'string', |
65
|
|
|
), |
66
|
|
|
'resultText' => array( |
67
|
10 |
|
'description' => __( 'Result text.', 'pronamic_ideal' ), |
68
|
10 |
|
'type' => 'string', |
69
|
|
|
), |
70
|
|
|
), |
71
|
|
|
) |
72
|
|
|
); |
73
|
10 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* REST API Adyen payments result handler. |
77
|
|
|
* |
78
|
|
|
* @param WP_REST_Request $request Request. |
79
|
|
|
* @return object |
80
|
|
|
*/ |
81
|
2 |
|
public function rest_api_adyen_payments_result( WP_REST_Request $request ) { |
82
|
2 |
|
$config_id = $request->get_param( 'config_id' ); |
83
|
|
|
|
84
|
2 |
|
if ( null === $config_id ) { |
85
|
|
|
return new WP_Error( |
86
|
|
|
'pronamic-pay-adyen-no-gateway-configuration-id', |
87
|
|
|
__( 'No gateway configuration ID given in `config_id` parameter.', 'pronamic_ideal' ) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
$payload = $request->get_param( 'payload' ); |
92
|
|
|
|
93
|
2 |
|
if ( null === $payload ) { |
94
|
1 |
|
return new WP_Error( |
95
|
1 |
|
'pronamic-pay-adyen-no-payload', |
96
|
1 |
|
__( 'No payload given in `payload` parameter.', 'pronamic_ideal' ) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Gateway. |
101
|
1 |
|
$gateway = Plugin::get_gateway( $config_id ); |
102
|
|
|
|
103
|
1 |
|
if ( empty( $gateway ) ) { |
104
|
|
|
return new WP_Error( |
105
|
|
|
'pronamic-pay-adyen-gateway-not-found', |
106
|
|
|
sprintf( |
107
|
|
|
/* translators: %s: Gateway configuration ID */ |
108
|
|
|
__( 'Could not found gateway with ID `%s`.', 'pronamic_ideal' ), |
109
|
|
|
$config_id |
110
|
|
|
), |
111
|
|
|
$config_id |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
if ( ! isset( $gateway->client ) ) { |
116
|
|
|
return new WP_Error( |
117
|
|
|
'pronamic-pay-adyen-client-not-found', |
118
|
|
|
sprintf( |
119
|
|
|
/* translators: %s: Gateway configuration ID */ |
120
|
|
|
__( 'Could not found client in gateway with ID `%s`.', 'pronamic_ideal' ), |
121
|
|
|
$config_id |
122
|
|
|
), |
123
|
|
|
$config_id |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Client. |
128
|
1 |
|
$payment_result_request = new PaymentResultRequest( $payload ); |
129
|
|
|
|
130
|
1 |
|
$payment_result_response = $gateway->client->get_payment_result( $payment_result_request ); |
131
|
|
|
|
132
|
|
|
$merchant_reference = $payment_result_response->get_merchant_reference(); |
133
|
|
|
|
134
|
|
|
// Payment. |
135
|
|
|
$payment = get_pronamic_payment( $merchant_reference ); |
136
|
|
|
|
137
|
|
|
if ( empty( $payment ) ) { |
138
|
|
|
return new WP_Error( |
139
|
|
|
'pronamic-pay-adyen-payment-not-found', |
140
|
|
|
sprintf( |
141
|
|
|
/* translators: %s: Adyen merchant reference */ |
142
|
|
|
__( 'Could not found payment with ID `%s`.', 'pronamic_ideal' ), |
143
|
|
|
$merchant_reference |
144
|
|
|
), |
145
|
|
|
$payment_result_response |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
PaymentResultHelper::update_payment( $payment, $payment_result_response ); |
150
|
|
|
|
151
|
|
|
// Return payment result response. |
152
|
|
|
return $payment_result_response->get_json(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|