1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 15:42:51 +01:00
invoiceninja/app/PaymentDrivers/CBAPowerBoard/Customer.php

156 lines
4.6 KiB
PHP
Raw Normal View History

2024-09-07 01:34:53 +02:00
<?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\Models\ClientGatewayToken;
2024-09-07 09:27:00 +02:00
use App\PaymentDrivers\CBAPowerBoard\Models\Customer as ModelsCustomer;
2024-09-07 01:34:53 +02:00
use App\PaymentDrivers\CBAPowerBoardPaymentDriver;
class Customer
{
public function __construct(public CBAPowerBoardPaymentDriver $powerboard)
{
}
public function findOrCreateCustomer(array $customer_data): mixed
2024-09-07 01:34:53 +02:00
{
$token = $this->powerboard
->client
->gateway_tokens()
->whereNotNull('gateway_customer_reference')
2024-09-07 01:34:53 +02:00
->where('company_gateway_id', $this->powerboard->company_gateway->id)
->first();
if($token && $customer = $this->getCustomer($token->gateway_customer_reference)){
2024-09-07 09:28:48 +02:00
return (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(ModelsCustomer::class, $customer->resource->data);
2024-09-07 01:34:53 +02:00
}
if($customer = $this->findCustomer())
2024-09-07 09:28:48 +02:00
return (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(ModelsCustomer::class, $customer);
2024-09-07 01:34:53 +02:00
return $this->createCustomer($customer_data);
2024-09-07 01:34:53 +02:00
}
public function getCustomer(string $id): mixed
{
$uri = "/v1/customers/{$id}";
$r = $this->powerboard->gatewayRequest($uri, (\App\Enum\HttpVerb::GET)->value, [], []);
if($r->successful())
return $r->object();
return false;
}
public function findCustomer(): mixed
{
$uri = '/v1/customers';
$query = [
'reference' => $this->powerboard->client->client_hash,
];
$r = $this->powerboard->gatewayRequest($uri, (\App\Enum\HttpVerb::GET)->value, $query, []);
$search_results = $r->object();
$customers = $search_results->resource->data;
return reset($customers); // returns first element or false
2024-09-07 01:34:53 +02:00
}
public function createCustomer(array $data = []): object
{
2024-09-07 09:32:23 +02:00
2024-09-07 01:34:53 +02:00
$payload = [
2024-09-07 09:27:00 +02:00
'company_name' => $this->powerboard->client->present()->name(),
2024-09-07 01:34:53 +02:00
'first_name' => $this->powerboard->client->present()->first_name(),
'last_name' => $this->powerboard->client->present()->first_name(),
'email' => $this->powerboard->client->present()->email(),
'reference' => $this->powerboard->client->client_hash,
'phone' => $this->powerboard->client->present()->phone(),
2024-09-07 01:34:53 +02:00
];
foreach($payload as $key => $value){
if(strlen($value ?? '') == 0)
unset($payload[$key]);
}
2024-09-07 01:34:53 +02:00
$payload = array_merge($payload, $data);
nlog($payload);
2024-09-07 01:34:53 +02:00
$uri = "/v1/customers";
$r = $this->powerboard->gatewayRequest($uri, (\App\Enum\HttpVerb::POST)->value, $payload, []);
if($r->successful())
$this->storePaymentMethod($r->object());
2024-09-07 09:28:48 +02:00
return (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(ModelsCustomer::class, $r->object()->resource->data) ?? $r->throw();
2024-09-07 01:34:53 +02:00
}
private function storePaymentMethod(mixed $customer): ClientGatewayToken
{
$response_payload = $customer->resource->data;
$source = end($customer->resource->data->payment_sources);
$payment_meta = new \stdClass();
$payment_meta->exp_month = (string) $source->expire_month;
$payment_meta->exp_year = (string) $source->expire_year;
$payment_meta->brand = (string) $source->card_scheme;
$payment_meta->last4 = (string) $source->card_number_last4;
$payment_meta->gateway_id = (string) $source->gateway_id;
$payment_meta->type = \App\Models\GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $source->vault_token,
'payment_method_id' => \App\Models\GatewayType::CREDIT_CARD,
];
$cgt = $this->powerboard->storeGatewayToken($data, ['gateway_customer_reference' => $response_payload->_id]);
return $cgt;
}
public function addTokenToCustomer(string $token, mixed $customer): mixed
{
$uri = "/v1/customers/{$customer->_id}";
$payload = [
'payment_source' => [
'vault_token' => $token,
]
];
$r = $this->powerboard->gatewayRequest($uri, (\App\Enum\HttpVerb::POST)->value, $payload, []);
return $r->successful() ? $r->object() : $r->throw();
}
2024-09-07 01:34:53 +02:00
}