From a9637b3cb557c885d2d225e5c871f627beed5420 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 10 Jun 2020 22:42:10 +1000 Subject: [PATCH] Authorize a credit card --- .../Authorize/AuthorizePaymentMethod.php | 62 ++++++++++++++++--- app/PaymentDrivers/AuthorizePaymentDriver.php | 9 --- app/PaymentDrivers/BaseDriver.php | 2 +- config/beacon.php | 2 +- 4 files changed, 56 insertions(+), 19 deletions(-) diff --git a/app/PaymentDrivers/Authorize/AuthorizePaymentMethod.php b/app/PaymentDrivers/Authorize/AuthorizePaymentMethod.php index 16ec873c1343..8343a8c85470 100644 --- a/app/PaymentDrivers/Authorize/AuthorizePaymentMethod.php +++ b/app/PaymentDrivers/Authorize/AuthorizePaymentMethod.php @@ -22,10 +22,12 @@ use net\authorize\api\contract\v1\CreateCustomerProfileRequest; use net\authorize\api\contract\v1\CustomerAddressType; use net\authorize\api\contract\v1\CustomerPaymentProfileType; use net\authorize\api\contract\v1\CustomerProfileType; +use net\authorize\api\contract\v1\GetCustomerPaymentProfileRequest; use net\authorize\api\contract\v1\OpaqueDataType; use net\authorize\api\contract\v1\PaymentType; use net\authorize\api\controller\CreateCustomerPaymentProfileController; use net\authorize\api\controller\CreateCustomerProfileController; +use net\authorize\api\controller\GetCustomerPaymentProfileController; /** * Class AuthorizePaymentMethod @@ -64,6 +66,7 @@ class AuthorizePaymentMethod public function authorizeResponseView($payment_method, $data) { + $this->payment_method = $payment_method; switch ($payment_method) { case GatewayType::CREDIT_CARD: @@ -98,10 +101,13 @@ class AuthorizePaymentMethod { $client_profile_id = null; - if($client_gateway_token = $this->authorize->findClientGatewayRecord()) + if($client_gateway_token = $this->authorize->findClientGatewayRecord()){ + info("i found a company gateway record"); $payment_profile = $this->addPaymentMethodToClient($client_gateway_token->gateway_customer_reference, $data); + } else{ $gateway_customer_reference = (new AuthorizeCreateCustomer($this->authorize, $this->authorize->client))->create($data); + info($gateway_customer_reference); $payment_profile = $this->addPaymentMethodToClient($gateway_customer_reference, $data); } @@ -118,10 +124,12 @@ class AuthorizePaymentMethod private function createClientGatewayToken($payment_profile, $gateway_customer_reference) { + info(print_r($payment_profile,1)); + $client_gateway_token = new ClientGatewayToken(); $client_gateway_token->company_id = $this->authorize->client->company_id; $client_gateway_token->client_id = $this->authorize->client->id; - $client_gateway_token->token = $payment_profile->getCustomerPaymentProfileId(); + $client_gateway_token->token = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId(); $client_gateway_token->company_gateway_id = $this->authorize->company_gateway->id; $client_gateway_token->gateway_type_id = $this->payment_method; $client_gateway_token->gateway_customer_reference = $gateway_customer_reference; @@ -134,8 +142,8 @@ class AuthorizePaymentMethod $payment_meta = new \stdClass; $payment_meta->exp_month = 'xx'; $payment_meta->exp_year = 'xx'; - $payment_meta->brand = $payment_profile->getCardType(); - $payment_meta->last4 = $payment_profile->getCardNumber(); + $payment_meta->brand = $payment_profile->getPaymentProfile()->getPayment()->getCreditCard()->getCardType(); + $payment_meta->last4 = $payment_profile->getPaymentProfile()->getPayment()->getCreditCard()->getCardNumber(); $payment_meta->type = $this->payment_method; return $payment_meta; @@ -146,7 +154,7 @@ class AuthorizePaymentMethod error_reporting (E_ALL & ~E_DEPRECATED); - $merchantAuthentication = $this->authorize->init(); + $this->authorize->init(); // Set the transaction's refId $refId = 'ref' . time(); @@ -190,10 +198,10 @@ class AuthorizePaymentMethod // Assemble the complete transaction request $paymentprofilerequest = new CreateCustomerPaymentProfileRequest(); - $paymentprofilerequest->setMerchantAuthentication($merchantAuthentication); + $paymentprofilerequest->setMerchantAuthentication($this->authorize->merchant_authentication); // Add an existing profile id to the request - $paymentprofilerequest->setCustomerProfileId($gateway_customer_references); + $paymentprofilerequest->setCustomerProfileId($gateway_customer_reference); $paymentprofilerequest->setPaymentProfile($paymentprofile); $paymentprofilerequest->setValidationMode("liveMode"); @@ -202,7 +210,7 @@ class AuthorizePaymentMethod $response = $controller->executeWithApiResponse($this->authorize->mode()); if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") ) { - return $response; + return $this->getPaymentProfile($gateway_customer_reference, $response->getCustomerPaymentProfileId()); } else { $errorMessages = $response->getMessages()->getMessage(); @@ -217,5 +225,43 @@ class AuthorizePaymentMethod } } + + public function getPaymentProfile($gateway_customer_reference, $payment_profile_id) + { + + error_reporting (E_ALL & ~E_DEPRECATED); + + $this->authorize->init(); + + // Set the transaction's refId + $refId = 'ref' . time(); + + //request requires customerProfileId and customerPaymentProfileId + $request = new GetCustomerPaymentProfileRequest(); + $request->setMerchantAuthentication($this->authorize->merchant_authentication); + $request->setRefId($refId); + $request->setCustomerProfileId($gateway_customer_reference); + $request->setCustomerPaymentProfileId($payment_profile_id); + + $controller = new GetCustomerPaymentProfileController($request); + $response = $controller->executeWithApiResponse($this->authorize->mode()); + + if(($response != null) && ($response->getMessages()->getResultCode() == "Ok")) { + return $response; + } + else if($response){ + + $errorMessages = $response->getMessages()->getMessage(); + + $message = "Unable to add payment method to Authorize.net gateway"; + + if(is_array($errorMessages)) + $message = $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText(); + + throw new GenericPaymentDriverFailure($message); + } + else + throw new GenericPaymentDriverFailure("Error communicating with Authorize.net"); + } } diff --git a/app/PaymentDrivers/AuthorizePaymentDriver.php b/app/PaymentDrivers/AuthorizePaymentDriver.php index 8cfd0cc84445..af585d035b37 100644 --- a/app/PaymentDrivers/AuthorizePaymentDriver.php +++ b/app/PaymentDrivers/AuthorizePaymentDriver.php @@ -91,15 +91,6 @@ class AuthorizePaymentDriver extends BaseDriver } - // public function fire() - // { - - // $controller = new CreateTransactionController($this->anet); - // $response = $controller->executeWithApiResponse($env); - - // return $response; - // } - public function authorize($payment_method) { diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 18a2d62f50ab..43cf26c9ab58 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -45,7 +45,7 @@ class BaseDriver extends AbstractPaymentDriver protected $can_authorise_credit_card = false; - public function __construct(CompanyGateway $company_gateway, Client $client, $invitation = false) + public function __construct(CompanyGateway $company_gateway, Client $client = null, $invitation = false) { $this->company_gateway = $company_gateway; diff --git a/config/beacon.php b/config/beacon.php index b4bc4fd80d38..884266b8f62c 100644 --- a/config/beacon.php +++ b/config/beacon.php @@ -36,7 +36,7 @@ return [ 'system_logging' => [ 'Turbo124\Beacon\Jobs\System\CpuMetric', 'Turbo124\Beacon\Jobs\System\HdMetric', - 'Turbo124\Beacon\Jobs\System\MemMetric', + 'Turbo124\Beacon\Jobs\System\MemMetric' ], ]; \ No newline at end of file