| 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 InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 2 |  | 	public function setup() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2 |  | 		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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( 'pronamic-pay/v1', '/adyen/notifications', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  | 			'methods'  => 'POST', | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  | 			'callback' => array( $this, 'rest_api_adyen_notifications' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * REST API Adyen notifications handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @param WP_REST_Request $request Request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  | 	public function rest_api_adyen_notifications( WP_REST_Request $request ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  | 		$json = $request->get_body(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  | 		$data = json_decode( $json ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  | 			$notification_request = NotificationRequest::from_object( $data ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  | 		} catch ( InvalidArgumentException $e ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  | 			return new WP_Error( 'adyen_invalid_notification', __( 'Cannot parse JSON notification.' ), array( 'status' => 500 ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  | 		foreach ( $notification_request->get_items() as $item ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  | 			$payment = get_pronamic_payment( $item->get_merchant_reference() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  | 			if ( empty( $payment ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 				continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			// Store notification. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  | 			$payment->set_meta( 'adyen_notification', $json ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			// Add note. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  | 			$note = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 				/* translators: %s: Adyen */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  | 				__( 'Webhook requested by %s.', 'pronamic_ideal' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  | 				__( 'Adyen', 'pronamic_ideal' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  | 			$payment->add_note( $note ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			// Authorization. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  | 			if ( EventCode::AUTHORIZATION === $item->get_event_code() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  | 				$payment->set_status( $item->is_success() ? PaymentStatus::SUCCESS : PaymentStatus::FAILURE ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$response = (object) array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  | 			'notificationResponse' => '[accepted]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  | 		return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  |