1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file implements Subscription Charge Completed. |
4
|
|
|
* |
5
|
|
|
* @author Bilal Gultekin <[email protected]> |
6
|
|
|
* @author Justin Hartman <[email protected]> |
7
|
|
|
* @copyright 2019 22 Digital |
8
|
|
|
* @license MIT |
9
|
|
|
* @since v0.1 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace TwentyTwoDigital\CashierFastspring\Listeners; |
13
|
|
|
|
14
|
|
|
use Carbon\Carbon; |
15
|
|
|
use Illuminate\Support\Str; |
16
|
|
|
use TwentyTwoDigital\CashierFastspring\Events; |
17
|
|
|
use TwentyTwoDigital\CashierFastspring\Invoice; |
18
|
|
|
use TwentyTwoDigital\CashierFastspring\Subscription; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This class is a listener for subscription charge completed events. |
22
|
|
|
* It updates or creates related order model so that you can show payment |
23
|
|
|
* and bill details to your customers. |
24
|
|
|
* |
25
|
|
|
* IMPORTANT: This class handles expansion enabled webhooks. |
26
|
|
|
* |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
class SubscriptionChargeCompleted extends Base |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Create the event listener. |
33
|
|
|
* |
34
|
|
|
* @return null |
35
|
|
|
*/ |
36
|
1 |
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
// |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Handle the event. |
43
|
|
|
* |
44
|
|
|
* @param \TwentyTwoDigital\CashierFastspring\Events\SubscriptionChargeCompleted $event |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
1 |
|
public function handle(Events\SubscriptionChargeCompleted $event) |
49
|
|
|
{ |
50
|
|
|
// when subscription charge completed event is triggered |
51
|
|
|
// try to find that order on the database |
52
|
|
|
// if not exists then create one |
53
|
1 |
|
$data = $event->data; |
54
|
|
|
|
55
|
1 |
|
$invoice = Invoice::firstOrNew([ |
56
|
1 |
|
'fastspring_id' => $data['order']['id'], |
57
|
1 |
|
'type' => 'subscription', |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
// retrieve subscription to change state of it |
61
|
1 |
|
$subscription = Subscription::where('fastspring_id', $data['subscription']['id'])->first(); |
62
|
|
|
|
63
|
|
|
// unfortunately fastspring does not provide subscription |
64
|
|
|
// dates with this event event their doc says it provides |
65
|
|
|
// we need to calculate ourselves |
66
|
1 |
|
$nextDate = Carbon::createFromTimestampUTC($data['subscription']['nextInSeconds']); |
67
|
1 |
|
$periodEndDate = $nextDate->subDay()->format('Y-m-d H:i:s'); |
68
|
|
|
|
69
|
|
|
// yeap, weird way |
70
|
1 |
|
$methodName = 'sub' . Str::title($subscription->interval_unit) . 'sNoOverflow'; |
71
|
1 |
|
$periodStartDate = $nextDate->$methodName($subscription->interval_length)->addDay()->format('Y-m-d H:i:s'); |
72
|
|
|
|
73
|
|
|
// fill the model |
74
|
1 |
|
$invoice->subscription_sequence = $data['subscription']['sequence']; |
75
|
1 |
|
$invoice->user_id = $this->getUserByFastspringId($data['account']['id'])->id; |
76
|
1 |
|
$invoice->subscription_display = $data['subscription']['display']; |
77
|
1 |
|
$invoice->subscription_product = $data['subscription']['product']; |
78
|
1 |
|
$invoice->invoice_url = $data['order']['invoiceUrl']; |
79
|
1 |
|
$invoice->total = $data['order']['total']; |
80
|
1 |
|
$invoice->tax = $data['order']['tax']; |
81
|
1 |
|
$invoice->subtotal = $data['order']['subtotal']; |
82
|
1 |
|
$invoice->discount = $data['order']['discount']; |
83
|
1 |
|
$invoice->currency = $data['order']['currency']; |
84
|
1 |
|
$invoice->payment_type = $data['order']['payment']['type']; |
85
|
1 |
|
$invoice->completed = $data['order']['completed']; |
86
|
1 |
|
$invoice->subscription_period_start_date = $periodStartDate; |
87
|
1 |
|
$invoice->subscription_period_end_date = $periodEndDate; |
88
|
|
|
|
89
|
|
|
// and save |
90
|
1 |
|
$invoice->save(); |
91
|
|
|
|
92
|
1 |
|
if ($subscription) { |
93
|
1 |
|
$subscription->state = 'active'; |
94
|
1 |
|
$subscription->save(); |
95
|
|
|
} |
96
|
1 |
|
} |
97
|
|
|
} |
98
|
|
|
|