1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Authorize a credit card

This commit is contained in:
David Bomba 2020-06-10 22:42:10 +10:00
parent 78f99140d8
commit a9637b3cb5
4 changed files with 56 additions and 19 deletions

View File

@ -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");
}
}

View File

@ -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)
{

View File

@ -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;

View File

@ -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'
],
];