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

Update MemberPress subscription gateway on payment method changes. #24

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 67 additions & 44 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
\add_filter( 'pronamic_payment_redirect_url_memberpress_transaction', [ $this, 'redirect_url' ], 10, 2 );
\add_action( 'pronamic_payment_status_update_memberpress_transaction', [ $this, 'status_update' ], 10, 1 );

\add_action( 'pronamic_payment_status_update', [ $this, 'maybe_update_memberpress_subscription_gateway' ], 10, 1 );
\add_action( 'pronamic_pay_new_payment', [ $this, 'maybe_create_memberpress_transaction' ], 10, 1 );
\add_action( 'pronamic_pay_update_payment', [ $this, 'maybe_record_memberpress_transaction_refund' ], 10, 1 );

Expand Down Expand Up @@ -207,6 +208,71 @@
return $url;
}

/**
* Maybe update MemberPress subscription gateway for the Pronamic payment.
*
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
* @param Payment $payment Payment.
* @return void
* @throws \Exception Throws an exception when the MemberPress subscription cannot be found.
*/
public function maybe_update_memberpress_subscription_gateway( Payment $payment ) {
if ( 'subscription_payment_method_change' !== $payment->get_source() ) {
return;
}

if ( PaymentStatus::SUCCESS !== $payment->get_status() ) {
return;
}

$pronamic_subscriptions = $payment->get_subscriptions();

foreach ( $pronamic_subscriptions as $pronamic_subscription ) {
$memberpress_subscription_id = $pronamic_subscription->get_source_id();

$memberpress_subscription = MemberPress::get_subscription_by_id( $memberpress_subscription_id );

if ( null === $memberpress_subscription ) {
throw new \Exception(
\sprintf(
'Could not find MemberPress subscription with ID: %s.',
\esc_html( (string) $memberpress_subscription_id )
)
);
}

/**
* If the payment method is changed we have to update the MemberPress
* subscription.
*
* @link https://github.com/wp-pay-extensions/memberpress/commit/3631bcb24f376fb637c1317e15f540cb1f9136f4#diff-6f62438f6bf291e85f644dbdbb14b2a71a9a7ed205b01ce44290ed85abe2aa07L259-L290
*/
$memberpress_gateways = MeprOptions::fetch()->payment_methods();

foreach ( $memberpress_gateways as $memberpress_gateway ) {
if ( ! $memberpress_gateway instanceof Gateway ) {
continue;
}

if ( null === $memberpress_gateway->get_payment_method() ) {
$memberpress_subscription->gateway = $memberpress_gateway->id;
}

if ( $payment->get_payment_method() === $memberpress_gateway->get_payment_method() ) {
$memberpress_subscription->gateway = $memberpress_gateway->id;

break;
}
}

/**
* Store the MemberPress subscription in case of gateway changes.
*/
$memberpress_subscription->store();
}
}

/**
* Maybe create MemberPress transaction for the Pronamic payment.
*
Expand Down Expand Up @@ -250,44 +316,6 @@
);
}

/**
* If the payment method is changed we have to update the MemberPress
* subscription.
*
* @link https://github.com/wp-pay-extensions/memberpress/commit/3631bcb24f376fb637c1317e15f540cb1f9136f4#diff-6f62438f6bf291e85f644dbdbb14b2a71a9a7ed205b01ce44290ed85abe2aa07L259-L290
*/
$memberpress_gateways = MeprOptions::fetch()->payment_methods();

foreach ( $memberpress_gateways as $memberpress_gateway ) {
if ( ! $memberpress_gateway instanceof Gateway ) {
continue;
}

if ( $memberpress_gateway->get_payment_method() === $payment->get_payment_method() ) {
$memberpress_subscription->gateway = $memberpress_gateway->id;
}
}

/**
* Payment method.
*
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
*/
$memberpress_gateway = $memberpress_subscription->payment_method();

if ( ! $memberpress_gateway instanceof Gateway ) {
return;
}

/**
* At this point we should call `MeprBaseRealGateway->record_subscription_payment`.
*
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
* @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L205-L255
*/
$memberpress_gateway->record_subscription_payment();

$memberpress_transaction = new MeprTransaction();

$memberpress_transaction->user_id = $memberpress_subscription->user_id;
Expand All @@ -297,7 +325,7 @@
$memberpress_transaction->coupon_id = $memberpress_subscription->coupon_id;
$memberpress_transaction->trans_num = $payment->get_transaction_id();
$memberpress_transaction->subscription_id = $memberpress_subscription->id;
$memberpress_transaction->gateway = $memberpress_gateway->id;
$memberpress_transaction->gateway = $memberpress_subscription->gateway;

$periods = $payment->get_periods();

Expand All @@ -322,11 +350,6 @@

$memberpress_transaction->store();

/**
* Store the MemberPress subscription in case of gateway changes.
*/
$memberpress_subscription->store();

/**
* Update payment source.
*
Expand Down Expand Up @@ -676,7 +699,7 @@
'pronamic_subscription_id' => (string) $subscription->get_id(),
'pronamic_subscription_cancel_url' => $subscription->get_cancel_url(),
'pronamic_subscription_renewal_url' => $subscription->get_renewal_url(),
'pronamic_subscription_renewal_date' => null === $next_payment_date ? '' : \date_i18n( \get_option( 'date_format' ), $next_payment_date->getTimestamp() ),

Check failure on line 702 in src/Extension.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Parameter #1 $format of function date_i18n expects string, mixed given.
]
);
}
Expand Down
Loading