diff --git a/app/Http/Controllers/ClientPortal/PaymentMethodController.php b/app/Http/Controllers/ClientPortal/PaymentMethodController.php index 4ecc39cb0cf6..892b40f11fc2 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 c0d88d1096dd..8b25238c7f4d 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 deaa654e651e..497d8b764c27 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 d221cada8e92..ed99a41b4cc2 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); } /**