diff --git a/README.md b/README.md index a1aec603..906e3f63 100644 --- a/README.md +++ b/README.md @@ -554,6 +554,9 @@ A new subscription was started. #### `SubscriptionCancelled` event The subscription was cancelled. +#### `SubscriptionResumed` event +The subscription was resumed. + #### `SubscriptionPlanSwapped` event The subscription plan was swapped. diff --git a/resources/views/receipt.blade.php b/resources/views/receipt.blade.php index f4ab2bf6..b726fcb2 100644 --- a/resources/views/receipt.blade.php +++ b/resources/views/receipt.blade.php @@ -53,7 +53,7 @@ {{ Laravel\Cashier\Cashier::formatAmount($item->getSubtotal()) }} - + {{ $item->getTaxPercentage() }}% diff --git a/src/Billable.php b/src/Billable.php index 0f754b43..6a159220 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -9,6 +9,7 @@ use Laravel\Cashier\Credit\Credit; use Laravel\Cashier\Events\MandateClearedFromBillable; use Laravel\Cashier\Exceptions\InvalidMandateException; +use Laravel\Cashier\Order\Invoice; use Laravel\Cashier\Order\Order; use Laravel\Cashier\Order\OrderItem; use Laravel\Cashier\Plan\Contracts\PlanRepository; @@ -374,6 +375,22 @@ public function invoices() return $this->orders->invoices(); } + /** + * Create an invoice download response. + * + * @param $orderId + * @param null|array $data + * @param string $view + * @return \Symfony\Component\HttpFoundation\Response + */ + public function downloadInvoice($orderId, $data = [], $view = Invoice::DEFAULT_VIEW) + { + /** @var Order $order */ + $order = $this->orders()->where('id', $orderId)->firstOrFail(); + + return $order->invoice()->download($data, $view); + } + /** * @return null|string */ diff --git a/src/CashierServiceProvider.php b/src/CashierServiceProvider.php index 8f1e5897..9e041fda 100644 --- a/src/CashierServiceProvider.php +++ b/src/CashierServiceProvider.php @@ -14,7 +14,7 @@ class CashierServiceProvider extends ServiceProvider { - const PACKAGE_VERSION = '1.6.0'; + const PACKAGE_VERSION = '1.7.0'; /** * Bootstrap the application services. diff --git a/src/Events/SubscriptionResumed.php b/src/Events/SubscriptionResumed.php new file mode 100644 index 00000000..a6e7feea --- /dev/null +++ b/src/Events/SubscriptionResumed.php @@ -0,0 +1,21 @@ +subscription = $subscription; + } +} diff --git a/src/FirstPayment/Actions/StartSubscription.php b/src/FirstPayment/Actions/StartSubscription.php index 928671c6..6a43ee8e 100644 --- a/src/FirstPayment/Actions/StartSubscription.php +++ b/src/FirstPayment/Actions/StartSubscription.php @@ -208,7 +208,6 @@ public function trialDays(int $trialDays) * * @param Carbon $trialUntil * @return $this - * @throws \Throwable|\Laravel\Cashier\Exceptions\PlanNotFoundException */ public function trialUntil(Carbon $trialUntil) { @@ -228,7 +227,7 @@ public function skipTrial() { $this->skipTrial = true; $this->trialUntil = null; - $this->builder->skipTrial(); + $this->builder()->skipTrial(); $this->subtotal = $this->plan->amount(); return $this; diff --git a/src/Http/Controllers/WebhookController.php b/src/Http/Controllers/WebhookController.php index 33d56ae5..7a38c716 100644 --- a/src/Http/Controllers/WebhookController.php +++ b/src/Http/Controllers/WebhookController.php @@ -49,8 +49,7 @@ protected function getOrder(Payment $payment) if(!$order) { if(isset($payment->metadata, $payment->metadata->temporary_mollie_payment_id)) { - $uuid = 'temp_' . $payment->metadata->temporary_payment_id; - $order = Order::findByPaymentId($uuid); + $order = Order::findByPaymentId($payment->metadata->temporary_mollie_payment_id); } } diff --git a/src/Order/Invoice.php b/src/Order/Invoice.php index 91400d08..fe2191ef 100644 --- a/src/Order/Invoice.php +++ b/src/Order/Invoice.php @@ -273,7 +273,7 @@ public function taxDetails() }); return [ - 'tax_percentage' => $percentage, + 'tax_percentage' => (float) $percentage, 'raw_over_subtotal' => $raw_over_subtotal, 'over_subtotal' => $this->formatAmount(money($raw_over_subtotal, $this->currency)), 'raw_total' => $raw_total, diff --git a/src/Order/Order.php b/src/Order/Order.php index f9d53da7..44d525ea 100644 --- a/src/Order/Order.php +++ b/src/Order/Order.php @@ -27,7 +27,7 @@ */ class Order extends Model { - use hasOwner; + use HasOwner; use ConvertsToMoney; /** diff --git a/src/Order/OrderItem.php b/src/Order/OrderItem.php index c510b7fa..042aec11 100644 --- a/src/Order/OrderItem.php +++ b/src/Order/OrderItem.php @@ -20,7 +20,7 @@ */ class OrderItem extends Model implements InvoicableItem { - use hasOwner; + use HasOwner; use FormatsAmount; use ConvertsToMoney; @@ -257,7 +257,7 @@ public function getSubtotal() */ public function getTaxPercentage() { - return $this->tax_percentage; + return (float) $this->tax_percentage; } /** diff --git a/src/Subscription.php b/src/Subscription.php index e10ef906..3b393a27 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; +use Laravel\Cashier\Events\SubscriptionResumed; use Laravel\Cashier\Order\Contracts\InteractsWithOrderItems; use Laravel\Cashier\Order\Contracts\PreprocessesOrderItems; use Laravel\Cashier\Coupon\AppliedCoupon; @@ -44,7 +45,7 @@ */ class Subscription extends Model implements InteractsWithOrderItems, PreprocessesOrderItems, AcceptsCoupons { - use hasOwner; + use HasOwner; /** * The attributes that are not mass assignable. @@ -321,6 +322,8 @@ public function resume() 'scheduled_order_item_id' => $item->id, ])->save(); + Event::dispatch(new SubscriptionResumed($this)); + return $this; }); } diff --git a/tests/ManageSubscriptionTest.php b/tests/ManageSubscriptionTest.php index 54054852..0062362d 100644 --- a/tests/ManageSubscriptionTest.php +++ b/tests/ManageSubscriptionTest.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Illuminate\Support\Facades\Event; use Laravel\Cashier\Cashier; +use Laravel\Cashier\Events\SubscriptionResumed; use Laravel\Cashier\Events\SubscriptionStarted; use Laravel\Cashier\Events\SubscriptionQuantityUpdated; use Laravel\Cashier\Order\OrderItem; @@ -147,9 +148,16 @@ public function canCreateDirectDebitSubscriptionForMandatedCustomer() $subscription->fill(['ends_at' => $oldGracePeriod])->save(); // Resume Subscription + Event::fake(); + $old_subscription = $subscription->fresh(); $subscription = $subscription->resume()->fresh(); + Event::assertDispatched(SubscriptionResumed::class, function (SubscriptionResumed $e) use ($subscription) { + $this->assertTrue($e->subscription->is($subscription)); + return true; + }); + $this->assertTrue($subscription->active()); $this->assertFalse($subscription->cancelled()); $this->assertFalse($subscription->onGracePeriod()); @@ -238,8 +246,15 @@ public function testCreatingSubscriptionWithTrial() $this->assertEquals(0, OrderItem::count()); // Resume Subscription + Event::fake(); + $subscription->resume(); + Event::assertDispatched(SubscriptionResumed::class, function (SubscriptionResumed $e) use ($subscription) { + $this->assertTrue($e->subscription->is($subscription)); + return true; + }); + $this->assertTrue($subscription->active()); $this->assertFalse($subscription->onGracePeriod()); $this->assertTrue($subscription->onTrial()); diff --git a/tests/SubscriptionTest.php b/tests/SubscriptionTest.php index d81a4c8b..4cd0a6be 100644 --- a/tests/SubscriptionTest.php +++ b/tests/SubscriptionTest.php @@ -3,6 +3,8 @@ namespace Laravel\Cashier\Tests; use Carbon\Carbon; +use Illuminate\Support\Facades\Event; +use Laravel\Cashier\Events\SubscriptionResumed; use Laravel\Cashier\Order\OrderItem; use Laravel\Cashier\Subscription; use Laravel\Cashier\Tests\Fixtures\User; @@ -74,7 +76,11 @@ public function cannotResumeIfNotCancelled() $this->assertFalse($subscription->cancelled()); + Event::fake(); + $subscription->resume(); + + Event::assertNotDispatched(SubscriptionResumed::class); } /** @test */ @@ -89,7 +95,11 @@ public function cannotResumeIfNotOnGracePeriod() $this->assertTrue($subscription->cancelled()); $this->assertFalse($subscription->onGracePeriod()); + Event::fake(); + $subscription->resume(); + + Event::assertNotDispatched(SubscriptionResumed::class); } /** @test */