powerboardOM

This commit is contained in:
David Bomba 2024-09-10 08:40:40 +10:00
parent 6177483791
commit b715c7dd20
9 changed files with 232 additions and 48 deletions

View File

@ -28,6 +28,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @property bool $update_details
* @property bool $is_deleted
* @property string $config
* @property object $settings
* @property mixed $fees_and_limits
* @property string|null $custom_value1
* @property string|null $custom_value2
@ -74,6 +75,7 @@ class CompanyGateway extends BaseModel
protected $casts = [
'fees_and_limits' => 'object',
'settings' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
@ -484,6 +486,18 @@ class CompanyGateway extends BaseModel
return $fee;
}
public function getSettings()
{
// return $this->settings;
return $this->settings ?? new \stdClass;
}
public function setSettings($settings)
{
$this->settings = $settings;
$this->save();
}
public function webhookUrl()
{
return route('payment_webhook', ['company_key' => $this->company->company_key, 'company_gateway_id' => $this->hashed_id]);

View File

@ -25,11 +25,15 @@ use App\PaymentDrivers\CBAPowerBoard\Models\Charge;
use App\PaymentDrivers\Common\LivewireMethodInterface;
use App\PaymentDrivers\CBAPowerBoard\Models\PaymentSource;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\PaymentDrivers\CBAPowerBoard\Models\Gateway;
class CreditCard implements LivewireMethodInterface
{
private Gateway $cba_gateway;
public function __construct(public CBAPowerBoardPaymentDriver $powerboard)
{
$this->cba_gateway = $this->powerboard->settings()->getPaymentGatewayConfiguration(GatewayType::CREDIT_CARD);
}
public function authorizeView(array $data)
@ -51,13 +55,14 @@ class CreditCard implements LivewireMethodInterface
$browser_details = json_decode($request->browser_details, true);
$payload = [
"capture" => false,
"amount" => 1,
"currency" => $this->powerboard->client->currency()->code,
"description" => "Card authorization",
"customer" => [
"payment_source" => [
"vault_token" => $payment_source->vault_token,
"gateway_id" => $payment_source->gateway_id,
"gateway_id" => $this->powerboard->settings()->getGatewayId(GatewayType::CREDIT_CARD),
],
],
"_3ds" => [
@ -75,8 +80,8 @@ class CreditCard implements LivewireMethodInterface
$charge = $r->json();
nlog($charge['resource']['data']);
return response()->json($charge['resource']['data'], 200);
return response()->json($charge['resource']['data'], 200);
}
elseif($request->charge) {
@ -90,6 +95,7 @@ class CreditCard implements LivewireMethodInterface
'id' => $charge_request['charge_3ds_id'],
],
"capture" => false,
"authorization" => true,
"amount"=> 1,
"currency"=> $this->powerboard->client->currency()->code,
"store_cvv"=> true,
@ -99,19 +105,23 @@ class CreditCard implements LivewireMethodInterface
$r = $this->powerboard->gatewayRequest("/v1/charges", (\App\Enum\HttpVerb::POST)->value, $payload, []);
if($r->failed())
return $this->processUnsuccessfulPayment($r);
if($r->failed()){
$error_payload = $this->getErrorFromResponse($r);
throw new PaymentFailed($error_payload[0], $error_payload[1]);
}
$charge = (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(Charge::class, $r->object()->resource->data) ?? $r->throw();
nlog($charge);
if ($charge->status == 'complete') {
$this->powerboard->logSuccessfulGatewayResponse(['response' => $charge, 'data' => $this->powerboard->payment_hash], SystemLog::TYPE_POWERBOARD);
$vt = $charge->customer->payment_source->vault_token;
if($request->store_card){
$data = [
"payment_source" => [
"vault_token" => $vt,
@ -120,9 +130,8 @@ class CreditCard implements LivewireMethodInterface
$customer = $this->powerboard->customer()->findOrCreateCustomer($data);
$cgt = $this->powerboard->customer()->storePaymentMethod($charge->customer->payment_source, $charge->customer);
}
return redirect()->route('client.payment_methods.index');
return redirect()->route('client.payment_methods.show', ['payment_method' => $cgt->hashed_id]);
}
@ -183,12 +192,15 @@ class CreditCard implements LivewireMethodInterface
public function paymentData(array $data): array
{
if($this->cba_gateway->verification_status != "completed")
throw new PaymentFailed("This payment method is not configured as yet. Reference Powerboard portal for further information", 400);
$merge = [
'public_key' => $this->powerboard->company_gateway->getConfigField('publicKey'),
'widget_endpoint' => $this->powerboard->widget_endpoint,
'gateway' => $this->powerboard,
'environment' => $this->powerboard->environment,
'gateway_id' => $this->cba_gateway->_id,
];
return array_merge($data, $merge);
@ -250,7 +262,7 @@ class CreditCard implements LivewireMethodInterface
"customer" => [
"payment_source" => [
"vault_token" => $source->vault_token,
"gateway_id" => $source->gateway_id,
"gateway_id" => $this->powerboard->settings()->getGatewayId(GatewayType::CREDIT_CARD),
],
],
"_3ds" => [
@ -389,18 +401,37 @@ class CreditCard implements LivewireMethodInterface
return redirect()->route('client.payments.show', ['payment' => $payment->hashed_id]);
}
public function processUnsuccessfulPayment($response)
private function getErrorFromResponse($response)
{
try{
try {
$response->throw();
}
catch(RequestException $exception){
} catch (RequestException $exception) {
$error_object = $exception->response->object();
//todo log this error_object
nlog($error_object);
return response()->json(['message' => $error_object->error->details->messages[0]->gateway_specific_code, 'code' => 400], 400);
$error_message = "Unknown error";
match($error_object->error->code) {
"UnfulfilledCondition" => $error_message = $error_object->error->message,
"transaction_declined" => $error_message = $error_object->error->details[0]->status_code_description,
default => $error_message = "Unknown error",
};
return [$error_message, $exception->getCode()];
}
}
public function processUnsuccessfulPayment($response)
{
$error_payload = $this->getErrorFromResponse($response);
return response()->json(['message' => $error_payload[0], 'code' => $error_payload[1]], $error_payload[1]);
// $this->stripe->sendFailureMail($server_response->cancellation_reason);

View File

@ -1,18 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\CBAPowerBoard\Models;
class Customers
{
/** @var \App\PaymentDrivers\CBAPowerBoard\Models\Customer[] */
public array $customers;
}

View File

@ -0,0 +1,56 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\CBAPowerBoard\Models;
class Gateway
{
/** @var string */
public string $_id;
/** @var string */
public string $name;
/** @var string */
public string $type;
/** @var string */
public string $mode;
/** @var string */
public string $created_at;
/** @var string */
public string $updated_at;
/** @var bool */
public bool $archived;
/** @var bool */
public bool $default;
/** @var string */
public string $verification_status;
public function __construct(
string $_id,
string $name,
string $type,
string $mode,
string $created_at,
string $updated_at,
bool $archived,
bool $default,
string $verification_status
) {
$this->_id = $_id;
$this->name = $name;
$this->type = $type;
$this->mode = $mode;
$this->created_at = $created_at;
$this->updated_at = $updated_at;
$this->archived = $archived;
$this->default = $default;
$this->verification_status = $verification_status;
}
}

View File

@ -0,0 +1,96 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\CBAPowerBoard;
use App\PaymentDrivers\CBAPowerBoard\Models\Gateways;
use App\PaymentDrivers\CBAPowerBoard\Models\Gateway;
use App\PaymentDrivers\CBAPowerBoardPaymentDriver;
class Settings
{
protected const GATEWAY_CBA = 'MasterCard';
protected const GATEWAY_AFTERPAY = 'Afterpay';
protected const GATEWAY_PAYPAL = 'Paypal';
protected const GATEWAY_ZIP = 'Zipmoney';
public function __construct(public CBAPowerBoardPaymentDriver $powerboard)
{
}
public function getGateways()
{
$r = $this->powerboard->gatewayRequest('/v1/gateways', (\App\Enum\HttpVerb::GET)->value, [], []);
if($r->failed())
$r->throw();
nlog($r->object());
return (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(Gateway::class."[]", $r->object()->resource->data);
}
/** We will need to have a process that updates this at intervals */
public function updateSettings():self
{
$gateways = $this->getGateways();
$settings = $this->powerboard->company_gateway->getSettings();
$settings->gateways = $gateways;
$this->powerboard->company_gateway->setSettings($settings);
return $this;
}
public function getSettings(): mixed
{
return $this->powerboard->company_gateway->getSettings();
}
public function getPaymentGatewayConfiguration(int $gateway_type_id): mixed
{
$type = self::GATEWAY_CBA;
match($gateway_type_id){
\App\Models\GatewayType::CREDIT_CARD => $type = self::GATEWAY_CBA,
default => $type = self::GATEWAY_CBA,
};
return $this->getGatewayByType($type);
}
private function getGatewayByType(string $gateway_type_const): mixed
{
$settings = $this->getSettings();
if(!property_exists($settings,'gateways')){
$this->updateSettings();
$settings = $this->getSettings();
}
$gateways = (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(Gateway::class."[]", $settings->gateways);
return collect($gateways)->first(function (Gateway $gateway) use ($gateway_type_const){
return $gateway->type == $gateway_type_const;
});
}
public function getGatewayId(int $gateway_type_id): string
{
$gateway = $this->getPaymentGatewayConfiguration($gateway_type_id);
return $gateway->_id;
}
}

View File

@ -25,6 +25,7 @@ use App\Models\ClientGatewayToken;
use Illuminate\Support\Facades\Http;
use App\PaymentDrivers\CBAPowerBoard\CreditCard;
use App\PaymentDrivers\CBAPowerBoard\Customer;
use App\PaymentDrivers\CBAPowerBoard\Settings;
/**
* Class CBAPowerBoardPaymentDriver.
@ -206,4 +207,9 @@ class CBAPowerBoardPaymentDriver extends BaseDriver
{
return new Customer($this);
}
public function settings(): Settings
{
return new Settings($this);
}
}

View File

@ -20,9 +20,6 @@ return new class extends Migration
$fields->publicKey = '';
$fields->secretKey = '';
// $fields->applicationId = '';
// $fields->locationId = '';
$fields->testMode = false;
$fields->Threeds = false;
@ -37,6 +34,10 @@ return new class extends Migration
$powerboard->fields = json_encode($fields);
$powerboard->save();
Schema::table("company_gateways", function (\Illuminate\Database\Schema\Blueprint $table){
$table->text('settings')->nullable();
});
}
/**

View File

@ -18,12 +18,10 @@
<div class="alert alert-failure mb-4" hidden id="errors"></div>
@component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.method')])
<div id="powerboard-payment-container" class="w-full">
<div id="widget" style="block"></div>
<div id="widget-3dsecure"></div>
</div>
@endcomponent
@component('portal.ninja2020.gateways.includes.pay_now', ['id' => 'authorize-card'])
{{ ctrans('texts.add_payment_method') }}
@ -44,7 +42,7 @@
<script src="{{ $widget_endpoint }}"></script>
<script>
var widget = new cba.HtmlWidget('#widget', '{{ $public_key }}', 'not_configured');
var widget = new cba.HtmlWidget('#widget', '{{ $public_key }}', '{{ $gateway_id }}');
widget.setEnv("{{ $environment }}");
widget.useAutoResize();
// widget.interceptSubmitForm('#server-response');

View File

@ -85,7 +85,7 @@
<script src="{{ $widget_endpoint }}"></script>
<script>
var widget = new cba.HtmlWidget('#widget', '{{ $public_key }}', 'not_configured');
var widget = new cba.HtmlWidget('#widget', '{{ $public_key }}', '{{ $gateway_id }}');
widget.setEnv("{{ $environment }}");
widget.useAutoResize();
// widget.interceptSubmitForm('#server-response');