Use CHECKOUT.ORDER.APPROVED for simulation

Not sure if it matters which event we use, but at least we are subscribed to this one
This commit is contained in:
Alex P 2021-09-24 17:36:51 +03:00
parent 07b9147174
commit 8c7baac7f9
3 changed files with 28 additions and 14 deletions

View file

@ -204,28 +204,31 @@ class WebhookEndpoint {
/**
* Request a simulated webhook to be sent.
*
* @param Webhook $hook The webhook subscription to use.
* @param string $event_type The event type, such as CHECKOUT.ORDER.APPROVED.
* @param Webhook $hook The webhook subscription to use.
* @param string $event_type The event type, such as CHECKOUT.ORDER.APPROVED.
* @param string|null $resource_version The event resource version, such as 2.0.
*
* @return WebhookEvent
* @throws RuntimeException If the request fails.
* @throws PayPalApiException If the request fails.
*/
public function simulate( Webhook $hook, string $event_type ): WebhookEvent {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/notifications/simulate-event';
public function simulate( Webhook $hook, string $event_type, ?string $resource_version ): WebhookEvent {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/notifications/simulate-event';
$data = array(
'webhook_id' => $hook->id(),
'event_type' => $event_type,
);
if ( $resource_version ) {
$data['resource_version'] = $resource_version;
}
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
),
'body' => wp_json_encode(
array(
'webhook_id' => $hook->id(),
'event_type' => $event_type,
)
),
'body' => wp_json_encode( $data ),
);
$response = $this->request( $url, $args );

View file

@ -145,7 +145,8 @@ return array(
return new WebhookSimulation(
$webhook_endpoint,
$webhook,
'PAYMENT.AUTHORIZATION.CREATED'
'CHECKOUT.ORDER.APPROVED',
'2.0'
);
},

View file

@ -46,21 +46,31 @@ class WebhookSimulation {
*/
private $event_type;
/**
* The event resource version, such as 2.0.
*
* @var string|null
*/
private $resource_version;
/**
* WebhookSimulation constructor.
*
* @param WebhookEndpoint $webhook_endpoint The webhooks endpoint.
* @param Webhook|null $webhook Our registered webhook.
* @param string $event_type The event type that will be simulated, such as CHECKOUT.ORDER.APPROVED.
* @param string|null $resource_version The event resource version, such as 2.0.
*/
public function __construct(
WebhookEndpoint $webhook_endpoint,
?Webhook $webhook,
string $event_type
string $event_type,
?string $resource_version
) {
$this->webhook_endpoint = $webhook_endpoint;
$this->webhook = $webhook;
$this->event_type = $event_type;
$this->resource_version = $resource_version;
}
/**
@ -73,7 +83,7 @@ class WebhookSimulation {
throw new Exception( 'Webhooks not registered' );
}
$event = $this->webhook_endpoint->simulate( $this->webhook, $this->event_type );
$event = $this->webhook_endpoint->simulate( $this->webhook, $this->event_type, $this->resource_version );
$this->save(
array(