|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Stripe; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
|
|
8
|
|
|
class GetCurrentSubscription extends Controller |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Handle the incoming request. |
|
12
|
|
|
* |
|
13
|
|
|
* @return array |
|
14
|
|
|
*/ |
|
15
|
|
|
public function __invoke(Request $request) |
|
16
|
|
|
{ |
|
17
|
|
|
$user = auth()->user(); |
|
18
|
|
|
$data = []; |
|
19
|
|
|
$data['has_payment_method'] = $user->hasDefaultPaymentMethod(); |
|
20
|
|
|
if ($user->subscribed('default')) { |
|
21
|
|
|
$subscription = $user->subscription(); |
|
22
|
|
|
|
|
23
|
|
|
$data['subscribed'] = $subscription->stripe_status != 'canceled'; |
|
24
|
|
|
|
|
25
|
|
|
$data['plan_id'] = $subscription->stripe_price; |
|
26
|
|
|
$data['trial_end'] = null; |
|
27
|
|
|
|
|
28
|
|
|
try { |
|
29
|
|
|
\Stripe\Stripe::setApiKey(\Config::get('services.stripe.secret')); |
|
30
|
|
|
$stripeSub = \Stripe\Subscription::retrieve($subscription->stripe_id); |
|
31
|
|
|
|
|
32
|
|
|
$data['trial_end'] = date('F j, Y', $stripeSub->trial_end); |
|
33
|
|
|
|
|
34
|
|
|
$finalPrice = $price = $stripeSub->plan->amount; |
|
35
|
|
|
$discountAmount = 0; |
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
|
|
$coupon = $stripeSub->discount->coupon; |
|
39
|
|
|
// check the type of coupon - it can be 'fixed_amount' or 'percent_off' |
|
40
|
|
|
if ($coupon->amount_off) { |
|
|
|
|
|
|
41
|
|
|
$discountAmount = $coupon->amount_off; |
|
42
|
|
|
$finalPrice = $price - $discountAmount; |
|
43
|
|
|
} elseif ($coupon->percent_off) { |
|
44
|
|
|
$discountAmount = ($price * $coupon->percent_off / 100); |
|
45
|
|
|
$finalPrice = $price - $discountAmount; |
|
46
|
|
|
} |
|
47
|
|
|
} catch (\Exception) { |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$data['final_price'] = $finalPrice; |
|
51
|
|
|
$data['discount_amount'] = $discountAmount; |
|
52
|
|
|
} catch (Exception) { |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} else { |
|
55
|
|
|
$data['subscribed'] = false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $data; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: