bakaphp /
cashier
| 1 | <?php |
||
|
1 ignored issue
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 2 | |||
| 3 | namespace Phalcon\Cashier\Traits; |
||
| 4 | |||
| 5 | use Phalcon\Http\Response; |
||
| 6 | use Exception; |
||
| 7 | use Baka\Auth\Models\Users; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Trait WebhookHandlers |
||
| 11 | * |
||
| 12 | * @package Phalcon\Cashier\Traits |
||
| 13 | * |
||
| 14 | * @property Users $user |
||
| 15 | * @property Subscriptions $subscriptions |
||
| 16 | * |
||
| 17 | */ |
||
| 18 | trait StripeWebhookHandlersTrait |
||
|
1 ignored issue
–
show
|
|||
| 19 | {
|
||
| 20 | /** |
||
| 21 | * Handle stripe webhoook calls |
||
| 22 | * |
||
| 23 | * @return Response |
||
| 24 | */ |
||
| 25 | public function handleWebhook(): Response |
||
| 26 | {
|
||
| 27 | //we cant processs if we dont find the stripe header |
||
| 28 | if (!$this->request->hasHeader('Stripe-Signature')) {
|
||
| 29 | throw new Exception('Route not found for this call');
|
||
| 30 | } |
||
| 31 | |||
| 32 | $request = $this->request->getPost(); |
||
| 33 | |||
| 34 | if (empty($request)) {
|
||
| 35 | $request = $this->request->getJsonRawBody(true); |
||
| 36 | } |
||
| 37 | $type = str_replace('.', '', ucwords(str_replace('_', '', $request['type']), '.'));
|
||
| 38 | $method = 'handle' . $type; |
||
| 39 | |||
| 40 | $payloadContent = json_encode($request); |
||
| 41 | $this->log->info("Webhook Handler Method: {$method} \n");
|
||
| 42 | $this->log->info("Payload: {$payloadContent} \n");
|
||
| 43 | |||
| 44 | if (method_exists($this, $method)) {
|
||
| 45 | return $this->{$method}($request);
|
||
| 46 | } else {
|
||
| 47 | return $this->response(['Missing Method to Handled']); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Handle customer subscription updated. |
||
| 53 | * |
||
| 54 | * @param array $payload |
||
| 55 | * @return Response |
||
| 56 | */ |
||
| 57 | protected function handleCustomerSubscriptionUpdated(array $payload): Response |
||
| 58 | {
|
||
| 59 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
| 60 | if ($user) {
|
||
| 61 | //We need to send a mail to the user |
||
| 62 | $this->sendWebhookResponseEmail($user, $payload); |
||
| 63 | } |
||
| 64 | return $this->response(['Webhook Handled']); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Handle a cancelled customer from a Stripe subscription. |
||
| 69 | * |
||
| 70 | * @param array $payload |
||
| 71 | * @return Response |
||
| 72 | */ |
||
| 73 | protected function handleCustomerSubscriptionDeleted(array $payload): Response |
||
| 74 | {
|
||
| 75 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
| 76 | if ($user) {
|
||
| 77 | $subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']);
|
||
| 78 | |||
| 79 | if (is_object($subscription)) {
|
||
| 80 | $subscription->markAsCancelled(); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | return $this->response(['Webhook Handled']); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Handle customer subscription free trial ending. |
||
| 88 | * |
||
| 89 | * @param array $payload |
||
| 90 | * @return Response |
||
| 91 | */ |
||
| 92 | protected function handleCustomerSubscriptionTrialwillend(array $payload): Response |
||
| 93 | {
|
||
| 94 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
| 95 | if ($user) {
|
||
| 96 | //We need to send a mail to the user |
||
| 97 | $this->sendWebhookResponseEmail($user, $payload); |
||
| 98 | } |
||
| 99 | return $this->response(['Webhook Handled']); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Handle customer updated. |
||
| 104 | * |
||
| 105 | * @param array $payload |
||
| 106 | * @return Response |
||
| 107 | */ |
||
| 108 | protected function handleCustomerUpdated(array $payload): Response |
||
| 109 | {
|
||
| 110 | if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) {
|
||
| 111 | $user->updateCardFromStripe(); |
||
| 112 | } |
||
| 113 | return $this->response(['Webhook Handled']); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Handle customer source deleted. |
||
| 118 | * |
||
| 119 | * @param array $payload |
||
| 120 | * @return Response |
||
| 121 | */ |
||
| 122 | protected function handleCustomerSourceDeleted(array $payload) : Response |
||
| 123 | {
|
||
| 124 | if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) {
|
||
| 125 | $user->updateCardFromStripe(); |
||
| 126 | } |
||
| 127 | return $this->response(['Webhook Handled']); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Handle deleted customer. |
||
| 132 | * |
||
| 133 | * @param array $payload |
||
| 134 | * @return Response |
||
| 135 | */ |
||
| 136 | protected function handleCustomerDeleted(array $payload) : Response |
||
| 137 | {
|
||
| 138 | $user = Users::findFirstByStripeId($payload['data']['object']['id']); |
||
| 139 | if ($user) {
|
||
| 140 | foreach ($user->subscriptions as $subscription) {
|
||
| 141 | $subscription->skipTrial()->markAsCancelled(); |
||
| 142 | } |
||
| 143 | |||
| 144 | $user->stripe_id = null; |
||
| 145 | $user->trial_ends_at = null; |
||
| 146 | $user->card_brand = null; |
||
| 147 | $user->card_last_four = null; |
||
| 148 | $user->update(); |
||
| 149 | } |
||
| 150 | return $this->response(['Webhook Handled']); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Handle sucessfull payment |
||
| 155 | * |
||
| 156 | * @todo send email |
||
| 157 | * @param array $payload |
||
| 158 | * @return Response |
||
| 159 | */ |
||
| 160 | protected function handleChargeSucceeded(array $payload): Response |
||
| 161 | {
|
||
| 162 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
| 163 | if ($user) {
|
||
| 164 | //We need to send a mail to the user |
||
| 165 | $this->sendWebhookResponseEmail($user, $payload); |
||
| 166 | } |
||
| 167 | return $this->response(['Webhook Handled']); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Handle bad payment |
||
| 172 | * |
||
| 173 | * @todo send email |
||
| 174 | * @param array $payload |
||
| 175 | * @return Response |
||
| 176 | */ |
||
| 177 | protected function handleChargeFailed(array $payload) : Response |
||
| 178 | {
|
||
| 179 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
| 180 | if ($user) {
|
||
| 181 | //We need to send a mail to the user |
||
| 182 | $this->sendWebhookResponseEmail($user, $payload); |
||
| 183 | } |
||
| 184 | return $this->response(['Webhook Handled']); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Handle looking for refund |
||
| 189 | * |
||
| 190 | * @todo send email |
||
| 191 | * @param array $payload |
||
| 192 | * @return Response |
||
| 193 | */ |
||
| 194 | protected function handleChargeDisputeCreated(array $payload) : Response |
||
| 195 | {
|
||
| 196 | return $this->response(['Webhook Handled']); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Handle pending payments |
||
| 201 | * |
||
| 202 | * @todo send email |
||
| 203 | * @param array $payload |
||
| 204 | * @return Response |
||
| 205 | */ |
||
| 206 | protected function handleChargePending(array $payload) : Response |
||
| 207 | {
|
||
| 208 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
| 209 | if ($user) {
|
||
| 210 | //We need to send a mail to the user |
||
| 211 | $this->sendWebhookResponseEmail($user, $payload); |
||
| 212 | } |
||
| 213 | return $this->response(['Webhook Handled']); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Send webhook related emails to user |
||
| 218 | * @param Users $user |
||
| 219 | * @param array $payload |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | protected static function sendWebhookResponseEmail(Users $user, array $payload): void |
||
| 223 | {
|
||
| 224 | // $subject = ''; |
||
| 225 | // $content = ''; |
||
| 226 | // Di::getDefault()->getMail() |
||
| 227 | // ->to($user->email) |
||
| 228 | // ->subject($subject) |
||
| 229 | // ->content($content) |
||
| 230 | // ->sendNow(); |
||
| 231 | } |
||
| 232 | } |
||
| 233 |