Add request body for VerifyWebhookSignature

- Fixes #798
This commit is contained in:
brluk and japatel
2017-03-22 17:16:35 -05:00
parent 1c6734257f
commit bf3f436523
2 changed files with 53 additions and 12 deletions

View File

@@ -166,7 +166,7 @@ class VerifyWebhookSignature extends PayPalResourceModel
/** /**
* The webhook notification, which is the content of the HTTP `POST` request body. * The webhook notification, which is the content of the HTTP `POST` request body.
* * @deprecated Please use setRequestBody($request_body) instead.
* @param \PayPal\Api\WebhookEvent $webhook_event * @param \PayPal\Api\WebhookEvent $webhook_event
* *
* @return $this * @return $this
@@ -187,6 +187,29 @@ class VerifyWebhookSignature extends PayPalResourceModel
return $this->webhook_event; return $this->webhook_event;
} }
/**
* The content of the HTTP `POST` request body of the webhook notification you received as a string.
*
* @param string $request_body
*
* @return $this
*/
public function setRequestBody($request_body)
{
$this->request_body = $request_body;
return $this;
}
/**
* The content of the HTTP `POST` request body of the webhook notification you received as a string.
*
* @return string
*/
public function getRequestBody()
{
return $this->request_body;
}
/** /**
* Verifies a webhook signature. * Verifies a webhook signature.
* *
@@ -197,6 +220,7 @@ class VerifyWebhookSignature extends PayPalResourceModel
public function post($apiContext = null, $restCall = null) public function post($apiContext = null, $restCall = null)
{ {
$payLoad = $this->toJSON(); $payLoad = $this->toJSON();
$json = self::executeCall( $json = self::executeCall(
"/v1/notifications/verify-webhook-signature", "/v1/notifications/verify-webhook-signature",
"POST", "POST",
@@ -210,4 +234,23 @@ class VerifyWebhookSignature extends PayPalResourceModel
return $ret; return $ret;
} }
public function toJSON($options = 0)
{
if (!is_null($this->request_body)) {
$valuesToEncode = $this->toArray();
unset($valuesToEncode['webhook_event']);
unset($valuesToEncode['request_body']);
$payLoad = "{";
foreach ($valuesToEncode as $field => $value) {
$payLoad .= "\"$field\": \"$value\",";
}
$payLoad .= "\"webhook_event\": $this->request_body";
$payLoad .= "}";
return $payLoad;
} else {
$payLoad = parent::toJSON($options);
return $payLoad;
}
}
} }

View File

@@ -57,9 +57,7 @@ $signatureVerification->setWebhookId("9XL90610J3647323C"); // Note that the Webh
$signatureVerification->setTransmissionSig($headers['PAYPAL-TRANSMISSION-SIG']); $signatureVerification->setTransmissionSig($headers['PAYPAL-TRANSMISSION-SIG']);
$signatureVerification->setTransmissionTime($headers['PAYPAL-TRANSMISSION-TIME']); $signatureVerification->setTransmissionTime($headers['PAYPAL-TRANSMISSION-TIME']);
$webhookEvent = new WebhookEvent(); $signatureVerification->setRequestBody($requestBody);
$webhookEvent->fromJson($requestBody);
$signatureVerification->setWebhookEvent($webhookEvent);
$request = clone $signatureVerification; $request = clone $signatureVerification;
try { try {