Merge pull request #6163 from beganovich/v5-2906-payment-methods-enabled-check

(v5) Check for enabled payment methods
This commit is contained in:
Benjamin Beganović 2021-06-30 01:59:25 +02:00 committed by GitHub
commit 1693086fbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 63 deletions

View File

@ -14,7 +14,7 @@ namespace App\Http\Controllers\ClientPortal;
use App\Events\Payment\Methods\MethodDeleted; use App\Events\Payment\Methods\MethodDeleted;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\CreatePaymentMethodRequest; use App\Http\Requests\ClientPortal\PaymentMethod\CreatePaymentMethodRequest;
use App\Http\Requests\Request; use App\Http\Requests\Request;
use App\Models\ClientGatewayToken; use App\Models\ClientGatewayToken;
use App\Models\GatewayType; use App\Models\GatewayType;

View File

@ -1,31 +0,0 @@
<?php
namespace App\Http\Requests\ClientPortal;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class CreatePaymentMethodRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->client->getCreditCardGateway() ? true : false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Http\Requests\ClientPortal\PaymentMethod;
use App\Http\Requests\Request;
use App\Models\Client;
use Illuminate\Foundation\Http\FormRequest;
use function auth;
use function collect;
class CreatePaymentMethodRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
/** @var Client $client */
$client = auth()->user()->client;
$available_methods = [];
collect($client->service()->getPaymentMethods(1))
->filter(function ($method) use (&$available_methods) {
$available_methods[] = $method['gateway_type_id'];
});
if (in_array($this->query('method'), $available_methods)) {
return true;
}
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -54,11 +54,13 @@ class AuthorizePaymentDriver extends BaseDriver
/** /**
* Returns the gateway types. * Returns the gateway types.
*/ */
public function gatewayTypes() :array public function gatewayTypes(): array
{ {
$types = [ $types = [];
GatewayType::CREDIT_CARD,
]; if ($this->company_gateway->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled) {
$types[] = GatewayType::CREDIT_CARD;
}
return $types; return $types;
} }

View File

