mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 21:22:58 +01:00
Extract response from authorization
This commit is contained in:
parent
0f4d7f6aed
commit
b94ce97bac
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user