Refactor view for authorization

This commit is contained in:
Benjamin Beganović 2020-06-01 14:03:18 +02:00
parent 90e4534fef
commit 0f4d7f6aed
3 changed files with 69 additions and 10 deletions

View File

@ -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);
}
/**

View File

@ -0,0 +1,40 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers\Stripe;
use App\PaymentDrivers\StripePaymentDriver;
class CreditCard
{
/** @var StripePaymentDriver */
public $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$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));
}
}

View File

@ -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);
}