Skip already handled refunds in webhook

When we add a refund from WC, we still receive a webhook with it. And for some partial refunds the duplicated refund can be added in WC.
This commit is contained in:
Alex P 2022-04-28 16:20:05 +03:00
parent 13e04b009c
commit 35f058870a
5 changed files with 74 additions and 10 deletions

View file

@ -198,11 +198,11 @@ class PaymentsEndpoint {
*
* @param Refund $refund The refund to be processed.
*
* @return void
* @return string Refund ID.
* @throws RuntimeException If the request fails.
* @throws PayPalApiException If the request fails.
*/
public function refund( Refund $refund ) : void {
public function refund( Refund $refund ) : string {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/payments/captures/' . $refund->for_capture()->id() . '/refund';
$args = array(
@ -223,12 +223,15 @@ class PaymentsEndpoint {
}
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 201 !== $status_code ) {
$json = json_decode( $response['body'] );
if ( 201 !== $status_code || ! is_object( $json ) ) {
throw new PayPalApiException(
$json,
$status_code
);
}
return $json->id;
}
/**