bakaphp /
phalcon-api
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Gewaer\Api\Controllers; |
||
| 6 | |||
| 7 | use Phalcon\Cashier\Traits\StripeWebhookHandlersTrait; |
||
| 8 | use Phalcon\Http\Response; |
||
| 9 | use Gewaer\Models\Users; |
||
| 10 | use Gewaer\Models\EmailTemplates; |
||
| 11 | use Phalcon\Di; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class PaymentsController |
||
| 15 | * |
||
| 16 | * Class to handle payment webhook from our cashier library |
||
| 17 | * |
||
| 18 | * @package Gewaer\Api\Controllers |
||
| 19 | * @property Log $log |
||
| 20 | * |
||
| 21 | */ |
||
| 22 | class PaymentsController extends BaseController |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Stripe Webhook Handlers |
||
| 26 | */ |
||
| 27 | use StripeWebhookHandlersTrait; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Handle stripe webhoook calls |
||
| 31 | * |
||
| 32 | * @return Response |
||
| 33 | */ |
||
| 34 | 4 | public function handleWebhook(): Response |
|
| 35 | { |
||
| 36 | //we cant processs if we dont find the stripe header |
||
| 37 | 4 | if (!$this->request->hasHeader('Stripe-Signature')) { |
|
| 38 | throw new Exception('Route not found for this call'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 39 | } |
||
| 40 | 4 | $request = $this->request->getPost(); |
|
| 41 | 4 | if (empty($request)) { |
|
| 42 | $request = $this->request->getJsonRawBody(true); |
||
| 43 | } |
||
| 44 | 4 | $type = str_replace('.', '', ucwords(str_replace('_', '', $request['type']), '.')); |
|
| 45 | 4 | $method = 'handle' . $type; |
|
| 46 | 4 | $payloadContent = json_encode($request); |
|
| 47 | 4 | $this->log->info("Webhook Handler Method: {$method} \n"); |
|
| 48 | 4 | $this->log->info("Payload: {$payloadContent} \n"); |
|
| 49 | 4 | if (method_exists($this, $method)) { |
|
| 50 | 4 | return $this->{$method}($request, $method); |
|
| 51 | } else { |
||
| 52 | return $this->response(['Missing Method to Handled']); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Handle customer subscription updated. |
||
| 58 | * |
||
| 59 | * @param array $payload |
||
| 60 | * @return Response |
||
| 61 | */ |
||
| 62 | protected function handleCustomerSubscriptionUpdated(array $payload, string $method): Response |
||
| 63 | { |
||
| 64 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
| 65 | if ($user) { |
||
| 66 | //We need to send a mail to the user |
||
| 67 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
| 68 | } |
||
| 69 | return $this->response(['Webhook Handled']); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Handle customer subscription free trial ending. |
||
| 74 | * |
||
| 75 | * @param array $payload |
||
| 76 | * @return Response |
||
| 77 | */ |
||
| 78 | 1 | protected function handleCustomerSubscriptionTrialwillend(array $payload, string $method): Response |
|
| 79 | { |
||
| 80 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
| 81 | 1 | if ($user) { |
|
| 82 | //We need to send a mail to the user |
||
| 83 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
| 84 | } |
||
| 85 | 1 | return $this->response(['Webhook Handled']); |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Handle sucessfull payment |
||
| 90 | * |
||
| 91 | * @todo send email |
||
| 92 | * @param array $payload |
||
| 93 | * @return Response |
||
| 94 | */ |
||
| 95 | 1 | protected function handleChargeSucceeded(array $payload, string $method): Response |
|
| 96 | { |
||
| 97 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
| 98 | 1 | if ($user) { |
|
| 99 | //We need to send a mail to the user |
||
| 100 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
| 101 | } |
||
| 102 | 1 | return $this->response(['Webhook Handled']); |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Handle bad payment |
||
| 107 | * |
||
| 108 | * @todo send email |
||
| 109 | * @param array $payload |
||
| 110 | * @return Response |
||
| 111 | */ |
||
| 112 | 1 | protected function handleChargeFailed(array $payload, string $method) : Response |
|
| 113 | { |
||
| 114 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
| 115 | 1 | if ($user) { |
|
| 116 | //We need to send a mail to the user |
||
| 117 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
| 118 | } |
||
| 119 | 1 | return $this->response(['Webhook Handled']); |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Handle pending payments |
||
| 124 | * |
||
| 125 | * @todo send email |
||
| 126 | * @param array $payload |
||
| 127 | * @return Response |
||
| 128 | */ |
||
| 129 | 1 | protected function handleChargePending(array $payload, string $method) : Response |
|
| 130 | { |
||
| 131 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
| 132 | 1 | if ($user) { |
|
| 133 | //We need to send a mail to the user |
||
| 134 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
| 135 | } |
||
| 136 | 1 | return $this->response(['Webhook Handled']); |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Send webhook related emails to user |
||
| 141 | * @param Users $user |
||
| 142 | * @param array $payload |
||
| 143 | * @param string $method |
||
| 144 | * @return void |
||
| 145 | */ |
||
| 146 | protected function sendWebhookResponseEmail(Users $user, array $payload, string $method): void |
||
| 147 | { |
||
| 148 | switch ($method) { |
||
| 149 | case 'handleCustomerSubscriptionTrialwillend': |
||
| 150 | $templateName = 'users-trial-end'; |
||
| 151 | break; |
||
| 152 | case 'handleCustomerSubscriptionUpdated': |
||
| 153 | $templateName = 'users-subscription-updated'; |
||
| 154 | break; |
||
| 155 | |||
| 156 | case 'handleChargeSucceeded': |
||
| 157 | $templateName = 'users-charge-success'; |
||
| 158 | break; |
||
| 159 | |||
| 160 | case 'handleChargeFailed': |
||
| 161 | $templateName = 'users-charge-failed'; |
||
| 162 | break; |
||
| 163 | |||
| 164 | case 'handleChargePending': |
||
| 165 | $templateName = 'users-charge-pending'; |
||
| 166 | break; |
||
| 167 | |||
| 168 | default: |
||
| 169 | break; |
||
| 170 | } |
||
| 171 | |||
| 172 | //Search for actual template by templateName |
||
| 173 | $emailTemplate = EmailTemplates::getByName($templateName); |
||
| 174 | |||
| 175 | Di::getDefault()->getMail() |
||
| 176 | ->to($user->email) |
||
| 177 | ->subject('Canvas Payments and Subscriptions') |
||
| 178 | ->content($emailTemplate->template) |
||
| 179 | ->sendNow(); |
||
| 180 | } |
||
| 181 | } |
||
| 182 |