wp-pay-gateways /
adyen
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Notifications 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 WP_Error; |
||
| 16 | use WP_REST_Request; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Notification controller |
||
| 20 | * |
||
| 21 | * @link https://docs.adyen.com/developers/api-reference/notifications-api#notificationrequest |
||
| 22 | * @author Remco Tolsma |
||
| 23 | * @version 1.0.0 |
||
| 24 | * @since 1.0.0 |
||
| 25 | */ |
||
| 26 | class NotificationsController { |
||
| 27 | /** |
||
| 28 | * Setup. |
||
| 29 | */ |
||
| 30 | 8 | public function setup() { |
|
| 31 | 8 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
| 32 | 8 | } |
|
| 33 | |||
| 34 | /** |
||
| 35 | * REST API init. |
||
| 36 | * |
||
| 37 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
||
| 38 | * @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
||
| 39 | */ |
||
| 40 | 1 | public function rest_api_init() { |
|
| 41 | 1 | register_rest_route( |
|
| 42 | 1 | Integration::REST_ROUTE_NAMESPACE, |
|
| 43 | 1 | '/notifications', |
|
| 44 | array( |
||
| 45 | 1 | 'methods' => 'POST', |
|
| 46 | 1 | 'callback' => array( $this, 'rest_api_adyen_notifications' ), |
|
| 47 | 1 | 'permission_callback' => array( $this, 'rest_api_adyen_permissions_check' ), |
|
| 48 | ) |
||
| 49 | ); |
||
| 50 | 1 | } |
|
| 51 | |||
| 52 | /** |
||
| 53 | * REST API Adyen permissions check. |
||
| 54 | * |
||
| 55 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#permissions-callback |
||
| 56 | * |
||
| 57 | * @param WP_REST_Request $request Request. |
||
| 58 | */ |
||
| 59 | 6 | public function rest_api_adyen_permissions_check( WP_REST_Request $request ) { |
|
| 60 | 6 | $username = get_option( 'pronamic_pay_adyen_notification_authentication_username' ); |
|
| 61 | 6 | $password = get_option( 'pronamic_pay_adyen_notification_authentication_password' ); |
|
| 62 | |||
| 63 | 6 | if ( empty( $username ) && empty( $password ) ) { |
|
| 64 | 4 | return true; |
|
| 65 | } |
||
| 66 | |||
| 67 | 2 | $authorization = $request->get_header( 'Authorization' ); |
|
| 68 | |||
| 69 | 2 | if ( 'Basic ' . base64_encode( $username . ':' . $password ) === $authorization ) { |
|
| 70 | 1 | return true; |
|
| 71 | } |
||
| 72 | |||
| 73 | 1 | return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to post Adyen notifications.' ), array( 'status' => rest_authorization_required_code() ) ); |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * REST API Adyen notifications handler. |
||
| 78 | * |
||
| 79 | * @param WP_REST_Request $request Request. |
||
| 80 | */ |
||
| 81 | 5 | public function rest_api_adyen_notifications( WP_REST_Request $request ) { |
|
| 82 | 5 | $json = $request->get_body(); |
|
| 83 | |||
| 84 | 5 | $data = json_decode( $json ); |
|
| 85 | |||
| 86 | try { |
||
| 87 | 5 | $notification_request = NotificationRequest::from_object( $data ); |
|
| 88 | 2 | } catch ( ValidationException $e ) { |
|
| 89 | 2 | return new WP_Error( 'adyen_invalid_notification', __( 'Cannot parse JSON notification.' ), array( 'status' => 500 ) ); |
|
| 90 | } |
||
| 91 | |||
| 92 | 3 | foreach ( $notification_request->get_items() as $item ) { |
|
| 93 | 3 | $payment = get_pronamic_payment( $item->get_merchant_reference() ); |
|
| 94 | |||
| 95 | 3 | if ( null === $payment ) { |
|
| 96 | 2 | continue; |
|
| 97 | } |
||
| 98 | |||
| 99 | // Add note. |
||
| 100 | 1 | $note = sprintf( |
|
| 101 | 1 | '<p>%1$s</p><pre>%2$s</pre>', |
|
| 102 | 1 | sprintf( |
|
| 103 | /* translators: %s: payment provider name */ |
||
| 104 | 1 | __( 'Webhook requested by %s.', 'pronamic_ideal' ), |
|
| 105 | 1 | __( 'Adyen', 'pronamic_ideal' ) |
|
| 106 | ), |
||
| 107 | 1 | wp_json_encode( $item->get_json(), JSON_PRETTY_PRINT ) |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 108 | ); |
||
| 109 | |||
| 110 | 1 | $payment->add_note( $note ); |
|
| 111 | |||
| 112 | // Authorization. |
||
| 113 | 1 | if ( EventCode::AUTHORIZATION === $item->get_event_code() ) { |
|
| 114 | 1 | $payment->set_status( $item->is_success() ? PaymentStatus::SUCCESS : PaymentStatus::FAILURE ); |
|
| 115 | |||
| 116 | 1 | $payment->save(); |
|
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | $response = (object) array( |
||
| 121 | 3 | 'notificationResponse' => '[accepted]', |
|
| 122 | ); |
||
| 123 | |||
| 124 | 3 | return $response; |
|
| 125 | } |
||
| 126 | } |
||
| 127 |