From 0f4d7f6aedded4f9f8df8a53f5e6dcc38b2d9993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 1 Jun 2020 14:03:18 +0200 Subject: [PATCH 1/6] Refactor view for authorization --- .../ClientPortal/PaymentMethodController.php | 5 ++- app/PaymentDrivers/Stripe/CreditCard.php | 40 +++++++++++++++++++ app/PaymentDrivers/StripePaymentDriver.php | 34 +++++++++++----- 3 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 app/PaymentDrivers/Stripe/CreditCard.php diff --git a/app/Http/Controllers/ClientPortal/PaymentMethodController.php b/app/Http/Controllers/ClientPortal/PaymentMethodController.php index ee24a565db..4ecc39cb0c 100644 --- a/app/Http/Controllers/ClientPortal/PaymentMethodController.php +++ b/app/Http/Controllers/ClientPortal/PaymentMethodController.php @@ -48,7 +48,10 @@ class PaymentMethodController extends Controller 'token' => false, ]; - return $gateway->driver(auth()->user()->client)->authorizeCreditCardView($data); + return $gateway + ->driver(auth()->user()->client) + ->setPaymentMethod('App\\PaymentDrivers\\Stripe\\CreditCard') + ->authorizeView($data); } /** diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php new file mode 100644 index 0000000000..deaa654e65 --- /dev/null +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -0,0 +1,40 @@ +stripe = $stripe; + } + + /** + * Authorises a credit card for future use. + * + * @param array $data Array of variables needed for the view. + * + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + */ + public function authorizeView(array $data) + { + $intent['intent'] = $this->stripe->getSetupIntent(); + + return render('gateways.stripe.add_credit_card', array_merge($data, $intent)); + } +} \ No newline at end of file diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index f53f34e115..d221cada8e 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -40,6 +40,8 @@ class StripePaymentDriver extends BasePaymentDriver protected $customer_reference = 'customerReferenceParam'; + protected $payment_method; + /** * Methods in this class are divided into * two separate streams @@ -62,6 +64,21 @@ class StripePaymentDriver extends BasePaymentDriver Stripe::setApiKey($this->company_gateway->getConfigField('apiKey')); } + /** + * Return payment method type. + * + * @param string $method + * @return $this + */ + public function setPaymentMethod(string $method) + { + // Example: setPaymentMethod('App\\PaymentDrivers\\Stripe\\CreditCard'); + + $this->payment_method = new $method($this); + + return $this; + } + /** * Returns the gateway types */ @@ -128,16 +145,15 @@ class StripePaymentDriver extends BasePaymentDriver } /** - * Authorises a credit card for future use. - * - * @param array $data Array of variables needed for the view + * Proxy method to pass the data into payment method authorizeView(). + * + * @param array $data + * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ - public function authorizeCreditCardView(array $data) + public function authorizeView(array $data) { - $intent['intent'] = $this->getSetupIntent(); - - return render('gateways.stripe.add_credit_card', array_merge($data, $intent)); + return $this->payment_method->authorizeView($data); } /** @@ -400,12 +416,12 @@ class StripePaymentDriver extends BasePaymentDriver return $payment; } - private function convertFromStripeAmount($amount, $precision) + public function convertFromStripeAmount($amount, $precision) { return $amount / pow(10, $precision); } - private function convertToStripeAmount($amount, $precision) + public function convertToStripeAmount($amount, $precision) { return $amount * pow(10, $precision); } From b94ce97bac670475cd3216368b24d13790d7b87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 1 Jun 2020 14:14:41 +0200 Subject: [PATCH 2/6] Extract response from authorization --- .../ClientPortal/PaymentMethodController.php | 5 +- app/PaymentDrivers/BasePaymentDriver.php | 2 +- app/PaymentDrivers/Stripe/CreditCard.php | 57 ++++++++++++++++--- app/PaymentDrivers/StripePaymentDriver.php | 44 +------------- 4 files changed, 54 insertions(+), 54 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentMethodController.php b/app/Http/Controllers/ClientPortal/PaymentMethodController.php index 4ecc39cb0c..892b40f11f 100644 --- a/app/Http/Controllers/ClientPortal/PaymentMethodController.php +++ b/app/Http/Controllers/ClientPortal/PaymentMethodController.php @@ -64,7 +64,10 @@ class PaymentMethodController extends Controller { $gateway = auth()->user()->client->getCreditCardGateway(); - return $gateway->driver(auth()->user()->client)->authorizeCreditCardResponse($request); + return $gateway + ->driver(auth()->user()->client) + ->setPaymentMethod('App\\PaymentDrivers\\Stripe\\CreditCard') + ->authorizeCreditCardResponse($request); } /** diff --git a/app/PaymentDrivers/BasePaymentDriver.php b/app/PaymentDrivers/BasePaymentDriver.php index c0d88d1096..8b25238c7f 100644 --- a/app/PaymentDrivers/BasePaymentDriver.php +++ b/app/PaymentDrivers/BasePaymentDriver.php @@ -48,7 +48,7 @@ class BasePaymentDriver use MakesHash; /* The company gateway instance*/ - protected $company_gateway; + public $company_gateway; /* The Omnipay payment driver instance*/ protected $gateway; diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index deaa654e65..497d8b764c 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -12,11 +12,12 @@ namespace App\PaymentDrivers\Stripe; +use App\Models\ClientGatewayToken; +use App\Models\GatewayType; use App\PaymentDrivers\StripePaymentDriver; class CreditCard { - /** @var StripePaymentDriver */ public $stripe; public function __construct(StripePaymentDriver $stripe) @@ -24,17 +25,55 @@ class CreditCard $this->stripe = $stripe; } - /** - * Authorises a credit card for future use. - * - * @param array $data Array of variables needed for the view. - * - * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View - */ public function authorizeView(array $data) { $intent['intent'] = $this->stripe->getSetupIntent(); return render('gateways.stripe.add_credit_card', array_merge($data, $intent)); } -} \ No newline at end of file + + public function authorizeResponse($request) + { + $server_response = json_decode($request->input('gateway_response')); + + $gateway_id = $request->input('gateway_id'); + $gateway_type_id = $request->input('gateway_type_id'); + $is_default = $request->input('is_default'); + + $payment_method = $server_response->payment_method; + + $customer = $this->stripe->findOrCreateCustomer(); + + $this->stripe->init(); + + $stripe_payment_method = \Stripe\PaymentMethod::retrieve($payment_method); + $stripe_payment_method_obj = $stripe_payment_method->jsonSerialize(); + $stripe_payment_method->attach(['customer' => $customer->id]); + + $payment_meta = new \stdClass; + $payment_meta->exp_month = $stripe_payment_method_obj['card']['exp_month']; + $payment_meta->exp_year = $stripe_payment_method_obj['card']['exp_year']; + $payment_meta->brand = $stripe_payment_method_obj['card']['brand']; + $payment_meta->last4 = $stripe_payment_method_obj['card']['last4']; + $payment_meta->type = GatewayType::CREDIT_CARD; + + $client_gateway_token = new ClientGatewayToken(); + $client_gateway_token->company_id = $this->stripe->client->company->id; + $client_gateway_token->client_id = $this->stripe->client->id; + $client_gateway_token->token = $payment_method; + $client_gateway_token->company_gateway_id = $this->stripe->company_gateway->id; + $client_gateway_token->gateway_type_id = $gateway_type_id; + $client_gateway_token->gateway_customer_reference = $customer->id; + $client_gateway_token->meta = $payment_meta; + $client_gateway_token->save(); + + if ($is_default == 'true' || $this->stripe->client->gateway_tokens->count() == 1) { + $this->stripe->client->gateway_tokens()->update(['is_default'=>0]); + + $client_gateway_token->is_default = 1; + $client_gateway_token->save(); + } + + return redirect()->route('client.payment_methods.index'); + } +} diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index d221cada8e..ed99a41b4c 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -165,49 +165,7 @@ class StripePaymentDriver extends BasePaymentDriver */ public function authorizeCreditCardResponse($request) { - $server_response = json_decode($request->input('gateway_response')); - - $gateway_id = $request->input('gateway_id'); - $gateway_type_id = $request->input('gateway_type_id'); - $is_default = $request->input('is_default'); - - $payment_method = $server_response->payment_method; - - $customer = $this->findOrCreateCustomer(); - - $this->init(); - $stripe_payment_method = \Stripe\PaymentMethod::retrieve($payment_method); - $stripe_payment_method_obj = $stripe_payment_method->jsonSerialize(); - $stripe_payment_method->attach(['customer' => $customer->id]); - - $payment_meta = new \stdClass; - - if ($stripe_payment_method_obj['type'] == 'card') { - $payment_meta->exp_month = $stripe_payment_method_obj['card']['exp_month']; - $payment_meta->exp_year = $stripe_payment_method_obj['card']['exp_year']; - $payment_meta->brand = $stripe_payment_method_obj['card']['brand']; - $payment_meta->last4 = $stripe_payment_method_obj['card']['last4']; - $payment_meta->type = GatewayType::CREDIT_CARD; - } - - $cgt = new ClientGatewayToken; - $cgt->company_id = $this->client->company->id; - $cgt->client_id = $this->client->id; - $cgt->token = $payment_method; - $cgt->company_gateway_id = $this->company_gateway->id; - $cgt->gateway_type_id = $gateway_type_id; - $cgt->gateway_customer_reference = $customer->id; - $cgt->meta = $payment_meta; - $cgt->save(); - - if ($is_default == 'true' || $this->client->gateway_tokens->count() == 1) { - $this->client->gateway_tokens()->update(['is_default'=>0]); - - $cgt->is_default = 1; - $cgt->save(); - } - - return redirect()->route('client.payment_methods.index'); + return $this->payment_method->authorizeResponse($request); } /** From cf503b45315041a5886f671732b513f3615e407f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 1 Jun 2020 14:17:29 +0200 Subject: [PATCH 3/6] Extract Stripe utilites --- app/PaymentDrivers/Stripe/Utilities.php | 16 ++++++++++++++++ app/PaymentDrivers/StripePaymentDriver.php | 12 ++---------- 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 app/PaymentDrivers/Stripe/Utilities.php diff --git a/app/PaymentDrivers/Stripe/Utilities.php b/app/PaymentDrivers/Stripe/Utilities.php new file mode 100644 index 0000000000..3d01a79611 --- /dev/null +++ b/app/PaymentDrivers/Stripe/Utilities.php @@ -0,0 +1,16 @@ + Date: Mon, 1 Jun 2020 14:29:41 +0200 Subject: [PATCH 4/6] Extract paymentView --- .../ClientPortal/PaymentController.php | 13 +++++--- app/PaymentDrivers/Stripe/CreditCard.php | 30 ++++++++++++++++++- app/PaymentDrivers/StripePaymentDriver.php | 16 ++++------ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index ae856a240f..4dbeb2ae7f 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -95,9 +95,11 @@ class PaymentController extends Controller return $invoice; }); - $invoices->each(function ($invoice) { - InjectSignature::dispatch($invoice, request()->signature); - }); + if ((bool) request()->signature) { + $invoices->each(function ($invoice) { + InjectSignature::dispatch($invoice, request()->signature); + }); + } $payment_methods = auth()->user()->client->getPaymentMethods($amount); $gateway = CompanyGateway::find(request()->input('company_gateway_id')); @@ -116,7 +118,10 @@ class PaymentController extends Controller 'hashed_ids' => request()->invoices, ]; - return $gateway->driver(auth()->user()->client)->processPaymentView($data); + return $gateway + ->driver(auth()->user()->client) + ->setPaymentMethod('App\\PaymentDrivers\\Stripe\\CreditCard') + ->processPaymentView($data); } public function response(Request $request) diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index 497d8b764c..c3eb5426bb 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -68,7 +68,7 @@ class CreditCard $client_gateway_token->save(); if ($is_default == 'true' || $this->stripe->client->gateway_tokens->count() == 1) { - $this->stripe->client->gateway_tokens()->update(['is_default'=>0]); + $this->stripe->client->gateway_tokens()->update(['is_default' => 0]); $client_gateway_token->is_default = 1; $client_gateway_token->save(); @@ -76,4 +76,32 @@ class CreditCard return redirect()->route('client.payment_methods.index'); } + + public function paymentView(array $data) + { + $payment_intent_data = [ + 'amount' => $this->stripe->convertToStripeAmount($data['amount_with_fee'], $this->stripe->client->currency()->precision), + 'currency' => $this->stripe->client->getCurrencyCode(), + 'customer' => $this->stripe->findOrCreateCustomer(), + 'description' => $data['invoices']->pluck('id'), //todo more meaningful description here: + ]; + + if ($data['token']) { + $payment_intent_data['payment_method'] = $data['token']->token; + } else { + $payment_intent_data['setup_future_usage'] = 'off_session'; + // $payment_intent_data['save_payment_method'] = true; + // $payment_intent_data['confirm'] = true; + } + + $data['intent'] = $this->stripe->createPaymentIntent($payment_intent_data); + $data['gateway'] = $this->stripe; + + return render('gateways.stripe.credit_card', $data); + } + + public function paymentResponse() + { + # code... + } } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 2f46950323..70fdeea09e 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -65,12 +65,6 @@ class StripePaymentDriver extends BasePaymentDriver Stripe::setApiKey($this->company_gateway->getConfigField('apiKey')); } - /** - * Return payment method type. - * - * @param string $method - * @return $this - */ public function setPaymentMethod(string $method) { // Example: setPaymentMethod('App\\PaymentDrivers\\Stripe\\CreditCard'); @@ -160,9 +154,9 @@ class StripePaymentDriver extends BasePaymentDriver /** * Processes the gateway response for credit card authorization. * - * @param Request $request The returning request object - * @return view Returns the user to payment methods screen. - * @throws \Stripe\Exception\ApiErrorException + * @param \Illuminate\Http\Request $request The returning request object + + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function authorizeCreditCardResponse($request) { @@ -173,11 +167,13 @@ class StripePaymentDriver extends BasePaymentDriver * Process the payment with gateway. * * @param array $data + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|void - * @throws \Exception */ public function processPaymentView(array $data) { + return $this->payment_method->paymentView($data); + $payment_intent_data = [ 'amount' => $this->convertToStripeAmount($data['amount_with_fee'], $this->client->currency()->precision), 'currency' => $this->client->getCurrencyCode(), From fb793fe2f250bc59ef8468016d59cc389c1dfd86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 1 Jun 2020 14:31:02 +0200 Subject: [PATCH 5/6] Remove old code from processPaymentView --- app/PaymentDrivers/StripePaymentDriver.php | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 70fdeea09e..036a13c1e6 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -173,28 +173,6 @@ class StripePaymentDriver extends BasePaymentDriver public function processPaymentView(array $data) { return $this->payment_method->paymentView($data); - - $payment_intent_data = [ - 'amount' => $this->convertToStripeAmount($data['amount_with_fee'], $this->client->currency()->precision), - 'currency' => $this->client->getCurrencyCode(), - 'customer' => $this->findOrCreateCustomer(), - 'description' => $data['invoices']->pluck('id'), //todo more meaningful description here: - ]; - - if ($data['token']) { - $payment_intent_data['payment_method'] = $data['token']->token; - } else { - $payment_intent_data['setup_future_usage'] = 'off_session'; -// $payment_intent_data['save_payment_method'] = true; -// $payment_intent_data['confirm'] = true; - } - - - $data['intent'] = $this->createPaymentIntent($payment_intent_data); - - $data['gateway'] = $this; - - return render($this->viewForType($data['payment_method_id']), $data); } /** From 06190362ee250d8cbc5247651f9b7a6bcaf83579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 1 Jun 2020 16:19:03 +0200 Subject: [PATCH 6/6] extract purchase response --- .../ClientPortal/PaymentController.php | 5 +- app/PaymentDrivers/Stripe/CreditCard.php | 129 +++++++++++++++++- app/PaymentDrivers/StripePaymentDriver.php | 127 +---------------- 3 files changed, 132 insertions(+), 129 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index 4dbeb2ae7f..b8a52f6a28 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -128,6 +128,9 @@ class PaymentController extends Controller { $gateway = CompanyGateway::find($request->input('company_gateway_id')); - return $gateway->driver(auth()->user()->client)->processPaymentResponse($request); + return $gateway + ->driver(auth()->user()->client) + ->setPaymentMethod('App\\PaymentDrivers\\Stripe\\CreditCard') + ->processPaymentResponse($request); } } diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index c3eb5426bb..678500a710 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -12,9 +12,16 @@ namespace App\PaymentDrivers\Stripe; +use App\Events\Payment\PaymentWasCreated; +use App\Jobs\Mail\PaymentFailureMailer; +use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; +use App\Models\Invoice; +use App\Models\PaymentType; +use App\Models\SystemLog; use App\PaymentDrivers\StripePaymentDriver; +use Stripe\PaymentMethod; class CreditCard { @@ -100,8 +107,126 @@ class CreditCard return render('gateways.stripe.credit_card', $data); } - public function paymentResponse() + public function paymentResponse($request) { - # code... + $server_response = json_decode($request->input('gateway_response')); + + $state = [ + 'payment_method' => $server_response->payment_method, + 'payment_status' => $server_response->status, + 'save_card' => $request->store_card, + 'gateway_type_id' => $request->payment_method_id, + 'hashed_ids' => $request->hashed_ids, + 'server_response' => $server_response, + ]; + + $invoices = Invoice::whereIn('id', $this->stripe->transformKeys($state['hashed_ids'])) + ->whereClientId($this->stripe->client->id) + ->get(); + + if ($this->stripe->getContact()) { + $client_contact = $this->stripe->getContact(); + } else { + $client_contact = $invoices->first()->invitations->first()->contact; + } + + $this->stripe->init(); + + $state['payment_intent'] = \Stripe\PaymentIntent::retrieve($server_response->id); + $state['customer'] = $state['payment_intent']->customer; + + if ($state['payment_status'] == 'succeeded') { + return $this->processSuccessfulPayment($state); + } + + return $this->processUnsuccessfulPayment($server_response); + } + + private function processSuccessfulPayment($state) + { + $state['charge_id'] = $state['payment_intent']->charges->data[0]->id; + + $this->stripe->init(); + + $state['payment_method'] = PaymentMethod::retrieve($state['payment_method']); + $payment_method_object = $state['payment_method']->jsonSerialize(); + + $state['payment_meta'] = [ + 'exp_month' => $payment_method_object['card']['exp_month'], + 'exp_year' => $payment_method_object['card']['exp_year'], + 'brand' => $payment_method_object['card']['brand'], + 'last4' => $payment_method_object['card']['last4'], + 'type' => $payment_method_object['type'], + ]; + + $payment_type = PaymentType::parseCardType($payment_method_object['card']['brand']); + + if ($state['save_card'] === true) { + $this->saveCard($state); + } + + // Todo: Need to fix this to support payment types other than credit card.... sepa etc etc + if (!isset($state['payment_type'])) { + $state['payment_type'] = PaymentType::CREDIT_CARD_OTHER; + } + + $data = [ + 'payment_method' => $state['charge_id'], // ???? + 'payment_type' => $state['payment_type'], + 'amount' => $state['server_response']->amount, + ]; + + $payment = $this->stripe->createPayment($data); + + $this->stripe->attachInvoices($payment, $state['hashed_ids']); + + $payment->service()->updateInvoicePayment(); + + event(new PaymentWasCreated($payment, $payment->company)); + + $logger_message = [ + 'server_response' => $state['payment_intent'], + 'data' => $data + ]; + + SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->stripe->client); + + return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]); + } + + private function processUnsuccessfulPayment($server_response) + { + PaymentFailureMailer::dispatch($this->stripe->client, $server_response->cancellation_reason, $this->stripe->client->company, $server_response->amount); + + $message = [ + 'server_response' => $server_response, + 'data' => $data // - undefined @todo + ]; + + SystemLogger::dispatch($message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client); + + throw new \Exception('Failed to process the payment.', 1); + } + + private function saveCard($state) + { + $state['payment_method']->attach(['customer' => $state['customer']]); + + $company_gateway_token = new ClientGatewayToken(); + $company_gateway_token->company_id = $this->stripe->client->company->id; + $company_gateway_token->client_id = $this->stripe->client->id; + $company_gateway_token->token = $state['payment_method']; + $company_gateway_token->company_gateway_id = $this->stripe->company_gateway->id; + $company_gateway_token->gateway_type_id = $state['gateway_type_id']; + $company_gateway_token->gateway_customer_reference = $state['customer']; + $company_gateway_token->meta = $state['payment_meta']; + $company_gateway_token->save(); + + if ($this->stripe->client->gateway_tokens->count() == 1) { + $this->stripe->client->gateway_tokens()->update(['is_default' => 0]); + + $company_gateway_token->is_default = 1; + $company_gateway_token->save(); + } } } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 036a13c1e6..d46df13b53 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -205,132 +205,7 @@ class StripePaymentDriver extends BasePaymentDriver */ public function processPaymentResponse($request) //We never have to worry about unsuccessful payments as failures are handled at the front end for this driver. { - $server_response = json_decode($request->input('gateway_response')); - - $payment_method = $server_response->payment_method; - $payment_status = $server_response->status; - $save_card = $request->input('store_card'); - - $gateway_type_id = $request->input('payment_method_id'); - $hashed_ids = $request->input('hashed_ids'); - $invoices = Invoice::whereIn('id', $this->transformKeys($hashed_ids)) - ->whereClientId($this->client->id) - ->get(); - /** - * Potential statuses that can be returned - * - * requires_action - * processing - * canceled - * requires_action - * requires_confirmation - * requires_payment_method - * - */ - - if ($this->getContact()) { - $client_contact = $this->getContact(); - } else { - $client_contact = $invoices->first()->invitations->first()->contact; - } - - $this->init(); - $payment_intent = \Stripe\PaymentIntent::retrieve($server_response->id); - $customer = $payment_intent->customer; - - if ($payment_status == 'succeeded') { - - $charge_id = $payment_intent->charges->data[0]->id; - - $this->init(); - $stripe_payment_method = \Stripe\PaymentMethod::retrieve($payment_method); - $stripe_payment_method_obj = $stripe_payment_method->jsonSerialize(); - - $payment_meta = new \stdClass; - - if ($stripe_payment_method_obj['type'] == 'card') { - $payment_meta->exp_month = $stripe_payment_method_obj['card']['exp_month']; - $payment_meta->exp_year = $stripe_payment_method_obj['card']['exp_year']; - $payment_meta->brand = $stripe_payment_method_obj['card']['brand']; - $payment_meta->last4 = $stripe_payment_method_obj['card']['last4']; - $payment_meta->type = $stripe_payment_method_obj['type']; - - $payment_type = PaymentType::parseCardType($stripe_payment_method_obj['card']['brand']); - } - - if ($save_card == 'true') { - $stripe_payment_method->attach(['customer' => $customer]); - - $cgt = new ClientGatewayToken; - $cgt->company_id = $this->client->company->id; - $cgt->client_id = $this->client->id; - $cgt->token = $payment_method; - $cgt->company_gateway_id = $this->company_gateway->id; - $cgt->gateway_type_id = $gateway_type_id; - $cgt->gateway_customer_reference = $customer; - $cgt->meta = $payment_meta; - $cgt->save(); - - if ($this->client->gateway_tokens->count() == 1) { - $this->client->gateway_tokens()->update(['is_default'=>0]); - - $cgt->is_default = 1; - $cgt->save(); - } - } - - //todo need to fix this to support payment types other than credit card.... sepa etc etc - if (!$payment_type) { - $payment_type = PaymentType::CREDIT_CARD_OTHER; - } - - - $data = [ - 'payment_method' => $charge_id, - 'payment_type' => $payment_type, - 'amount' => $server_response->amount, - ]; - - /* Create payment*/ - $payment = $this->createPayment($data); - - /* Link invoices to payment*/ - $this->attachInvoices($payment, $hashed_ids); - - $payment->service()->UpdateInvoicePayment(); - - event(new PaymentWasCreated($payment, $payment->company)); - - SystemLogger::dispatch( - [ - 'server_response' => $payment_intent, - 'data' => $data - ], - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_SUCCESS, - SystemLog::TYPE_STRIPE, - $this->client - ); - - return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); - } else { - - PaymentFailureMailer::dispatch($this->client, $server_response->cancellation_reason, $this->client->company, $server_response->amount); - - /*Fail and log*/ - SystemLogger::dispatch( - [ - 'server_response' => $server_response, - 'data' => $data - ], - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_FAILURE, - SystemLog::TYPE_STRIPE, - $this->client - ); - - throw new \Exception("Failed to process payment", 1); - } + return $this->payment_method->paymentResponse($request); } public function createPayment($data) :Payment