From b715c7dd204e6a71a5065df1dcf36f879fa20a31 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 Sep 2024 08:40:40 +1000 Subject: [PATCH] powerboardOM --- app/Models/CompanyGateway.php | 14 +++ .../CBAPowerBoard/CreditCard.php | 75 ++++++++++----- .../CBAPowerBoard/Models/Customers.php | 18 ---- .../CBAPowerBoard/Models/Gateway.php | 56 +++++++++++ app/PaymentDrivers/CBAPowerBoard/Settings.php | 96 +++++++++++++++++++ .../CBAPowerBoardPaymentDriver.php | 8 +- .../2024_09_06_042040_cba_powerboard.php | 7 +- .../credit_card/authorize.blade.php | 4 +- .../powerboard/credit_card/pay.blade.php | 2 +- 9 files changed, 232 insertions(+), 48 deletions(-) delete mode 100644 app/PaymentDrivers/CBAPowerBoard/Models/Customers.php create mode 100644 app/PaymentDrivers/CBAPowerBoard/Models/Gateway.php create mode 100644 app/PaymentDrivers/CBAPowerBoard/Settings.php diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index f8ff6118490f..73dbae6617d7 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -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]); diff --git a/app/PaymentDrivers/CBAPowerBoard/CreditCard.php b/app/PaymentDrivers/CBAPowerBoard/CreditCard.php index 5ba8cab91516..87e2577ff8b5 100644 --- a/app/PaymentDrivers/CBAPowerBoard/CreditCard.php +++ b/app/PaymentDrivers/CBAPowerBoard/CreditCard.php @@ -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,30 +105,33 @@ 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, - ], - ]; - - $customer = $this->powerboard->customer()->findOrCreateCustomer($data); - $cgt = $this->powerboard->customer()->storePaymentMethod($charge->customer->payment_source, $charge->customer); - } + $data = [ + "payment_source" => [ + "vault_token" => $vt, + ], + ]; + + $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); diff --git a/app/PaymentDrivers/CBAPowerBoard/Models/Customers.php b/app/PaymentDrivers/CBAPowerBoard/Models/Customers.php deleted file mode 100644 index ea3c5ac2f27a..000000000000 --- a/app/PaymentDrivers/CBAPowerBoard/Models/Customers.php +++ /dev/null @@ -1,18 +0,0 @@ -_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; + } +} diff --git a/app/PaymentDrivers/CBAPowerBoard/Settings.php b/app/PaymentDrivers/CBAPowerBoard/Settings.php new file mode 100644 index 000000000000..945a72f48ea7 --- /dev/null +++ b/app/PaymentDrivers/CBAPowerBoard/Settings.php @@ -0,0 +1,96 @@ +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; + } +} diff --git a/app/PaymentDrivers/CBAPowerBoardPaymentDriver.php b/app/PaymentDrivers/CBAPowerBoardPaymentDriver.php index ae301b650330..5e0b64f0af5e 100644 --- a/app/PaymentDrivers/CBAPowerBoardPaymentDriver.php +++ b/app/PaymentDrivers/CBAPowerBoardPaymentDriver.php @@ -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); + } +} \ No newline at end of file diff --git a/database/migrations/2024_09_06_042040_cba_powerboard.php b/database/migrations/2024_09_06_042040_cba_powerboard.php index bfeba4de60e1..bcccbb2d2dfd 100644 --- a/database/migrations/2024_09_06_042040_cba_powerboard.php +++ b/database/migrations/2024_09_06_042040_cba_powerboard.php @@ -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(); + }); } /** diff --git a/resources/views/portal/ninja2020/gateways/powerboard/credit_card/authorize.blade.php b/resources/views/portal/ninja2020/gateways/powerboard/credit_card/authorize.blade.php index 3d78eb6d9345..e13ccff69d71 100644 --- a/resources/views/portal/ninja2020/gateways/powerboard/credit_card/authorize.blade.php +++ b/resources/views/portal/ninja2020/gateways/powerboard/credit_card/authorize.blade.php @@ -18,12 +18,10 @@ - @component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.method')])
- @endcomponent @component('portal.ninja2020.gateways.includes.pay_now', ['id' => 'authorize-card']) {{ ctrans('texts.add_payment_method') }} @@ -44,7 +42,7 @@