Fixed Stripe payment Handler doesn't handle recurring payments correctly

K a M a L

Well-known member
Affected version
2.1.10
Hello , we are trying to enable stripe to handle recurring payments on our forum but seems Xenforo implementation of stripe subscriptions is broken .

  • Xenforo creates customer before getting card token .. resulting in customer with no attached payment method .. it doesn't try to update the customer and attach payment method .
  • Xenforo creates a fake trial subscription that ends by subscription end date.
$subscription = \Stripe\Subscription::create([ 'customer' => $customer->id, 'items' => [ ['plan' => $plan->id] ], 'trial_end' => strtotime("+ {$purchase->lengthAmount} {$purchase->lengthUnit}"), 'metadata' => $this->getTransactionMetadata($purchase) ]);

and charges one-off payment instead of charging the fake trial subscription which is left with no attached payment method and cannot be charged for future payments.

"object": "subscription", "application_fee_percent": null, "billing": "charge_automatically", "billing_cycle_anchor": 1627926814, "billing_thresholds": null, "cancel_at": null, "cancel_at_period_end": false, "canceled_at": null, "collection_method": "charge_automatically", "created": 1596390814, "current_period_end": 1627926814, "current_period_start": 1596390814, "customer": "cus_HlHxfROc90hpUo", "days_until_due": null, "default_payment_method": null, "default_source": null, "default_tax_rates": [ ],

The recurring payment integration looks like a scam and completely done in the wrong way
 
You've listed the affected version as 2.1.x but this isn't helpful. The Stripe implementation changed in the middle of 2.1.x so knowing the specific version is crucial.
 
In my recent testing subscriptions are working fine but we will look into it.
For me it works only in first cycle , then stripe can't charge the customer because no payment method attached .. I will double check also with a fresh setup and let you know .
 
@Chris D
I've Just done some more testing and I think I found the bug
when Xenforo receives the one-off charge .. it tries to to update customer and subscription and attach payment method ( I was wrong on this )

try { /** @var \Stripe\Subscription $subscription */ $subscription = \Stripe\Subscription::retrieve( $purchaseRequest->provider_metadata ); /** @var \Stripe\PaymentMethod $paymentMethod */ $paymentMethod = \Stripe\PaymentMethod::retrieve( $state->event['payment_method'] ); \Stripe\Subscription::update($subscription->id, [ 'default_payment_method' => $paymentMethod->id ]); } catch (\Stripe\Exception\ExceptionInterface $e) {}

when removing the empty catch block , I see this in error logs


Stripe\Exception\AuthenticationException: No API key provided. (HINT: set your API key using "Stripe::setApiKey(<API-KEY>)". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email support@stripe.com if you have any questions. src\vendor\stripe\stripe-php\lib\ApiRequestor.php:321 Generated by: Unknown account Aug 3, 2020 at 2:10 PM Stack trace #0 src\vendor\stripe\stripe-php\lib\ApiRequestor.php(117): Stripe\ApiRequestor->_requestRaw('get', '/v1/subscriptio...', Array, Array) #1 src\vendor\stripe\stripe-php\lib\ApiResource.php(62): Stripe\ApiRequestor->request('get', '/v1/subscriptio...', Array, Array) #2 src\vendor\stripe\stripe-php\lib\ApiOperations\Retrieve.php(26): Stripe\ApiResource->refresh() #3 src\XF\Payment\Stripe.php(783): Stripe\Subscription::retrieve('sub_Hlabrx6JxqT...') #4 payment_callback.php(62): XF\Payment\Stripe->getPaymentResult(Object(XF\Payment\CallbackState)) #5 {main}

Think this explains what is going on
 
If you add:

PHP:
$this->setupStripe($state->paymentProfile);

Above the try does that solve the problem in your testing?
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.0 Beta 2).

Change log:
Ensure we set up the Stripe API correctly before attempting to associate a payment method with a subscription.
There may be a delay before changes are rolled out to the XenForo Community.
 
Ahhh sorry, I didn't check what changes you'd actually made. Assumed you'd made changes to the free trial setup.
 
Top Bottom