1 | <?php |
||||||
2 | |||||||
3 | namespace Bgultekin\CashierFastspring; |
||||||
4 | |||||||
5 | use Bgultekin\CashierFastspring\Fastspring\Fastspring; |
||||||
6 | use GuzzleHttp\Exception\ClientException; |
||||||
7 | |||||||
8 | class SubscriptionBuilder |
||||||
9 | { |
||||||
10 | /** |
||||||
11 | * The model that is subscribing. |
||||||
12 | * |
||||||
13 | * @var \Illuminate\Database\Eloquent\Model |
||||||
14 | */ |
||||||
15 | protected $owner; |
||||||
16 | |||||||
17 | /** |
||||||
18 | * The name of the subscription. |
||||||
19 | * |
||||||
20 | * @var string |
||||||
21 | */ |
||||||
22 | protected $name; |
||||||
23 | |||||||
24 | /** |
||||||
25 | * The name of the plan being subscribed to. |
||||||
26 | * |
||||||
27 | * @var string |
||||||
28 | */ |
||||||
29 | protected $plan; |
||||||
30 | |||||||
31 | /** |
||||||
32 | * The quantity of the subscription. |
||||||
33 | * |
||||||
34 | * @var int |
||||||
35 | */ |
||||||
36 | protected $quantity = 1; |
||||||
37 | |||||||
38 | /** |
||||||
39 | * The coupon code being applied to the customer. |
||||||
40 | * |
||||||
41 | * @var string|null |
||||||
42 | */ |
||||||
43 | protected $coupon; |
||||||
44 | |||||||
45 | /** |
||||||
46 | * Create a new subscription builder instance. |
||||||
47 | * |
||||||
48 | * @param mixed $owner |
||||||
49 | * @param string $name |
||||||
50 | * @param string $plan |
||||||
51 | * |
||||||
52 | * @return void |
||||||
53 | */ |
||||||
54 | 5 | public function __construct($owner, $name, $plan) |
|||||
55 | { |
||||||
56 | 5 | $this->name = $name; |
|||||
57 | 5 | $this->plan = $plan; |
|||||
58 | 5 | $this->owner = $owner; |
|||||
59 | 5 | } |
|||||
60 | |||||||
61 | /** |
||||||
62 | * Specify the quantity of the subscription. |
||||||
63 | * |
||||||
64 | * @param int $quantity |
||||||
65 | * |
||||||
66 | * @return $this |
||||||
67 | */ |
||||||
68 | 1 | public function quantity($quantity) |
|||||
69 | { |
||||||
70 | 1 | $this->quantity = $quantity; |
|||||
71 | |||||||
72 | 1 | return $this; |
|||||
73 | } |
||||||
74 | |||||||
75 | /** |
||||||
76 | * The coupon to apply to a new subscription. |
||||||
77 | * |
||||||
78 | * @param string $coupon |
||||||
79 | * |
||||||
80 | * @return $this |
||||||
81 | */ |
||||||
82 | 1 | public function withCoupon($coupon) |
|||||
83 | { |
||||||
84 | 1 | $this->coupon = $coupon; |
|||||
85 | |||||||
86 | 1 | return $this; |
|||||
87 | } |
||||||
88 | |||||||
89 | /** |
||||||
90 | * Create a new Fastspring session and return it as object. |
||||||
91 | * |
||||||
92 | * @return object |
||||||
93 | */ |
||||||
94 | 4 | public function create() |
|||||
95 | { |
||||||
96 | 4 | $fastspringId = $this->getFastspringIdOfCustomer(); |
|||||
97 | |||||||
98 | 4 | return Fastspring::createSession($this->buildPayload($fastspringId)); |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
99 | } |
||||||
100 | |||||||
101 | /** |
||||||
102 | * Get the fastspring id for the current user. |
||||||
103 | * |
||||||
104 | * @return int|string |
||||||
105 | */ |
||||||
106 | 4 | protected function getFastspringIdOfCustomer() |
|||||
107 | { |
||||||
108 | 4 | if (!$this->owner->fastspring_id) { |
|||||
109 | try { |
||||||
110 | 2 | $customer = $this->owner->createAsFastspringCustomer(); |
|||||
0 ignored issues
–
show
|
|||||||
111 | 1 | } catch (ClientException $e) { |
|||||
112 | // we should get its id and save it |
||||||
113 | 1 | $response = $e->getResponse(); |
|||||
114 | 1 | $content = json_decode($response->getBody()->getContents()); |
|||||
115 | |||||||
116 | // if email key exists in error node |
||||||
117 | // then we assume this error is related to that |
||||||
118 | // there is already an account with this email in fastspring-side |
||||||
119 | // error message also returns account link but messages are easily |
||||||
120 | // changable so we can't rely on that |
||||||
121 | 1 | if (isset($content->error->email)) { |
|||||
122 | 1 | $response = Fastspring::getAccounts(['email' => $this->owner->email]); |
|||||
0 ignored issues
–
show
The method
getAccounts() does not exist on Bgultekin\CashierFastspring\Fastspring\Fastspring . Since you implemented __callStatic , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
123 | |||||||
124 | 1 | if ($response->accounts) { |
|||||
125 | 1 | $account = $response->accounts[0]; |
|||||
126 | |||||||
127 | // save it to eloquent model |
||||||
128 | 1 | $this->owner->fastspring_id = $account->id; |
|||||
129 | 1 | $this->owner->save(); |
|||||
130 | } |
||||||
131 | } else { |
||||||
132 | // if we are not sure about the exception |
||||||
133 | // then throw it again |
||||||
134 | // if returns it is yours, if doesn't it has never been yours |
||||||
135 | // (the previous line is a bad joke don't mind) |
||||||
136 | throw $e; // @codeCoverageIgnore |
||||||
137 | } |
||||||
138 | } |
||||||
139 | } |
||||||
140 | |||||||
141 | 4 | return $this->owner->fastspring_id; |
|||||
142 | } |
||||||
143 | |||||||
144 | /** |
||||||
145 | * Build the payload for session creation. |
||||||
146 | * |
||||||
147 | * @return array |
||||||
148 | */ |
||||||
149 | 4 | protected function buildPayload($fastspringId) |
|||||
150 | { |
||||||
151 | 4 | return array_filter([ |
|||||
152 | 4 | 'account' => $fastspringId, |
|||||
153 | 'items' => [ |
||||||
154 | [ |
||||||
155 | 4 | 'product' => $this->plan, |
|||||
156 | 4 | 'quantity' => $this->quantity, |
|||||
157 | ], |
||||||
158 | ], |
||||||
159 | 'tags' => [ |
||||||
160 | 4 | 'name' => $this->name, |
|||||
161 | ], |
||||||
162 | 4 | 'coupon' => $this->coupon, |
|||||
163 | ]); |
||||||
164 | } |
||||||
165 | } |
||||||
166 |