From 2a3c23e25dc14e38dc0d2f0418cdfc5a86703b62 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 15 Feb 2022 15:24:00 +1100 Subject: [PATCH] Working on important Auth.net customers --- .../Authorize/AuthorizeCreditCard.php | 2 +- .../Authorize/AuthorizeCustomer.php | 82 ++++++++++++++++++- app/PaymentDrivers/AuthorizePaymentDriver.php | 6 ++ 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php index 5c5b2e0a5c53..db5d2a6b54e7 100644 --- a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php +++ b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php @@ -80,7 +80,7 @@ class AuthorizeCreditCard $client_gateway_token = $authorise_payment_method->createClientGatewayToken($payment_profile, $gateway_customer_reference); } else{ - //remove the payment profile + //remove the payment profile if we are not storing tokens in our system $this->removePaymentProfile($gateway_customer_reference, $payment_profile_id); } diff --git a/app/PaymentDrivers/Authorize/AuthorizeCustomer.php b/app/PaymentDrivers/Authorize/AuthorizeCustomer.php index 9f54b149b3b9..440e8d7acdcc 100644 --- a/app/PaymentDrivers/Authorize/AuthorizeCustomer.php +++ b/app/PaymentDrivers/Authorize/AuthorizeCustomer.php @@ -13,8 +13,13 @@ namespace App\PaymentDrivers\Authorize; use App\Exceptions\GenericPaymentDriverFailure; +use App\Factory\ClientContactFactory; +use App\Factory\ClientFactory; use App\Models\Client; +use App\Models\ClientContact; +use App\Models\ClientGatewayToken; use App\PaymentDrivers\AuthorizePaymentDriver; +use Illuminate\Support\Facades\Cache; use net\authorize\api\contract\v1\CreateCustomerProfileRequest; use net\authorize\api\contract\v1\CustomerProfileType; use net\authorize\api\contract\v1\GetCustomerProfileIdsRequest; @@ -48,9 +53,6 @@ class AuthorizeCustomer return $response->getIds(); - // echo "GetCustomerProfileId's SUCCESS: " . "\n"; - // $profileIds[] = $response->getIds(); - // echo "There are " . count($profileIds[0]) . " Customer Profile ID's for this Merchant Name and Transaction Key" . "\n"; } else { @@ -77,7 +79,7 @@ class AuthorizeCustomer $paymentProfilesSelected = $profileSelected->getPaymentProfiles(); return [ - 'profile' => $profileSelected, + 'email' => $profileSelected->getEmail(), 'payment_profiles' => $paymentProfilesSelected, 'error' => '' ]; @@ -99,6 +101,78 @@ class AuthorizeCustomer } } + public function importCustomers() + { + $auth_customers = $this->getCustomerProfileIds(); + $company = $this->authorize->company_gateway->company; + $user = $company->owner(); + + foreach($auth_customers as $customer) + { + + + $profile = $this->getCustomerProfile($customer); + + //if the profile ID already exists in ClientGatewayToken we continue else - add. + if($client = ClientGatewayToken::where('company_id', $company->id)->where('gateway_customer_reference', $customer)->first()){ + + } + elseif($client_contact = ClientContact::where('company_id', $company->id)->where('email', $profile['email'])->first()){ + $client = $client_contact->client; + } + else { + + $first_payment_profile = $profile['payment_profiles'][0]; + + $client = ClientFactory::create($company->id, $user->id); + $billTo = $first_payment_profile->getBillTo(); + $client->address1 = $billTo->getAddress(); + $client->city = $billTo->getCity(); + $client->state = $billTo->getState(); + $client->postal_code = $billTo->getZip(); + $client->country_id = $billTo->getCountry() ? $this->getCountryCode($billTo->getCountry()) : $company->settings->country_id; + $client->save(); + + $client_contact = ClientContactFactory::create($company->id, $user->id); + $client_contact->client_id = $client->id; + $client_contact->first_name = $billTo->getFirstName(); + $client_contact->last_name = $billTo->getLastName(); + $client_contact->email = $profile['email']; + $client_contact->phone = $billTo->getPhoneNumber(); + $client_contact->save(); + } + + if($client){ + + foreach($profile['payment_profiles'] as $payment_profile) + { + + $data['payment_profile_id'] = $payment_profile->getCustomerPaymentProfileId(); + $data['card_number'] = $payment_profile->getPayment()->getCreditCard()->getCardNumber(); + $data['card_expiry'] = $payment_profile->getPayment()->getCreditCard()->getExpirationDate(); + $data['card_type'] = $payment_profile->getPayment()->getCreditCard()->getCardType(); + + return $data; + } + } + + } + //iterate through auth.net list + + //exclude any existing customers (ie. only import their missing payment profiles) + + } + + private function getCountryCode($country_code) + { + $countries = Cache::get('countries'); + + $country = $countries->filter(function ($item) use ($country_code) { + return $item->iso_3166_2 == $country_code || $item->iso_3166_3 == $country_code; + })->first(); + + return (string) $country->id; + } } diff --git a/app/PaymentDrivers/AuthorizePaymentDriver.php b/app/PaymentDrivers/AuthorizePaymentDriver.php index 428e1fda1b9d..1bc33c4d0e70 100644 --- a/app/PaymentDrivers/AuthorizePaymentDriver.php +++ b/app/PaymentDrivers/AuthorizePaymentDriver.php @@ -18,6 +18,7 @@ use App\Models\Payment; use App\Models\PaymentHash; use App\Models\SystemLog; use App\PaymentDrivers\Authorize\AuthorizeCreditCard; +use App\PaymentDrivers\Authorize\AuthorizeCustomer; use App\PaymentDrivers\Authorize\AuthorizePaymentMethod; use App\PaymentDrivers\Authorize\RefundTransaction; use net\authorize\api\constants\ANetEnvironment; @@ -159,4 +160,9 @@ class AuthorizePaymentDriver extends BaseDriver { return (new AuthorizePaymentMethod($this))->deletePaymentProfile($token->gateway_customer_reference, $token->token); } + + public function import() + { + return (new AuthorizeCustomer($this))->importCustomers(); + } }