Add payment method and attach to customer

This commit is contained in:
David Bomba 2024-09-07 16:37:20 +10:00
parent 9053e6ac7e
commit 78367c1e2d
2 changed files with 119 additions and 28 deletions

View File

@ -64,17 +64,11 @@ class CreditCard implements LivewireMethodInterface
$payment_source = $request->gateway_response; $payment_source = $request->gateway_response;
$customer = $this->powerboard->customer()->findOrCreateCustomer($payment_source);
nlog($customer);
$payload = array_merge($this->getCustomer(), [ $payload = array_merge($this->getCustomer(), [
'token' => $payment_source, 'token' => $payment_source,
'store_ccv' => true, 'store_ccv' => true,
]); ]);
nlog($payload);
$r = $this->powerboard->gatewayRequest('/v1/vault/payment_sources', (\App\Enum\HttpVerb::POST)->value, $payload, []); $r = $this->powerboard->gatewayRequest('/v1/vault/payment_sources', (\App\Enum\HttpVerb::POST)->value, $payload, []);
// { // {
@ -105,8 +99,12 @@ class CreditCard implements LivewireMethodInterface
if($r->failed()) if($r->failed())
return $this->powerboard->processInternallyFailedPayment($this->powerboard, $r->throw()); return $this->powerboard->processInternallyFailedPayment($this->powerboard, $r->throw());
nlog("payment source saving");
$response_payload = $r->object(); $response_payload = $r->object();
nlog($response_payload);
try { try {
$payment_meta = new \stdClass(); $payment_meta = new \stdClass();
@ -125,6 +123,42 @@ class CreditCard implements LivewireMethodInterface
//['gateway_customer_reference' => $response_payload->resource->data->ref_token] //['gateway_customer_reference' => $response_payload->resource->data->ref_token]
$cgt = $this->powerboard->storeGatewayToken($data, []); $cgt = $this->powerboard->storeGatewayToken($data, []);
$customer_payload = [
'payment_source' => [
'vault_token' => $cgt->token,
'address_line1' => $this->powerboard->client->address1 ?? '',
'address_line2' => $this->powerboard->client->address1 ?? '',
'address_state' => $this->powerboard->client->state ?? '',
'address_country' => $this->powerboard->client->country->iso_3166_3 ?? '',
'address_city' => $this->powerboard->client->city ?? '',
'address_postcode' => $this->powerboard->client->postcode ?? '',
],
];
foreach ($customer_payload['payment_source'] as $key => $value) {
if (strlen($value ?? '') == 0) {
unset($customer_payload['payment_source'][$key]);
}
}
$customer = $this->powerboard->customer()->findOrCreateCustomer($customer_payload);
$cgt->gateway_customer_reference = $customer->_id;
$cgt->save();
//test that payment token is attached to customer here
$hit=false;
foreach($customer->payment_sources as $source){
if($source->vault_token == $cgt->token)
$hit = true;
}
if(!$hit)
$this->powerboard->customer()->addTokenToCustomer($cgt->token, $customer);
return $cgt; return $cgt;
} catch (\Exception $e) { } catch (\Exception $e) {
@ -168,28 +202,52 @@ class CreditCard implements LivewireMethodInterface
{ {
nlog($request->all()); nlog($request->all());
$token = $request->payment_source;
$payload = [];
if($request->store_card) { if($request->store_card) {
$cgt = $this->storePaymentMethod($request);
$this->tokenBilling($request, $cgt, true); nlog("Store Payment Method");
$customer = $this->storePaymentMethod($request);
nlog($customer);
$payload["customer"] = [
"payment_source" => [
"vault_token" => "c90dbe45-7a23-4f26-9192-336a01e58e59",
"gateway_id" => "5dde1f3799cfea21ed2fc942"
]
];
} }
$uri = '/v1/charges';
$payload = [ $payload = [
'amount' => $this->powerboard->payment_hash->amount_with_fee(), "amount" => "10.00",
'currency' => $this->powerboard->client->currency()->code, "currency" =>"AUD",
'description' => $this->powerboard->getDescription(),
// 'descriptor' => ,
// 'reference' => ,
// 'reference2' => ,
// 'amount_surcharge' => ,
// 'amount_original' => ,
// 'initialization_source' => ,
'bypass_3ds' => false,
// 'token'=> ,
'payment_source_id' => $request->payment_source,
// 'customer_id' => ,
'customer' => $this->getCustomer(),
]; ];
$r = $this->powerboard->gatewayRequest($uri, (\App\Enum\HttpVerb::POST)->value, $payload, []);
// $payload = [
// 'amount' => $this->powerboard->payment_hash->amount_with_fee(),
// 'currency' => $this->powerboard->client->currency()->code,
// 'description' => $this->powerboard->getDescription(),
// // 'descriptor' => ,
// // 'reference' => ,
// // 'reference2' => ,
// // 'amount_surcharge' => ,
// // 'amount_original' => ,
// // 'initialization_source' => ,
// 'bypass_3ds' => false,
// // 'token'=> ,
// 'payment_source_id' => $request->payment_source,
// // 'customer_id' => ,
// 'customer' => $this->getCustomer(),
// ];
// $this->stripe->init(); // $this->stripe->init();

View File

@ -21,22 +21,23 @@ class Customer
{ {
} }
public function findOrCreateCustomer(string $payment_source): mixed public function findOrCreateCustomer(array $customer_data): mixed
{ {
$token = $this->powerboard $token = $this->powerboard
->client ->client
->gateway_tokens() ->gateway_tokens()
->whereNotNull('gateway_customer_reference')
->where('company_gateway_id', $this->powerboard->company_gateway->id) ->where('company_gateway_id', $this->powerboard->company_gateway->id)
->first(); ->first();
if($token && $customer = $this->getCustomer($token->gateway_customer_reference)){ if($token && $customer = $this->getCustomer($token->gateway_customer_reference)){
return $customer; return $customer->resource->data;
} }
if($customer = $this->findCustomer()) if($customer = $this->findCustomer())
return $customer; return $customer;
return $this->createCustomer(['token' => $payment_source]); return $this->createCustomer($customer_data);
} }
@ -65,7 +66,12 @@ class Customer
$search_results = $r->object(); $search_results = $r->object();
return reset($search_results->resource->data); // returns first element or false nlog("find customer");
nlog($search_results);
$customers = $search_results->resource->data;
nlog($customers);
return reset($customers); // returns first element or false
} }
@ -97,6 +103,7 @@ class Customer
*/ */
public function createCustomer(array $data = []): object public function createCustomer(array $data = []): object
{ {
nlog("creating customer flow");
// 'address_line1' => $this->powerboard->client->address1 ?? '', // 'address_line1' => $this->powerboard->client->address1 ?? '',
// 'address_line2' => $this->powerboard->client->address2 ?? '', // 'address_line2' => $this->powerboard->client->address2 ?? '',
@ -109,12 +116,21 @@ class Customer
'first_name' => $this->powerboard->client->present()->first_name(), 'first_name' => $this->powerboard->client->present()->first_name(),
'last_name' => $this->powerboard->client->present()->first_name(), 'last_name' => $this->powerboard->client->present()->first_name(),
'email' => $this->powerboard->client->present()->email(), 'email' => $this->powerboard->client->present()->email(),
'phone' => $this->powerboard->client->present()->phone(),
'reference' => $this->powerboard->client->client_hash, 'reference' => $this->powerboard->client->client_hash,
'phone' => $this->powerboard->client->present()->phone(),
]; ];
foreach($payload as $key => $value){
if(strlen($value ?? '') == 0)
unset($payload[$key]);
}
$payload = array_merge($payload, $data); $payload = array_merge($payload, $data);
nlog($payload);
$uri = "/v1/customers"; $uri = "/v1/customers";
$r = $this->powerboard->gatewayRequest($uri, (\App\Enum\HttpVerb::POST)->value, $payload, []); $r = $this->powerboard->gatewayRequest($uri, (\App\Enum\HttpVerb::POST)->value, $payload, []);
@ -122,7 +138,7 @@ class Customer
if($r->successful()) if($r->successful())
$this->storePaymentMethod($r->object()); $this->storePaymentMethod($r->object());
return $r->object() ?? $r->throw(); return $r->object()->resource->data ?? $r->throw();
} }
@ -206,6 +222,23 @@ class Customer
} }
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();
}
// public function updateCustomer(string $id, $data): object // public function updateCustomer(string $id, $data): object
// { // {