Merge pull request #807 from paypal/allow-request-body-on-validate-webhook-event

Add request body for VerifyWebhookSignature
This commit is contained in:
Jay
2017-03-23 15:16:15 -05:00
committed by GitHub
3 changed files with 62 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.
*
* @deprecated Please use setRequestBody($request_body) instead.
* @param \PayPal\Api\WebhookEvent $webhook_event
*
* @return $this
@@ -187,6 +187,29 @@ class VerifyWebhookSignature extends PayPalResourceModel
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.
*
@@ -197,6 +220,7 @@ class VerifyWebhookSignature extends PayPalResourceModel
public function post($apiContext = null, $restCall = null)
{
$payLoad = $this->toJSON();
$json = self::executeCall(
"/v1/notifications/verify-webhook-signature",
"POST",
@@ -210,4 +234,23 @@ class VerifyWebhookSignature extends PayPalResourceModel
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->setTransmissionTime($headers['PAYPAL-TRANSMISSION-TIME']);
$webhookEvent = new WebhookEvent();
$webhookEvent->fromJson($requestBody);
$signatureVerification->setWebhookEvent($webhookEvent);
$signatureVerification->setRequestBody($requestBody);
$request = clone $signatureVerification;
try {

View File

@@ -77,6 +77,15 @@ class VerifyWebhookSignatureTest extends \PHPUnit_Framework_TestCase
$obj = new VerifyWebhookSignature();
$obj->setCertUrl(null);
}
public function testToJsonToIncludeRequestBodyAsWebhookEvent() {
$obj = new VerifyWebhookSignature();
$requestBody = '{"id":"123", "links": [], "something": {}}';
$obj->setRequestBody($requestBody);
$this->assertEquals($obj->toJSON(), '{"webhook_event": ' . $requestBody .'}');
}
/**
* @dataProvider mockProvider
* @param VerifyWebhookSignature $obj