2020-06-10 03:06:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Authorize;
|
|
|
|
|
2020-06-10 07:21:11 +02:00
|
|
|
use App\Exceptions\GenericPaymentDriverFailure;
|
|
|
|
use App\Models\ClientGatewayToken;
|
2020-06-10 03:06:37 +02:00
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\PaymentDrivers\AuthorizePaymentDriver;
|
|
|
|
use App\PaymentDrivers\Authorize\AuthorizeCreateCustomer;
|
2020-06-10 07:21:11 +02:00
|
|
|
use net\authorize\api\contract\v1\CreateCustomerPaymentProfileRequest;
|
|
|
|
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;
|
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;
|
|
|
|
use net\authorize\api\controller\CreateCustomerProfileController;
|
2020-06-10 14:42:10 +02:00
|
|
|
use net\authorize\api\controller\GetCustomerPaymentProfileController;
|
2020-06-10 03:06:37 +02:00
|
|
|
|
|
|
|
/**
|
2020-06-10 10:05:30 +02:00
|
|
|
* Class AuthorizePaymentMethod
|
|
|
|
* @package App\PaymentDrivers\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-06-10 03:06:37 +02:00
|
|
|
public function __construct(AuthorizePaymentDriver $authorize)
|
|
|
|
{
|
|
|
|
$this->authorize = $authorize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeView($payment_method)
|
|
|
|
{
|
2020-06-10 07:21:11 +02:00
|
|
|
$this->payment_method = $payment_method;
|
2020-06-10 03:06:37 +02:00
|
|
|
|
|
|
|
switch ($payment_method) {
|
|
|
|
case GatewayType::CREDIT_CARD:
|
|
|
|
return $this->authorizeCreditCard();
|
|
|
|
break;
|
|
|
|
case GatewayType::BANK_TRANSFER:
|
|
|
|
return $this->authorizeBankTransfer();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
# code...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-27 04:21:16 +02:00
|
|
|
public function authorizeResponseView($data)
|
2020-06-10 03:06:37 +02:00
|
|
|
{
|
2020-06-27 04:21:16 +02:00
|
|
|
$this->payment_method = $data['payment_method_id'];
|
2020-06-10 03:06:37 +02:00
|
|
|
|
2020-06-27 04:21:16 +02:00
|
|
|
switch ($this->payment_method) {
|
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:
|
|
|
|
# code...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeCreditCard()
|
|
|
|
{
|
|
|
|
$data['gateway'] = $this->authorize->company_gateway;
|
|
|
|
$data['public_client_id'] = $this->authorize->init()->getPublicClientKey();
|
|
|
|
$data['api_login_id'] = $this->authorize->company_gateway->getConfigField('apiLoginId');
|
|
|
|
|
|
|
|
return render('gateways.authorize.add_credit_card', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-10 14:42: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);
|
2020-06-10 14:42:10 +02:00
|
|
|
}
|
2020-06-10 03:06:37 +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);
|
2020-06-10 03:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 07:21:11 +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-06-10 14:42:10 +02:00
|
|
|
info(print_r($payment_profile,1));
|
|
|
|
|
2020-06-10 07:21:11 +02:00
|
|
|
$client_gateway_token = new ClientGatewayToken();
|
2020-06-10 10:05:30 +02:00
|
|
|
$client_gateway_token->company_id = $this->authorize->client->company_id;
|
|
|
|
$client_gateway_token->client_id = $this->authorize->client->id;
|
2020-06-10 14:42:10 +02:00
|
|
|
$client_gateway_token->token = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId();
|
2020-06-10 07:21:11 +02:00
|
|
|
$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;
|
|
|
|
$client_gateway_token->meta = $this->buildPaymentMethod($payment_profile);
|
|
|
|
$client_gateway_token->save();
|
2020-06-16 14:41:56 +02:00
|
|
|
|
|
|
|
return $client_gateway_token;
|
2020-06-10 07:21:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function buildPaymentMethod($payment_profile)
|
|
|
|
{
|
|
|
|
$payment_meta = new \stdClass;
|
2020-06-10 07:28:41 +02:00
|
|
|
$payment_meta->exp_month = 'xx';
|
|
|
|
$payment_meta->exp_year = 'xx';
|
2020-06-10 14:42:10 +02:00
|
|
|
$payment_meta->brand = $payment_profile->getPaymentProfile()->getPayment()->getCreditCard()->getCardType();
|
|
|
|
$payment_meta->last4 = $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
|
|
|
{
|
|
|
|
|
|
|
|
error_reporting (E_ALL & ~E_DEPRECATED);
|
|
|
|
|
2020-06-10 14:42:10 +02:00
|
|
|
$this->authorize->init();
|
2020-06-10 07:21:11 +02:00
|
|
|
|
|
|
|
// Set the transaction's refId
|
|
|
|
$refId = 'ref' . time();
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2020-06-10 10:05:30 +02:00
|
|
|
$contact = $this->authorize->client->primary_contact()->first();
|
2020-06-10 07:21:11 +02:00
|
|
|
|
|
|
|
if($contact){
|
|
|
|
// Create the Bill To info for new payment type
|
|
|
|
$billto = new CustomerAddressType();
|
|
|
|
$billto->setFirstName($contact->present()->first_name());
|
|
|
|
$billto->setLastName($contact->present()->last_name());
|
2020-06-10 10:05:30 +02:00
|
|
|
$billto->setCompany($this->authorize->client->present()->name());
|
|
|
|
$billto->setAddress($this->authorize->client->address1);
|
|
|
|
$billto->setCity($this->authorize->client->city);
|
|
|
|
$billto->setState($this->authorize->client->state);
|
|
|
|
$billto->setZip($this->authorize->client->postal_code);
|
|
|
|
|
|
|
|
if($this->authorize->client->country_id)
|
|
|
|
$billto->setCountry($this->authorize->client->country->name);
|
2020-06-10 07:28:41 +02:00
|
|
|
|
2020-06-10 10:05:30 +02:00
|
|
|
$billto->setPhoneNumber($this->authorize->client->phone);
|
2020-06-10 07:21:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Customer Payment Profile object
|
|
|
|
$paymentprofile = new CustomerPaymentProfileType();
|
|
|
|
$paymentprofile->setCustomerType('individual');
|
|
|
|
|
|
|
|
if($billto)
|
|
|
|
$paymentprofile->setBillTo($billto);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
$paymentprofilerequest->setValidationMode("liveMode");
|
|
|
|
|
|
|
|
// Create the controller and get the response
|
|
|
|
$controller = new CreateCustomerPaymentProfileController($paymentprofilerequest);
|
|
|
|
$response = $controller->executeWithApiResponse($this->authorize->mode());
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$message = "Unable to add customer to Authorize.net gateway";
|
|
|
|
|
|
|
|
if(is_array($errorMessages))
|
|
|
|
$message = $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText();
|
|
|
|
|
|
|
|
throw new GenericPaymentDriverFailure($message);
|
|
|
|
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2020-06-10 03:06:37 +02:00
|
|
|
|
|
|
|
}
|