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