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

[10.0] Add latestPayment method on Subscription #705

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
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
30 changes: 21 additions & 9 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
);
}
}
19 changes: 18 additions & 1 deletion tests/Integration/SubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}
}