pronamic / wp-pronamic-pay

The Pronamic Pay plugin allows you to easily accept payments with payment methods like credit card, iDEAL, Bancontact and Sofort through a variety of payment providers on your WordPress website.
https://pronamicpay.com
35 stars 14 forks source link

Recurring payments create duplicate subscriptions #256

Closed rvdsteege closed 3 years ago

rvdsteege commented 3 years ago

A new recurring payment creates a duplicate subscription.

Should we use $subscription->save() instead of $this->plugin->subscriptions_data_store->create( $subscription )?

remcotolsma commented 3 years ago

I suggest we introduce two new actions:

We use these like this:

\add_action( 'pronamic_pay_pre_create_payment', function( $payment ) {
    // Complement payment.
    self::complement_payment( $payment );
} );

https://github.com/pronamic/wp-pay-core/blob/76f2987512bb6dd93949cce4054c872018f43f85/src/Plugin.php#L1011-L1012

\add_action( 'pronamic_pay_pre_create_subscription', function( $subscription ) {
    SubscriptionHelper::complement_subscription( $subscription );
    SubscriptionHelper::complement_subscription_dates( $subscription );
} );

https://github.com/pronamic/wp-pay-core/blob/3d56e5835e79a4801e2b8fb8c62d3dfd54261df9/src/Subscriptions/SubscriptionsModule.php#L138-L141

We only have to figure out where and how to call this:

SubscriptionHelper::complement_subscription_by_payment( $subscription, $payment );

Maybe something like this:

\add_action( 'pronamic_pay_pre_create_payment', function( $payment ) {
    foreach ( $payment->get_subscriptions() as $subscription ) {
        if ( $subscription->is_first_payment( $payment ) ) {
            SubscriptionHelper::complement_subscription_by_payment( $subscription, $payment );
        }
    }
} );

We should remove all calls like:

$this->plugin->subscriptions_data_store->create( $subscription ); // $subscription->save()
$pronamic_ideal->payments_data_store->create( $payment ); // $payment->save();

@rvdsteege Thoughts?