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