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\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\CreatePaymentMethodRequest;
use App\Http\Requests\ClientPortal\PaymentMethod\CreatePaymentMethodRequest;
use App\Http\Requests\Request;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
@ -52,7 +52,7 @@ class PaymentMethodController extends Controller
$data['gateway'] = $gateway;
$data['client'] = auth()->user()->client;
return $gateway
->driver(auth()->user()->client)
->setPaymentMethod($request->query('method'))
@ -93,7 +93,7 @@ class PaymentMethodController extends Controller
public function verify(ClientGatewayToken $payment_method)
{
// $gateway = $this->getClientGateway();
return $payment_method->gateway
->driver(auth()->user()->client)
->setPaymentMethod(request()->query('method'))

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.
*/
public function gatewayTypes() :array
public function gatewayTypes(): array
{
$types = [
GatewayType::CREDIT_CARD,
];
$types = [];
if ($this->company_gateway->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled) {
$types[] = GatewayType::CREDIT_CARD;
}
return $types;
}

View File

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

View File

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

View File

@ -32,7 +32,9 @@ use App\PaymentDrivers\Stripe\UpdatePaymentMethods;
use App\PaymentDrivers\Stripe\Utilities;
use App\Utils\Traits\MakesHash;
use Exception;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Carbon;
use Laracasts\Presenter\Exceptions\PresenterException;
use Stripe\Account;
use Stripe\Customer;
use Stripe\Exception\ApiErrorException;
@ -52,7 +54,7 @@ class StripePaymentDriver extends BaseDriver
public $can_authorise_credit_card = true;
/** @var \Stripe\StripeClient */
/** @var StripeClient */
public $stripe;
protected $customer_reference = 'customerReferenceParam';
@ -112,12 +114,13 @@ class StripePaymentDriver extends BaseDriver
public function gatewayTypes(): array
{
$types = [
GatewayType::CREDIT_CARD,
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
&& isset($this->client->country)
&& 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
&& 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;
}
@ -193,7 +197,7 @@ class StripePaymentDriver extends BaseDriver
$fields[] = ['name' => 'client_state', 'label' => ctrans('texts.state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_country_id', 'label' => ctrans('texts.country'), 'type' => 'text', 'validation' => 'required'];
}
$fields[] = ['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'];
if ($this->company_gateway->require_shipping_address) {
@ -213,7 +217,7 @@ class StripePaymentDriver extends BaseDriver
* Proxy method to pass the data into payment method authorizeView().
*
* @param array $data
* @return \Illuminate\Http\RedirectResponse|mixed
* @return RedirectResponse|mixed
*/
public function authorizeView(array $data)
{
@ -224,7 +228,7 @@ class StripePaymentDriver extends BaseDriver
* Processes the gateway response for credit card authorization.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|mixed
* @return RedirectResponse|mixed
*/
public function authorizeResponse($request)
{
@ -235,7 +239,7 @@ class StripePaymentDriver extends BaseDriver
* Process the payment with gateway.
*
* @param array $data
* @return \Illuminate\Http\RedirectResponse|mixed
* @return RedirectResponse|mixed
*/
public function processPaymentView(array $data)
{
@ -293,7 +297,7 @@ class StripePaymentDriver extends BaseDriver
* Finds or creates a Stripe Customer object.
*
* @return null|Customer A Stripe customer object
* @throws \Laracasts\Presenter\Exceptions\PresenterException
* @throws PresenterException
* @throws ApiErrorException
*/
public function findOrCreateCustomer(): ?Customer