2020-06-10 03:06:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-06-10 03:06:37 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-06-10 03:06:37 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-06-10 03:06:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Authorize;
|
|
|
|
|
2020-06-10 07:21:11 +02:00
|
|
|
use App\Exceptions\GenericPaymentDriverFailure;
|
2021-01-26 06:37:45 +01:00
|
|
|
use App\Exceptions\PaymentFailed;
|
2020-06-10 03:06:37 +02:00
|
|
|
use App\Models\GatewayType;
|
2020-09-06 11:38:10 +02:00
|
|
|
use App\PaymentDrivers\AuthorizePaymentDriver;
|
2020-06-10 07:21:11 +02:00
|
|
|
use net\authorize\api\contract\v1\CreateCustomerPaymentProfileRequest;
|
|
|
|
use net\authorize\api\contract\v1\CustomerAddressType;
|
|
|
|
use net\authorize\api\contract\v1\CustomerPaymentProfileType;
|
2021-01-25 13:16:43 +01:00
|
|
|
use net\authorize\api\contract\v1\DeleteCustomerPaymentProfileRequest;
|
2020-06-10 14:42:10 +02:00
|
|
|
use net\authorize\api\contract\v1\GetCustomerPaymentProfileRequest;
|
2020-06-10 07:21:11 +02:00
|
|
|
use net\authorize\api\contract\v1\OpaqueDataType;
|
|
|
|
use net\authorize\api\contract\v1\PaymentType;
|
|
|
|
use net\authorize\api\controller\CreateCustomerPaymentProfileController;
|
2021-01-25 13:16:43 +01:00
|
|
|
use net\authorize\api\controller\DeleteCustomerPaymentProfileController;
|
2020-06-10 14:42:10 +02:00
|
|
|
use net\authorize\api\controller\GetCustomerPaymentProfileController;
|
2020-10-28 11:10:49 +01:00
|
|
|
use stdClass;
|
2020-06-10 03:06:37 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class AuthorizePaymentMethod.
|
2020-06-10 03:06:37 +02:00
|
|
|
*/
|
|
|
|
class AuthorizePaymentMethod
|
|
|
|
{
|
|
|
|
public $authorize;
|
|
|
|
|
2020-06-10 07:21:11 +02:00
|
|
|
public $payment_method;
|
|
|
|
|
2020-11-26 03:30:36 +01:00
|
|
|
private $payment_method_id;
|
|
|
|
|
2020-06-10 03:06:37 +02:00
|
|
|
public function __construct(AuthorizePaymentDriver $authorize)
|
|
|
|
{
|
|
|
|
$this->authorize = $authorize;
|
|
|
|
}
|
|
|
|
|
2020-11-26 03:30:36 +01:00
|
|
|
public function authorizeView()
|
2020-06-10 03:06:37 +02:00
|
|
|
{
|
2020-11-27 12:08:42 +01:00
|
|
|
if ($this->authorize->payment_method instanceof AuthorizeCreditCard) {
|
2020-11-26 03:30:36 +01:00
|
|
|
$this->payment_method_id = GatewayType::CREDIT_CARD;
|
|
|
|
|
|
|
|
return $this->authorizeCreditCard();
|
2020-06-10 03:06:37 +02:00
|
|
|
}
|
2020-11-26 03:30:36 +01:00
|
|
|
|
|
|
|
// case GatewayType::BANK_TRANSFER:
|
|
|
|
// return $this->authorizeBankTransfer();
|
|
|
|
// break;
|
2020-06-10 03:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-26 11:00:55 +01:00
|
|
|
public function authorizeResponseView($request)
|
2020-06-10 03:06:37 +02:00
|
|
|
{
|
2020-11-26 11:00:55 +01:00
|
|
|
$data = $request->all();
|
2021-01-19 13:43:03 +01:00
|
|
|
|
2020-11-26 11:12:36 +01:00
|
|
|
$this->payment_method_id = $data['method'];
|
2020-06-10 03:06:37 +02:00
|
|
|
|
2020-11-26 12:22:33 +01:00
|
|
|
switch ($this->payment_method_id) {
|
2020-06-10 03:06:37 +02:00
|
|
|
case GatewayType::CREDIT_CARD:
|
|
|
|
return $this->authorizeCreditCardResponse($data);
|
|
|
|
break;
|
|
|
|
case GatewayType::BANK_TRANSFER:
|
|
|
|
return $this->authorizeBankTransferResponse($data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-09-06 11:38:10 +02:00
|
|
|
// code...
|
2020-06-10 03:06:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeCreditCard()
|
|
|
|
{
|
2021-01-26 13:29:27 +01:00
|
|
|
$data['gateway'] = $this->authorize;
|
2020-06-10 03:06:37 +02:00
|
|
|
$data['public_client_id'] = $this->authorize->init()->getPublicClientKey();
|
|
|
|
$data['api_login_id'] = $this->authorize->company_gateway->getConfigField('apiLoginId');
|
|
|
|
|
2021-01-19 13:43:03 +01:00
|
|
|
return render('gateways.authorize.credit_card.authorize', $data);
|
2020-06-10 03:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeBankTransfer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeCreditCardResponse($data)
|
|
|
|
{
|
2020-06-10 07:21:11 +02:00
|
|
|
$client_profile_id = null;
|
2020-06-10 03:06:37 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($client_gateway_token = $this->authorize->findClientGatewayRecord()) {
|
2020-06-10 07:21:11 +02:00
|
|
|
$payment_profile = $this->addPaymentMethodToClient($client_gateway_token->gateway_customer_reference, $data);
|
2021-01-25 11:34:12 +01:00
|
|
|
$gateway_customer_reference = $client_gateway_token->gateway_customer_reference;
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-06-10 10:05:30 +02:00
|
|
|
$gateway_customer_reference = (new AuthorizeCreateCustomer($this->authorize, $this->authorize->client))->create($data);
|
2020-06-10 07:21:11 +02:00
|
|
|
$payment_profile = $this->addPaymentMethodToClient($gateway_customer_reference, $data);
|
2021-01-25 16:46:40 +01:00
|
|
|
|
2020-06-10 03:06:37 +02:00
|
|
|
}
|
|
|
|
|
2024-04-02 09:31:51 +02:00
|
|
|
$this->createClientGatewayToken($payment_profile, $gateway_customer_reference);
|
|
|
|
|
2020-06-10 03:06:37 +02:00
|
|
|
return redirect()->route('client.payment_methods.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeBankTransferResponse($data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:46:12 +02:00
|
|
|
public function createClientGatewayToken($payment_profile, $gateway_customer_reference)
|
2020-06-10 03:06:37 +02:00
|
|
|
{
|
2020-11-26 03:30:36 +01:00
|
|
|
$data = [];
|
|
|
|
$additonal = [];
|
|
|
|
|
|
|
|
$data['token'] = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId();
|
|
|
|
$data['payment_method_id'] = $this->payment_method_id;
|
|
|
|
$data['payment_meta'] = $this->buildPaymentMethod($payment_profile);
|
2021-01-25 16:12:21 +01:00
|
|
|
$data['payment_method_id'] = GatewayType::CREDIT_CARD;
|
2020-11-26 03:30:36 +01:00
|
|
|
|
|
|
|
$additional['gateway_customer_reference'] = $gateway_customer_reference;
|
|
|
|
|
|
|
|
$this->authorize->storeGatewayToken($data, $additional);
|
2020-06-10 07:21:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function buildPaymentMethod($payment_profile)
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
$payment_meta = new stdClass();
|
2020-06-10 07:28:41 +02:00
|
|
|
$payment_meta->exp_month = 'xx';
|
|
|
|
$payment_meta->exp_year = 'xx';
|
2022-06-21 11:57:17 +02:00
|
|
|
$payment_meta->brand = (string) $payment_profile->getPaymentProfile()->getPayment()->getCreditCard()->getCardType();
|
|
|
|
$payment_meta->last4 = (string) $payment_profile->getPaymentProfile()->getPayment()->getCreditCard()->getCardNumber();
|
2020-06-10 07:21:11 +02:00
|
|
|
$payment_meta->type = $this->payment_method;
|
|
|
|
|
|
|
|
return $payment_meta;
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:46:12 +02:00
|
|
|
public function addPaymentMethodToClient($gateway_customer_reference, $data)
|
2020-06-10 07:21:11 +02:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
error_reporting(E_ALL & ~E_DEPRECATED);
|
2020-06-10 07:21:11 +02:00
|
|
|
|
2020-06-10 14:42:10 +02:00
|
|
|
$this->authorize->init();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-10 07:21:11 +02:00
|
|
|
// Set the transaction's refId
|
2020-09-06 11:38:10 +02:00
|
|
|
$refId = 'ref'.time();
|
2020-06-10 07:21:11 +02:00
|
|
|
|
|
|
|
// Set the payment data for the payment profile to a token obtained from Accept.js
|
|
|
|
$op = new OpaqueDataType();
|
|
|
|
$op->setDataDescriptor($data['dataDescriptor']);
|
|
|
|
$op->setDataValue($data['dataValue']);
|
|
|
|
$paymentOne = new PaymentType();
|
|
|
|
$paymentOne->setOpaqueData($op);
|
|
|
|
|
2022-01-27 06:15:30 +01:00
|
|
|
$contact = $this->authorize->client->primary_contact()->first() ?: $this->authorize->client->contacts()->first();
|
|
|
|
|
2022-01-06 05:58:55 +01:00
|
|
|
$billto = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($contact) {
|
|
|
|
// Create the Bill To info for new payment type
|
2020-06-10 07:21:11 +02:00
|
|
|
$billto = new CustomerAddressType();
|
2022-06-21 11:57:17 +02:00
|
|
|
$billto->setFirstName(substr($contact->present()->first_name(), 0, 50));
|
|
|
|
$billto->setLastName(substr($contact->present()->last_name(), 0, 50));
|
|
|
|
$billto->setCompany(substr($this->authorize->client->present()->name(), 0, 50));
|
|
|
|
$billto->setAddress(substr($this->authorize->client->address1, 0, 60));
|
|
|
|
$billto->setCity(substr($this->authorize->client->city, 0, 40));
|
|
|
|
$billto->setState(substr($this->authorize->client->state, 0, 40));
|
|
|
|
$billto->setZip(substr($this->authorize->client->postal_code, 0, 20));
|
2024-06-14 09:09:44 +02:00
|
|
|
|
|
|
|
if(isset($contact->email) && str_contains($contact->email, '@')) {
|
2024-04-02 09:31:51 +02:00
|
|
|
$billto->setEmail($contact->email);
|
2024-06-14 09:09:44 +02:00
|
|
|
}
|
2020-06-10 10:05:30 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->authorize->client->country_id) {
|
2020-06-10 10:05:30 +02:00
|
|
|
$billto->setCountry($this->authorize->client->country->name);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$billto->setPhoneNumber(substr($this->authorize->client->phone, 0, 20));
|
2020-06-10 07:21:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Customer Payment Profile object
|
|
|
|
$paymentprofile = new CustomerPaymentProfileType();
|
|
|
|
$paymentprofile->setCustomerType('individual');
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($billto) {
|
2020-06-10 07:21:11 +02:00
|
|
|
$paymentprofile->setBillTo($billto);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-06-10 07:21:11 +02:00
|
|
|
|
|
|
|
$paymentprofile->setPayment($paymentOne);
|
|
|
|
$paymentprofile->setDefaultPaymentProfile(true);
|
|
|
|
$paymentprofiles[] = $paymentprofile;
|
|
|
|
|
|
|
|
// Assemble the complete transaction request
|
|
|
|
$paymentprofilerequest = new CreateCustomerPaymentProfileRequest();
|
2020-06-10 14:42:10 +02:00
|
|
|
$paymentprofilerequest->setMerchantAuthentication($this->authorize->merchant_authentication);
|
2020-06-10 07:21:11 +02:00
|
|
|
|
|
|
|
// Add an existing profile id to the request
|
2020-06-10 14:42:10 +02:00
|
|
|
$paymentprofilerequest->setCustomerProfileId($gateway_customer_reference);
|
2020-06-10 07:21:11 +02:00
|
|
|
$paymentprofilerequest->setPaymentProfile($paymentprofile);
|
2020-09-06 11:38:10 +02:00
|
|
|
$paymentprofilerequest->setValidationMode('liveMode');
|
2020-06-10 07:21:11 +02:00
|
|
|
|
|
|
|
// Create the controller and get the response
|
|
|
|
$controller = new CreateCustomerPaymentProfileController($paymentprofilerequest);
|
|
|
|
$response = $controller->executeWithApiResponse($this->authorize->mode());
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (($response != null) && ($response->getMessages()->getResultCode() == 'Ok')) {
|
2020-06-10 14:42:10 +02:00
|
|
|
return $this->getPaymentProfile($gateway_customer_reference, $response->getCustomerPaymentProfileId());
|
2020-06-10 07:21:11 +02:00
|
|
|
} else {
|
|
|
|
$errorMessages = $response->getMessages()->getMessage();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$message = 'Unable to add customer to Authorize.net gateway';
|
2020-06-10 07:21:11 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (is_array($errorMessages)) {
|
|
|
|
$message = $errorMessages[0]->getCode().' '.$errorMessages[0]->getText();
|
|
|
|
}
|
2020-06-10 07:21:11 +02:00
|
|
|
|
2021-01-26 06:37:45 +01:00
|
|
|
throw new PaymentFailed($message, 500);
|
2020-06-10 07:21:11 +02:00
|
|
|
}
|
2020-06-10 03:06:37 +02:00
|
|
|
}
|
2020-06-10 14:42:10 +02:00
|
|
|
|
|
|
|
public function getPaymentProfile($gateway_customer_reference, $payment_profile_id)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
error_reporting(E_ALL & ~E_DEPRECATED);
|
2020-06-10 14:42:10 +02:00
|
|
|
|
|
|
|
$this->authorize->init();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-10 14:42:10 +02:00
|
|
|
// Set the transaction's refId
|
2020-09-06 11:38:10 +02:00
|
|
|
$refId = 'ref'.time();
|
2020-06-10 14:42:10 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
//request requires customerProfileId and customerPaymentProfileId
|
2020-06-10 14:42:10 +02:00
|
|
|
$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());
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (($response != null) && ($response->getMessages()->getResultCode() == 'Ok')) {
|
2020-06-10 14:42:10 +02:00
|
|
|
return $response;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($response) {
|
2020-06-10 14:42:10 +02:00
|
|
|
$errorMessages = $response->getMessages()->getMessage();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$message = 'Unable to add payment method to Authorize.net gateway';
|
2020-06-10 14:42:10 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (is_array($errorMessages)) {
|
|
|
|
$message = $errorMessages[0]->getCode().' '.$errorMessages[0]->getText();
|
|
|
|
}
|
2020-06-10 14:42:10 +02:00
|
|
|
|
|
|
|
throw new GenericPaymentDriverFailure($message);
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
|
|
|
throw new GenericPaymentDriverFailure('Error communicating with Authorize.net');
|
2020-06-10 14:42:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-25 13:16:43 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
public function deletePaymentProfile($gateway_customer_reference, $payment_profile_id)
|
|
|
|
{
|
2021-01-25 13:16:43 +01:00
|
|
|
error_reporting(E_ALL & ~E_DEPRECATED);
|
|
|
|
|
|
|
|
$this->authorize->init();
|
|
|
|
|
|
|
|
// Set the transaction's refId
|
2022-06-21 11:57:17 +02:00
|
|
|
$refId = 'ref'.time();
|
|
|
|
|
|
|
|
// Use an existing payment profile ID for this Merchant name and Transaction key
|
|
|
|
|
|
|
|
$request = new DeleteCustomerPaymentProfileRequest();
|
|
|
|
$request->setMerchantAuthentication($this->authorize->merchant_authentication);
|
|
|
|
$request->setCustomerProfileId($gateway_customer_reference);
|
|
|
|
$request->setCustomerPaymentProfileId($payment_profile_id);
|
|
|
|
$controller = new DeleteCustomerPaymentProfileController($request);
|
|
|
|
|
|
|
|
$response = $controller->executeWithApiResponse($this->authorize->mode());
|
|
|
|
if (($response != null) && ($response->getMessages()->getResultCode() == 'Ok')) {
|
|
|
|
nlog('SUCCESS: Delete Customer Payment Profile SUCCESS :');
|
|
|
|
} else {
|
|
|
|
nlog("ERROR : Delete Customer Payment Profile: Invalid response\n");
|
|
|
|
$errorMessages = $response->getMessages()->getMessage();
|
|
|
|
nlog('Response : '.$errorMessages[0]->getCode().' '.$errorMessages[0]->getText()."\n");
|
|
|
|
}
|
2021-01-25 13:16:43 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $response;
|
|
|
|
}
|
2020-06-10 03:06:37 +02:00
|
|
|
}
|