diff --git a/app/PaymentDrivers/CBAPowerBoard/CreditCard.php b/app/PaymentDrivers/CBAPowerBoard/CreditCard.php index c0cb09d235ca..ab369faefeaf 100644 --- a/app/PaymentDrivers/CBAPowerBoard/CreditCard.php +++ b/app/PaymentDrivers/CBAPowerBoard/CreditCard.php @@ -63,18 +63,12 @@ class CreditCard implements LivewireMethodInterface $this->powerboard->init(); $payment_source = $request->gateway_response; - - $customer = $this->powerboard->customer()->findOrCreateCustomer($payment_source); - - nlog($customer); $payload = array_merge($this->getCustomer(), [ 'token' => $payment_source, 'store_ccv' => true, ]); - nlog($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()) return $this->powerboard->processInternallyFailedPayment($this->powerboard, $r->throw()); + nlog("payment source saving"); + $response_payload = $r->object(); + nlog($response_payload); + try { $payment_meta = new \stdClass(); @@ -125,6 +123,42 @@ class CreditCard implements LivewireMethodInterface //['gateway_customer_reference' => $response_payload->resource->data->ref_token] $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; } catch (\Exception $e) { @@ -168,28 +202,52 @@ class CreditCard implements LivewireMethodInterface { nlog($request->all()); + $token = $request->payment_source; + $payload = []; + 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 = [ - '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(), + "amount" => "10.00", + "currency" =>"AUD", + ]; + $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(); diff --git a/app/PaymentDrivers/CBAPowerBoard/Customer.php b/app/PaymentDrivers/CBAPowerBoard/Customer.php index 7ad4266f2e0f..953825c8ebe0 100644 --- a/app/PaymentDrivers/CBAPowerBoard/Customer.php +++ b/app/PaymentDrivers/CBAPowerBoard/Customer.php @@ -21,22 +21,23 @@ class Customer { } - public function findOrCreateCustomer(string $payment_source): mixed + public function findOrCreateCustomer(array $customer_data): mixed { $token = $this->powerboard ->client ->gateway_tokens() + ->whereNotNull('gateway_customer_reference') ->where('company_gateway_id', $this->powerboard->company_gateway->id) ->first(); if($token && $customer = $this->getCustomer($token->gateway_customer_reference)){ - return $customer; + return $customer->resource->data; } if($customer = $this->findCustomer()) return $customer; - return $this->createCustomer(['token' => $payment_source]); + return $this->createCustomer($customer_data); } @@ -65,7 +66,12 @@ class Customer $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 { + nlog("creating customer flow"); // 'address_line1' => $this->powerboard->client->address1 ?? '', // 'address_line2' => $this->powerboard->client->address2 ?? '', @@ -109,12 +116,21 @@ class Customer 'first_name' => $this->powerboard->client->present()->first_name(), 'last_name' => $this->powerboard->client->present()->first_name(), 'email' => $this->powerboard->client->present()->email(), - 'phone' => $this->powerboard->client->present()->phone(), '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); + nlog($payload); + $uri = "/v1/customers"; $r = $this->powerboard->gatewayRequest($uri, (\App\Enum\HttpVerb::POST)->value, $payload, []); @@ -122,7 +138,7 @@ class Customer if($r->successful()) $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 // {