WebhookValidator.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\PayPalStandard\Webhooks; |
| 4 | |
| 5 | use Give\Log\Log; |
| 6 | |
| 7 | /** |
| 8 | * This class use to validate PayPal Standard ipn. |
| 9 | * Validate the IPN: https://developer.paypal.com/docs/api-basics/notifications/ipn/IPNImplementation/ |
| 10 | * |
| 11 | * @since 2.19.0 |
| 12 | */ |
| 13 | class WebhookValidator |
| 14 | { |
| 15 | /** |
| 16 | * @since 2.19.0 |
| 17 | * @since 2.19.3 Update log message. |
| 18 | * |
| 19 | * @param array $eventData PayPal ipn body data. |
| 20 | * |
| 21 | * @return bool |
| 22 | */ |
| 23 | public function verifyEventSignature(array $eventData) |
| 24 | { |
| 25 | $eventData = array_merge( [ 'cmd' => '_notify-validate' ], $eventData ); |
| 26 | |
| 27 | // Validate IPN request w/ PayPal if user hasn't disabled this security measure. |
| 28 | if (! give_is_setting_enabled(give_get_option('paypal_verification', 'enabled'))) { |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | $requestArgs = [ |
| 33 | 'method' => 'POST', |
| 34 | 'timeout' => 45, |
| 35 | 'redirection' => 5, |
| 36 | 'httpversion' => '1.1', |
| 37 | 'blocking' => true, |
| 38 | 'headers' => [ |
| 39 | 'host' => give_is_test_mode() ? 'www.sandbox.paypal.com' : 'www.paypal.com', |
| 40 | 'connection' => 'close', |
| 41 | 'content-type' => 'application/x-www-form-urlencoded', |
| 42 | 'post' => '/cgi-bin/webscr HTTP/1.1', |
| 43 | ], |
| 44 | 'sslverify' => false, |
| 45 | 'body' => $eventData, |
| 46 | ]; |
| 47 | |
| 48 | $apiResponse = wp_remote_post(give_get_paypal_redirect(), $requestArgs); |
| 49 | |
| 50 | if (is_wp_error($apiResponse)) { |
| 51 | Log::error( |
| 52 | 'PayPal Standard IPN Error', |
| 53 | ['IPN Data' => $apiResponse] |
| 54 | ); |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | if ('VERIFIED' !== $apiResponse['body']) { |
| 60 | Log::warning( |
| 61 | 'PayPal Standard IPN Error', |
| 62 | [ |
| 63 | 'Message' => 'This is not a verified IPN.', |
| 64 | 'IPN Data' => $apiResponse |
| 65 | ] |
| 66 | ); |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 |