diff --git a/src/Subscription.php b/src/Subscription.php index 160de974..ab2da808 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -6,6 +6,7 @@ use LogicException; use DateTimeInterface; use Illuminate\Database\Eloquent\Model; +use Stripe\Subscription as StripeSubscription; use Laravel\Cashier\Exceptions\IncompletePayment; class Subscription extends Model @@ -552,20 +553,31 @@ public function syncTaxPercentage() $subscription->save(); } + /** + * Get the latest payment for a Subscription. + * + * @return \Laravel\Cashier\Payment + */ + public function latestPayment() + { + return new Payment( + $this->asStripeSubscription(['latest_invoice.payment_intent']) + ->latest_invoice + ->payment_intent + ); + } + /** * Get the subscription as a Stripe subscription object. * + * @param array $expand * @return \Stripe\Subscription - * @throws \LogicException */ - public function asStripeSubscription() + public function asStripeSubscription(array $expand = []) { - $subscriptions = $this->user->asStripeCustomer()->subscriptions; - - if (! $subscriptions) { - throw new LogicException('The Stripe customer does not have any subscriptions.'); - } - - return $subscriptions->retrieve($this->stripe_id); + return StripeSubscription::retrieve( + ['id' => $this->stripe_id, 'expand' => $expand], + Cashier::stripeOptions() + ); } } diff --git a/tests/Integration/SubscriptionsTest.php b/tests/Integration/SubscriptionsTest.php index 1d49ecb1..d768bde0 100644 --- a/tests/Integration/SubscriptionsTest.php +++ b/tests/Integration/SubscriptionsTest.php @@ -8,6 +8,7 @@ use Stripe\Coupon; use Stripe\Product; use Illuminate\Support\Str; +use Laravel\Cashier\Payment; use Laravel\Cashier\Subscription; use Laravel\Cashier\Exceptions\PaymentFailure; use Laravel\Cashier\Exceptions\PaymentActionRequired; @@ -313,7 +314,7 @@ public function test_downgrade_with_3d_secure_does_not_incomplete_subscription() $subscription = $user->newSubscription('main', static::$premiumPlanId)->create('pm_card_visa'); // Set a card that requires a next action as the customer's default payment method. - $user->updateDefaultPaymentMethod('pm_card_chargeCustomerFail'); + $user->updateDefaultPaymentMethod('pm_card_threeDSecure2Required'); // Attempt to swap and pay with a faulty card. $subscription = $subscription->swap(static::$planId); @@ -556,4 +557,20 @@ public function test_subscription_state_scopes() $this->assertTrue($user->subscriptions()->notOnGracePeriod()->exists()); $this->assertTrue($user->subscriptions()->ended()->exists()); } + + public function test_retrieve_the_latest_payment_for_a_subscription() + { + $user = $this->createCustomer('retrieve_the_latest_payment_for_a_subscription'); + + try { + $user->newSubscription('main', static::$planId)->create('pm_card_threeDSecure2Required'); + + $this->fail('Expected exception '.PaymentActionRequired::class.' was not thrown.'); + } catch (PaymentActionRequired $e) { + $subscription = $user->refresh()->subscription('main'); + + $this->assertInstanceOf(Payment::class, $payment = $subscription->latestPayment()); + $this->assertTrue($payment->requiresAction()); + } + } }