@ -25,6 +25,8 @@ use App\Models\PaymentType;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\PaymentDrivers\Braintree\CreditCard; use App\PaymentDrivers\Braintree\CreditCard;
use App\PaymentDrivers\Braintree\PayPal; use App\PaymentDrivers\Braintree\PayPal;
use Braintree\Gateway;
use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class BraintreePaymentDriver extends BaseDriver class BraintreePaymentDriver extends BaseDriver
@ -36,7 +38,7 @@ class BraintreePaymentDriver extends BaseDriver
public $can_authorise_credit_card = true; public $can_authorise_credit_card = true;
/** /**
* @var \Braintree\Gateway; * @var Gateway;
*/ */
public $gateway; public $gateway;
@ -49,7 +51,7 @@ class BraintreePaymentDriver extends BaseDriver
public function init(): void public function init(): void
{ {
$this->gateway = new \Braintree\Gateway([ $this->gateway = new Gateway([
'environment' => $this->company_gateway->getConfigField('testMode') ? 'sandbox' : 'production', 'environment' => $this->company_gateway->getConfigField('testMode') ? 'sandbox' : 'production',
'merchantId' => $this->company_gateway->getConfigField('merchantId'), 'merchantId' => $this->company_gateway->getConfigField('merchantId'),
'publicKey' => $this->company_gateway->getConfigField('publicKey'), 'publicKey' => $this->company_gateway->getConfigField('publicKey'),
@ -68,10 +70,15 @@ class BraintreePaymentDriver extends BaseDriver
public function gatewayTypes(): array public function gatewayTypes(): array
{ {
return [ $types = [
GatewayType::CREDIT_CARD,
GatewayType::PAYPAL, GatewayType::PAYPAL,
]; ];
if ($this->company_gateway->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled) {
$types[] = GatewayType::CREDIT_CARD;
}
return $types;
} }
public function authorizeView($data) public function authorizeView($data)
@ -126,11 +133,11 @@ class BraintreePaymentDriver extends BaseDriver
return [ return [
'transaction_reference' => $response->id, 'transaction_reference' => $response->id,
'transaction_response' => json_encode($response), 'transaction_response' => json_encode($response),
'success' => (bool) $response->success, 'success' => (bool)$response->success,
'description' => $response->status, 'description' => $response->status,
'code' => 0, 'code' => 0,
]; ];
} catch (\Exception $e) { } catch (Exception $e) {
return [ return [
'transaction_reference' => null, 'transaction_reference' => null,
'transaction_response' => json_encode($e->getMessage()), 'transaction_response' => json_encode($e->getMessage()),
@ -174,7 +181,7 @@ class BraintreePaymentDriver extends BaseDriver
'gateway_type_id' => GatewayType::CREDIT_CARD, 'gateway_type_id' => GatewayType::CREDIT_CARD,
]; ];
$payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); $payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
SystemLogger::dispatch( SystemLogger::dispatch(
['response' => $result, 'data' => $data], ['response' => $result, 'data' => $data],

View File

@ -72,11 +72,15 @@ class CheckoutComPaymentDriver extends BaseDriver
/** /**
* Returns the default gateway type. * Returns the default gateway type.
*/ */
public function gatewayTypes() public function gatewayTypes(): array
{ {
return [ $types = [];
GatewayType::CREDIT_CARD,
]; if ($this->company_gateway->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled) {
$types[] = GatewayType::CREDIT_CARD;
}
return $types;
} }
/** /**
@ -232,7 +236,7 @@ class CheckoutComPaymentDriver extends BaseDriver
'transaction_reference' => $response->id, 'transaction_reference' => $response->id,
]; ];
$payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); $payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
SystemLogger::dispatch( SystemLogger::dispatch(
['response' => $response, 'data' => $data], ['response' => $response, 'data' => $data],
@ -269,11 +273,11 @@ class CheckoutComPaymentDriver extends BaseDriver
return false; return false;
} }
} catch (\Exception | CheckoutHttpException $e) { } catch (Exception | CheckoutHttpException $e) {
$this->unWindGatewayFees($payment_hash); $this->unWindGatewayFees($payment_hash);
$message = $e instanceof CheckoutHttpException $message = $e instanceof CheckoutHttpException
? $e->getBody() ? $e->getBody()
: $e->getMessage(); : $e->getMessage();
$data = [ $data = [
'status' => '', 'status' => '',

View File

@ -32,7 +32,9 @@ use App\PaymentDrivers\Stripe\UpdatePaymentMethods;
use App\PaymentDrivers\Stripe\Utilities; use App\PaymentDrivers\Stripe\Utilities;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Exception; use Exception;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Laracasts\Presenter\Exceptions\PresenterException;
use Stripe\Account; use Stripe\Account;
use Stripe\Customer; use Stripe\Customer;
use Stripe\Exception\ApiErrorException; use Stripe\Exception\ApiErrorException;
@ -52,7 +54,7 @@ class StripePaymentDriver extends BaseDriver
public $can_authorise_credit_card = true; public $can_authorise_credit_card = true;
/** @var \Stripe\StripeClient */ /** @var StripeClient */
public $stripe; public $stripe;
protected $customer_reference = 'customerReferenceParam'; protected $customer_reference = 'customerReferenceParam';
@ -112,12 +114,13 @@ class StripePaymentDriver extends BaseDriver
public function gatewayTypes(): array public function gatewayTypes(): array
{ {
$types = [ $types = [
GatewayType::CREDIT_CARD,
GatewayType::CRYPTO, GatewayType::CRYPTO,
// GatewayType::SEPA, // TODO: Missing implementation
// GatewayType::APPLE_PAY, // TODO:: Missing implementation
]; ];
if ($this->company_gateway->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled) {
$types[] = GatewayType::CREDIT_CARD;
}
if ($this->client if ($this->client
&& isset($this->client->country) && isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ['AUT', 'BEL', 'DEU', 'ITA', 'NLD', 'ESP'])) { && in_array($this->client->country->iso_3166_3, ['AUT', 'BEL', 'DEU', 'ITA', 'NLD', 'ESP'])) {
@ -126,7 +129,8 @@ class StripePaymentDriver extends BaseDriver
if ($this->client if ($this->client
&& isset($this->client->country) && isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ['USA'])) { && in_array($this->client->country->iso_3166_3, ['USA'])
&& $this->company_gateway->fees_and_limits->{GatewayType::BANK_TRANSFER}->is_enabled) {
$types[] = GatewayType::BANK_TRANSFER; $types[] = GatewayType::BANK_TRANSFER;
} }
@ -213,7 +217,7 @@ class StripePaymentDriver extends BaseDriver
* Proxy method to pass the data into payment method authorizeView(). * Proxy method to pass the data into payment method authorizeView().
* *
* @param array $data * @param array $data
* @return \Illuminate\Http\RedirectResponse|mixed * @return RedirectResponse|mixed
*/ */
public function authorizeView(array $data) public function authorizeView(array $data)
{ {
@ -224,7 +228,7 @@ class StripePaymentDriver extends BaseDriver
* Processes the gateway response for credit card authorization. * Processes the gateway response for credit card authorization.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|mixed * @return RedirectResponse|mixed
*/ */
public function authorizeResponse($request) public function authorizeResponse($request)
{ {
@ -235,7 +239,7 @@ class StripePaymentDriver extends BaseDriver
* Process the payment with gateway. * Process the payment with gateway.
* *
* @param array $data * @param array $data
* @return \Illuminate\Http\RedirectResponse|mixed * @return RedirectResponse|mixed
*/ */
public function processPaymentView(array $data) public function processPaymentView(array $data)
{ {
@ -293,7 +297,7 @@ class StripePaymentDriver extends BaseDriver
* Finds or creates a Stripe Customer object. * Finds or creates a Stripe Customer object.
* *
* @return null|Customer A Stripe customer object * @return null|Customer A Stripe customer object
* @throws \Laracasts\Presenter\Exceptions\PresenterException * @throws PresenterException
* @throws ApiErrorException * @throws ApiErrorException
*/ */
public function findOrCreateCustomer(): ?Customer public function findOrCreateCustomer(): ?Customer