PayPalWebhookHeaders.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\PayPalCommerce\DataTransferObjects; |
| 4 | |
| 5 | use HttpHeaderException; |
| 6 | |
| 7 | class PayPalWebhookHeaders { |
| 8 | /** |
| 9 | * @since 2.9.0 |
| 10 | * @var string |
| 11 | */ |
| 12 | public $transmissionId; |
| 13 | |
| 14 | /** |
| 15 | * @since 2.9.0 |
| 16 | * @var string |
| 17 | */ |
| 18 | public $transmissionTime; |
| 19 | |
| 20 | /** |
| 21 | * @since 2.9.0 |
| 22 | * @var string |
| 23 | */ |
| 24 | public $transmissionSig; |
| 25 | |
| 26 | /** |
| 27 | * @since 2.9.0 |
| 28 | * @var string |
| 29 | */ |
| 30 | public $certUrl; |
| 31 | |
| 32 | /** |
| 33 | * @since 2.9.0 |
| 34 | * @var string |
| 35 | */ |
| 36 | public $authAlgo; |
| 37 | |
| 38 | /** |
| 39 | * This grabs the headers from the webhook request to be used in the signature verification |
| 40 | * |
| 41 | * A strange thing here is that the headers are inconsistent between live and sandbox mode, so this also checks for |
| 42 | * both forms of the headers (studly case and all caps). |
| 43 | * |
| 44 | * @since 2.9.0 |
| 45 | * |
| 46 | * @param array $headers |
| 47 | * |
| 48 | * @return self |
| 49 | * @throws HttpHeaderException |
| 50 | */ |
| 51 | public static function fromHeaders( array $headers ) { |
| 52 | $headerKeys = [ |
| 53 | 'transmissionId' => 'Paypal-Transmission-Id', |
| 54 | 'transmissionTime' => 'Paypal-Transmission-Time', |
| 55 | 'transmissionSig' => 'Paypal-Transmission-Sig', |
| 56 | 'certUrl' => 'Paypal-Cert-Url', |
| 57 | 'authAlgo' => 'Paypal-Auth-Algo', |
| 58 | ]; |
| 59 | |
| 60 | $payPalHeaders = new self(); |
| 61 | $missingKeys = []; |
| 62 | foreach ( $headerKeys as $property => $key ) { |
| 63 | if ( ! isset( $headers[ $key ] ) ) { |
| 64 | $key = strtoupper( $key ); |
| 65 | } |
| 66 | |
| 67 | if ( isset( $headers[ $key ] ) ) { |
| 68 | $payPalHeaders->$property = $headers[ $key ]; |
| 69 | } else { |
| 70 | $missingKeys[] = $key; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if ( ! empty( $missingKeys ) ) { |
| 75 | give_record_gateway_error( |
| 76 | 'Missing PayPal webhook header', |
| 77 | print_r( |
| 78 | [ |
| 79 | 'missingKeys' => $missingKeys, |
| 80 | 'headers' => $headers, |
| 81 | ], |
| 82 | true |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | throw new HttpHeaderException( "Missing PayPal header: $key" ); |
| 87 | } |
| 88 | |
| 89 | return $payPalHeaders; |
| 90 | } |
| 91 | } |
| 92 |