Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for payment method updates. #23

Merged
merged 8 commits into from
Jul 22, 2024
111 changes: 108 additions & 3 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
use Pronamic\WordPress\DateTime\DateTime;
use Pronamic\WordPress\DateTime\DateTimeImmutable;
use Pronamic\WordPress\Pay\AbstractPluginIntegration;
use Pronamic\WordPress\Pay\Core\PaymentMethods;
use Pronamic\WordPress\Pay\Payments\PaymentStatus as Core_PaymentStatus;
use Pronamic\WordPress\Pay\Payments\Payment;
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus as Core_SubscriptionStatus;
use RCP_Membership;
use RCP_Payment_Gateways;
use RCP_Payments;
use WP_Query;

/**
* Extension class
*
*
* @link https://plugins.trac.wordpress.org/browser/restrict-content/tags/3.2.10/core/includes/gateways/class-rcp-payment-gateways.php#L47
* @phpstan-type RestrictContentProGatewayRegistration array{label: string, admin_label: string, class: class-string}
* @phpstan-type RestrictContentProPaymentObject object{id: int, membership_id: int, status: string}
Expand Down Expand Up @@ -85,6 +88,13 @@
\add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ], 5 );

\add_action( 'rcp_after_membership_admin_update', [ $this, 'rcp_after_membership_admin_update' ] );

/*
* Filter subscription details actions HTML.
*
* @link https://gitlab.com/pronamic-plugins/restrict-content-pro/-/blob/3.4.4/templates/subscription.php#L156-164
*/
\add_filter( 'rcp_subscription_details_actions', [ $this, 'rcp_subscription_details_actions' ], 10, 4 );
}

/**
Expand Down Expand Up @@ -128,6 +138,8 @@

\add_filter( 'rcp_gateway_subscription_id_url', [ $this, 'rcp_gateway_subscription_id_url' ], 10, 3 );

\add_action( 'save_post_pronamic_pay_subscr', [ $this, 'maybe_update_membership_gateway' ] );

/**
* Filter the subscription next payment delivery date.
*
Expand Down Expand Up @@ -215,6 +227,7 @@
'pronamic_pay_bancontact' => Gateways\BancontactGateway::class,
'pronamic_pay_banktransfer' => Gateways\BankTransferGateway::class,
'pronamic_pay_bitcoin' => Gateways\BitcoinGateway::class,
'pronamic_pay_card' => Gateways\CardGateway::class,
'pronamic_pay_credit_card' => Gateways\CreditCardGateway::class,
'pronamic_pay_direct_debit' => Gateways\DirectDebitGateway::class,
'pronamic_pay_direct_debit_bancontact' => Gateways\DirectDebitBancontactGateway::class,
Expand Down Expand Up @@ -599,6 +612,98 @@
return true;
}

/**
* Subscription action links HTML.
*
* @param string $actions Formatted HTML links.
* @param array<string> $links Links.
* @param int $user_id Current user ID.
* @param RCP_Membership $rcp_membership Membership object.
* @return string
*/
public function rcp_subscription_details_actions( $actions, $links, $user_id, $rcp_membership ) {
$subscriptions = \get_pronamic_subscriptions_by_source( 'rcp_membership', $rcp_membership->get_id() );

Check failure on line 625 in src/Extension.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Parameter #2 $source_id of function get_pronamic_subscriptions_by_source expects string|null, int given.

if ( 0 === count( $subscriptions ) ) {
return $actions;
}

$subscription = reset( $subscriptions );

// Payment method can only be updated for active subscription.
if ( Core_SubscriptionStatus::ACTIVE !== $subscription->get_status() ) {
return $actions;
}

$action = \sprintf(
'<a href="%1$s" title="%2$s"><button type="button">%3$s</button></a>',
\esc_url( $subscription->get_mandate_selection_url() ),
\esc_attr( \__( 'Update payment method', 'pronamic_ideal' ) ),
\esc_html( \__( 'Update payment method', 'pronamic_ideal' ) )
);

$actions = \sprintf( '%s<br/>%s', $action, $actions );

return $actions;
}

