wp-pay-gateways /
adyen
| 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 | * @author Remco Tolsma |
||
| 24 | * @version 1.0.0 |
||
| 25 | * @since 1.0.0 |
||
| 26 | */ |
||
| 27 | class PaymentsResultController { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Setup. |
||
| 31 | */ |
||
| 32 | 2 | public function setup() { |
|
| 33 | 2 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
| 34 | 2 | } |
|
| 35 | |||
| 36 | /** |
||
| 37 | * REST API init. |
||
| 38 | * |
||
| 39 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
||
| 40 | * @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
||
| 41 | */ |
||
| 42 | 1 | public function rest_api_init() { |
|
| 43 | 1 | register_rest_route( |
|
| 44 | 1 | Integration::REST_ROUTE_NAMESPACE, |
|
| 45 | 1 | '/payments/result/(?P<config_id>\d+)', |
|
| 46 | array( |
||
| 47 | 1 | 'methods' => 'POST', |
|
| 48 | 1 | 'callback' => array( $this, 'rest_api_adyen_payments_result' ), |
|
| 49 | 'args' => array( |
||
| 50 | 'config_id' => array( |
||
| 51 | 1 | 'description' => __( 'Gateway configuration ID.', 'pronamic_ideal' ), |
|
| 52 | 1 | 'type' => 'integer', |
|
| 53 | ), |
||
| 54 | 'payload' => array( |
||
| 55 | 1 | 'description' => __( 'Payload.', 'pronamic_ideal' ), |
|
| 56 | 1 | 'type' => 'string', |
|
| 57 | ), |
||
| 58 | 'resultCode' => array( |
||
| 59 | 1 | 'description' => __( 'Result code.', 'pronamic_ideal' ), |
|
| 60 | 1 | 'type' => 'string', |
|
| 61 | ), |
||
| 62 | 'resultText' => array( |
||
| 63 | 1 | 'description' => __( 'Result text.', 'pronamic_ideal' ), |
|
| 64 | 1 | 'type' => 'string', |
|
| 65 | ), |
||
| 66 | ), |
||
| 67 | ) |
||
| 68 | ); |
||
| 69 | 1 | } |
|
| 70 | |||
| 71 | /** |
||
| 72 | * REST API Adyen payments result handler. |
||
| 73 | * |
||
| 74 | * @param WP_REST_Request $request Request. |
||
| 75 | */ |
||
| 76 | public function rest_api_adyen_payments_result( WP_REST_Request $request ) { |
||
| 77 | $config_id = $request->get_param( 'config_id' ); |
||
| 78 | $payload = $request->get_param( 'payload' ); |
||
| 79 | |||
| 80 | // Gateway. |
||
| 81 | $gateway = Plugin::get_gateway( $config_id ); |
||
| 82 | |||
| 83 | if ( empty( $gateway ) ) { |
||
| 84 | return new WP_Error( |
||
| 85 | 'pronamic-pay-adyen-gateway-not-found', |
||
| 86 | sprintf( |
||
| 87 | /* translators: %s: Gateway configuration ID */ |
||
| 88 | __( 'Could not found gateway with ID `%s`.', 'pronamic_ideal' ), |
||
| 89 | $config_id |
||
| 90 | ), |
||
| 91 | $config_id |
||
| 92 | ); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Client. |
||
| 96 | $client = $gateway->client; |
||
| 97 | |||
| 98 | $payment_result_request = new PaymentResultRequest( $payload ); |
||
| 99 | |||
| 100 | $payment_result_response = $gateway->client->get_payment_result( $payment_result_request ); |
||
| 101 | |||
| 102 | $merchant_reference = $payment_result_response->get_merchant_reference(); |
||
| 103 | |||
| 104 | // Payment. |
||
| 105 | $payment = get_pronamic_payment( $merchant_reference ); |
||
| 106 | |||
| 107 | if ( empty( $payment ) ) { |
||
| 108 | return new WP_Error( |
||
| 109 | 'pronamic-pay-adyen-payment-not-found', |
||
| 110 | sprintf( |
||
| 111 | /* translators: %s: Adyen merchant reference */ |
||
| 112 | __( 'Could not found payment with ID `%s`.', 'pronamic_ideal' ), |
||
| 113 | $merchant_reference |
||
| 114 | ), |
||
| 115 | $payment_result_response |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Add note. |
||
| 120 | $note = sprintf( |
||
| 121 | '<p>%1$s</p><pre>%2$s</pre>', |
||
| 122 | sprintf( |
||
| 123 | /* translators: %s: payment provider name */ |
||
| 124 | __( 'Verified payment result.', 'pronamic_ideal' ), |
||
| 125 | __( 'Adyen', 'pronamic_ideal' ) |
||
| 126 | ), |
||
| 127 | wp_json_encode( $payment_result_response->get_json(), JSON_PRETTY_PRINT ) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 128 | ); |
||
| 129 | |||
| 130 | $payment->add_note( $note ); |
||
| 131 | |||
| 132 | // PSP reference. |
||
| 133 | $psp_reference = $payment_result_response->get_psp_reference(); |
||
| 134 | |||
| 135 | if ( null !== $psp_reference ) { |
||
| 136 | $payment->set_transaction_id( $psp_reference ); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Result code. |
||
| 140 | $result_code = $payment_result_response->get_result_code(); |
||
| 141 | |||
| 142 | if ( null !== $result_code ) { |
||
| 143 | $payment->set_status( ResultCode::transform( $result_code ) ); |
||
| 144 | } |
||
| 145 | |||
| 146 | // Payment save. |
||
| 147 | $payment->save(); |
||
| 148 | |||
| 149 | // Return payment result response. |
||
| 150 | return $payment_result_response->get_json(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 |