22digital /
laravel-cashier-fastspring
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file implements Subscription Builder. |
||
| 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; |
||
| 13 | |||
| 14 | use GuzzleHttp\Exception\ClientException; |
||
| 15 | use TwentyTwoDigital\CashierFastspring\Fastspring\Fastspring; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Front-end to create subscription objects step by step. |
||
| 19 | */ |
||
| 20 | class SubscriptionBuilder |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * The model that is subscribing. |
||
| 24 | * |
||
| 25 | * @var \Illuminate\Database\Eloquent\Model |
||
| 26 | */ |
||
| 27 | protected $owner; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The name of the subscription. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The name of the plan being subscribed to. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $plan; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The quantity of the subscription. |
||
| 45 | * |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | protected $quantity = 1; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The coupon code being applied to the customer. |
||
| 52 | * |
||
| 53 | * @var string|null |
||
| 54 | */ |
||
| 55 | protected $coupon; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Create a new subscription builder instance. |
||
| 59 | * |
||
| 60 | * @param mixed $owner Owner details |
||
| 61 | * @param string $name Plan name |
||
| 62 | * @param string $plan Plan |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | 5 | public function __construct($owner, $name, $plan) |
|
| 67 | { |
||
| 68 | 5 | $this->name = $name; |
|
| 69 | 5 | $this->plan = $plan; |
|
| 70 | 5 | $this->owner = $owner; |
|
| 71 | 5 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Specify the quantity of the subscription. |
||
| 75 | * |
||
| 76 | * @param int $quantity Number of items |
||
| 77 | * |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | 1 | public function quantity($quantity) |
|
| 81 | { |
||
| 82 | 1 | $this->quantity = $quantity; |
|
| 83 | |||
| 84 | 1 | return $this; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The coupon to apply to a new subscription. |
||
| 89 | * |
||
| 90 | * @param string $coupon Coupon string to use |
||
| 91 | * |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | 1 | public function withCoupon($coupon) |
|
| 95 | { |
||
| 96 | 1 | $this->coupon = $coupon; |
|
| 97 | |||
| 98 | 1 | return $this; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Create a new Fastspring session and return it as object. |
||
| 103 | * |
||
| 104 | * @return \TwentyTwoDigital\CashierFastspring\Fastspring\Fastspring |
||
| 105 | */ |
||
| 106 | 4 | public function create() |
|
| 107 | { |
||
| 108 | 4 | $fastspringId = $this->getFastspringIdOfCustomer(); |
|
| 109 | |||
| 110 | 4 | return Fastspring::createSession($this->buildPayload($fastspringId)); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get the fastspring id for the current user. |
||
| 115 | * |
||
| 116 | * If an email key exists in error node then we assume this error is related |
||
| 117 | * to the fact there is already an account with this email in |
||
| 118 | * fastspring-side error message. It will also returns account link but |
||
| 119 | * messages are easily changable so we can't rely on that. |
||
| 120 | * |
||
| 121 | * @throws Exception |
||
| 122 | * |
||
| 123 | * @return int|string |
||
| 124 | */ |
||
| 125 | 4 | protected function getFastspringIdOfCustomer() |
|
| 126 | { |
||
| 127 | 4 | if (!$this->owner->fastspring_id) { |
|
| 128 | try { |
||
| 129 | 2 | $customer = $this->owner->createAsFastspringCustomer(); |
|
|
0 ignored issues
–
show
|
|||
| 130 | 1 | } catch (ClientException $e) { |
|
| 131 | // we should get its id and save it |
||
| 132 | 1 | $response = $e->getResponse(); |
|
| 133 | 1 | $content = json_decode($response->getBody()->getContents()); |
|
| 134 | |||
| 135 | 1 | if (isset($content->error->email)) { |
|
| 136 | 1 | $response = Fastspring::getAccounts(['email' => $this->owner->email]); |
|
| 137 | |||
| 138 | 1 | if ($response->accounts) { |
|
| 139 | 1 | $account = $response->accounts[0]; |
|
| 140 | |||
| 141 | // save it to eloquent model |
||
| 142 | 1 | $this->owner->fastspring_id = $account->id; |
|
| 143 | 1 | $this->owner->save(); |
|
| 144 | } |
||
| 145 | } else { |
||
| 146 | throw $e; // @codeCoverageIgnore |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | 4 | return $this->owner->fastspring_id; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Build the payload for session creation. |
||
| 156 | * |
||
| 157 | * @param int $fastspringId The fastspring identifier |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | 4 | protected function buildPayload($fastspringId) |
|
| 162 | { |
||
| 163 | 4 | return array_filter([ |
|
| 164 | 4 | 'account' => $fastspringId, |
|
| 165 | 'items' => [ |
||
| 166 | [ |
||
| 167 | 4 | 'product' => $this->plan, |
|
| 168 | 4 | 'quantity' => $this->quantity, |
|
| 169 | ], |
||
| 170 | ], |
||
| 171 | 'tags' => [ |
||
| 172 | 4 | 'name' => $this->name, |
|
| 173 | ], |
||
| 174 | 4 | 'coupon' => $this->coupon, |
|
| 175 | ]); |
||
| 176 | } |
||
| 177 | } |
||
| 178 |