/**
* Maybe update membership gateway on subscription updates.
*
* @param int $post_id Subscription post ID.
* @return void
*/
public function maybe_update_membership_gateway( $post_id ) {
$subscription = \get_pronamic_subscription( $post_id );

if ( null === $subscription ) {
return;
}

if ( 'rcp_membership' !== $subscription->get_source() ) {
return;
}

$rcp_membership = \rcp_get_membership( (int) $subscription->get_source_id() );

if ( false === $rcp_membership ) {
return;
}

/**
* Update membership gateway.
*/
$rcp_gateways = new RCP_Payment_Gateways();

foreach ( $rcp_gateways->available_gateways as $gateway_id => $gateway ) {
if ( ! \array_key_exists( 'class', $gateway ) ) {
continue;
}

$rcp_gateway = new $gateway['class']();

if ( ! \method_exists( $rcp_gateway, 'get_pronamic_payment_method' ) ) {
continue;
}

if ( $rcp_gateway->get_pronamic_payment_method() !== $subscription->get_payment_method() ) {
continue;
}

if ( $rcp_membership->get_gateway() === $gateway_id ) {
break;
}

$rcp_membership->update(
[
'gateway' => $gateway_id,
]
);

break;
}
}

/**
* Source column
*
Expand Down Expand Up @@ -980,7 +1085,7 @@

/**
* Restrict Content Pro after membership admin update.
*
*
* @link https://plugins.trac.wordpress.org/browser/restrict-content/tags/3.2.10/core/includes/admin/memberships/membership-actions.php#L371
* @param RCP_Membership $rcp_membership Restrict Content Pro membership object.
* @return void
Expand All @@ -999,7 +1104,7 @@

/**
* Restrict Content Pro gateway subscription ID URL.
*
*
* @param string $url URL.
* @param string $gateway Payment gateway slug.
* @param string $subscription_id ID of the subscription in the gateway.
Expand Down
36 changes: 36 additions & 0 deletions src/Gateways/CardGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Card gateway
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2023 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Extensions\RestrictContent
*/

namespace Pronamic\WordPress\Pay\Extensions\RestrictContent\Gateways;

use Pronamic\WordPress\Pay\Core\PaymentMethods;

/**
* Card gateway
*
* @author Reüel van der Steege
* @version 4.6.0
* @since 4.6.0
*/
class CardGateway extends Gateway {
/**
* Gateway id.
*
* @var string
*/
protected $id = 'pronamic_pay_card';

/**
* Payment method.
*
* @var string
*/
protected $payment_method = PaymentMethods::CARD;
}
27 changes: 20 additions & 7 deletions src/Gateways/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Gateway extends RCP_Payment_Gateway {
/**
* Payment method
*
* @var string
* @var string|null
*/
protected $payment_method;

Expand All @@ -47,21 +47,21 @@ public function init() {
// Set supported features based on gateway.
$gateway = $this->get_pronamic_gateway();

if ( null !== $gateway ) {
if ( null !== $gateway && null !== $this->payment_method ) {
$payment_method = $gateway->get_payment_method( $this->payment_method );

if ( null !== $payment_method && $payment_method->supports( 'recurring' ) ) {
$this->supports = [
/**
* Price changes.
*
*
* @link https://github.com/pronamic/wp-pronamic-pay-restrict-content-pro/issues/19
*/
'price-changes',
'recurring',
/**
* Renewal date changes.
*
*
* @link https://github.com/pronamic/wp-pronamic-pay-restrict-content-pro/issues/17
* @link https://github.com/pronamic/wp-pronamic-pay-restrict-content-pro/pull/18#issuecomment-2107023059
* @link https://plugins.trac.wordpress.org/browser/restrict-content/tags/3.2.10/core/includes/memberships/class-rcp-membership.php#L3454
Expand All @@ -73,6 +73,15 @@ public function init() {
}
}

/**
* Get payment method.
*
* @return string|null
*/
public function get_pronamic_payment_method() {
return $this->payment_method;
}

/**
* Get the Pronamic configuration ID for this gateway.
*
Expand Down Expand Up @@ -103,7 +112,7 @@ protected function get_pronamic_config_id() {

/**
* Get the Pronamic gateway.
*
*
* @return PronamicGateway|null
*/
private function get_pronamic_gateway() {
Expand All @@ -112,7 +121,7 @@ private function get_pronamic_gateway() {
if ( null === $config_id ) {
return null;
}

if ( '' === $config_id ) {
return null;
}
Expand Down Expand Up @@ -251,6 +260,10 @@ public function fields() {
return '';
}

if ( null === $this->payment_method ) {
return '';
}

$payment_method = $gateway->get_payment_method( $this->payment_method );

if ( null === $payment_method ) {
Expand All @@ -276,7 +289,7 @@ public function fields() {

/**
* Process signup.
*
*
* @return void
* @throws \Exception Throws an exception if the Restrict Content data does not meet expectations.
*/
Expand Down
Loading