From 09a28d8304a477fdd4a0eb8cf43481dc62bce764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=C3=BCel=20van=20der=20Steege?= Date: Wed, 13 Mar 2024 11:49:38 +0100 Subject: [PATCH] Set `refunded` RCP payment status on refunds and chargebacks. --- src/Extension.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/Extension.php b/src/Extension.php index 1893a96..390af1b 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -119,6 +119,7 @@ public function plugins_loaded() { \add_filter( 'pronamic_subscription_source_text_rcp_membership', [ $this, 'subscription_source_text' ], 10, 2 ); \add_action( 'pronamic_pay_new_payment', [ $this, 'new_payment' ] ); + \add_action( 'pronamic_pay_update_payment', [ $this, 'maybe_record_restrictcontentpro_payment_refund' ], 10, 1 ); \add_action( 'rcp_edit_membership_after', [ $this, 'rcp_edit_membership_after' ] ); \add_action( 'rcp_edit_payment_after', [ $this, 'rcp_edit_payment_after' ] ); @@ -868,6 +869,52 @@ public function new_payment( Payment $payment ) { $payment->save(); } + /** + * Maybe record refund for Restrict Content Pro payment. + * + * @param Payment $payment Payment. + * @return void + */ + public function maybe_record_restrictcontentpro_payment_refund( Payment $payment ) { + if ( 'rcp_payment' !== $payment->get_source() ) { + return; + } + + $amount_refunded = $payment->get_refunded_amount(); + + $amount_charged_back = $payment->get_charged_back_amount(); + + if ( $amount_refunded->get_value() <= 0 && null === $amount_charged_back ) { + return; + } + + /** + * Find the related Restrict Content Pro payment. + * + * @link https://gitlab.com/pronamic-plugins/restrict-content-pro/blob/3.4.4/includes/class-rcp-payments.php#L312-337 + */ + $rcp_payments = new RCP_Payments(); + + $rcp_payment_id = $payment->get_source_id(); + + $rcp_payment = $rcp_payments->get_payment( $rcp_payment_id ); + + if ( null === $rcp_payment ) { + return; + } + + if ( PaymentStatus::REFUNDED === $rcp_payment->status ) { + return; + } + + $rcp_payments->update( + $rcp_payment_id, + [ + 'status' => PaymentStatus::REFUNDED, + ] + ); + } + /** * Restrict Content Pro edit membership after. *