Working on Authorize.net

This commit is contained in:
David Bomba 2020-06-10 11:06:37 +10:00
parent c119e9ece2
commit 17d10f5f66
6 changed files with 518 additions and 18 deletions

View File

@ -44,7 +44,7 @@ class PaymentMethodController extends Controller
$gateway = auth()->user()->client->getCreditCardGateway();
$auth = new AuthorizePaymentDriver($gateway, auth()->user()->client);
return $auth->authorizeView();
// $data = [
@ -69,10 +69,14 @@ class PaymentMethodController extends Controller
{
$gateway = auth()->user()->client->getCreditCardGateway();
return $gateway
->driver(auth()->user()->client)
->setPaymentMethod('App\\PaymentDrivers\\Stripe\\CreditCard')
->authorizeCreditCardResponse($request);
$auth = new AuthorizePaymentDriver($gateway, auth()->user()->client);
return $auth->authorizeResponseView($request->all());
// return $gateway
// ->driver(auth()->user()->client)
// ->setPaymentMethod('App\\PaymentDrivers\\Stripe\\CreditCard')
// ->authorizeCreditCardResponse($request);
}
/**

View File

@ -0,0 +1,38 @@
<?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;
use App\Models\GatewayType;
use App\PaymentDrivers\AuthorizePaymentDriver;
/**
* Class BaseDriver
* @package App\PaymentDrivers
*
*/
class AuthorizeCreateCustomer
{
public $authorize;
public function __construct(AuthorizePaymentDriver $authorize)
{
$this->authorize = $authorize;
}
public function create($data = null)
{
}
}

View File

@ -0,0 +1,109 @@
<?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;
use App\Models\GatewayType;
use App\PaymentDrivers\AuthorizePaymentDriver;
use App\PaymentDrivers\Authorize\AuthorizeCreateCustomer;
/**
* Class BaseDriver
* @package App\PaymentDrivers
*
*/
class AuthorizePaymentMethod
{
public $authorize;
public function __construct(AuthorizePaymentDriver $authorize)
{
$this->authorize = $authorize;
}
public function authorizeView($payment_method)
{
switch ($payment_method) {
case GatewayType::CREDIT_CARD:
return $this->authorizeCreditCard();
break;
case GatewayType::BANK_TRANSFER:
return $this->authorizeBankTransfer();
break;
default:
# code...
break;
}
}
public function authorizeResponseView($payment_method, $data)
{
switch ($payment_method) {
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)
{
if($client_gateway_token_record = $this->authorize->findClientGatewayRecord())
$this->addCreditCardToClient($client_gateway_token_record, $data);
else{
$client_gateway_token_record = (new AuthorizeCreateCustomer($this->authorize))->create($data);
$this->addCreditCardToClient($client_gateway_token_record, $data);
}
return redirect()->route('client.payment_methods.index');
}
public function authorizeBankTransferResponse($data)
{
}
private function addCreditCardToClient(ClientGatewayToken $client_gateway_token, $data)
{
//add a payment profile to the client profile
//we only use the $client_gateway_token record as a reference to create a NEW client_gateway_token for this gateway
}
}

View File

@ -12,6 +12,8 @@
namespace App\PaymentDrivers;
use App\Models\ClientGatewayToken;
use App\PaymentDrivers\Authorize\AuthorizeCreditCard;
use net\authorize\api\constants\ANetEnvironment;
use net\authorize\api\contract\v1\CreateTransactionRequest;
use net\authorize\api\contract\v1\GetMerchantDetailsRequest;
@ -63,13 +65,29 @@ class AuthorizePaymentDriver extends BaseDriver
}
public function authorizeView()
public function authorizeView($payment_method)
{
$data['gateway'] = $this->company_gateway;
$data['public_client_id'] = $this->init()->getPublicClientKey();
$data['api_login_id'] = $this->company_gateway->getConfigField('apiLoginId');
return (new AuthorizePaymentMethod($this))->authorizeView($payment_method);
}
public function authorizeResponse($payment_method, array $data)
{
// <input type="hidden" name="is_default" id="is_default">
// <input type="hidden" name="dataValue" id="dataValue" />
// <input type="hidden" name="dataDescriptor" id="dataDescriptor" />
// $client_gateway_token = new ClientGatewayToken();
// $client_gateway_token->company_id = $this->stripe->client->company->id;
// $client_gateway_token->client_id = $this->stripe->client->id;
// $client_gateway_token->token = $payment_method;
// $client_gateway_token->company_gateway_id = $this->stripe->company_gateway->id;
// $client_gateway_token->gateway_type_id = $gateway_type_id;
// $client_gateway_token->gateway_customer_reference = $customer->id;
// $client_gateway_token->meta = $payment_meta;
// $client_gateway_token->save();
return render('gateways.authorize.add_credit_card', $data);
}
// public function fire()
@ -82,11 +100,25 @@ class AuthorizePaymentDriver extends BaseDriver
// }
public function authorize() {}
public function purchase() {}
public function refund() {}
public function authorize($payment_method)
{
}
public function purchase()
{
}
public function refund()
{
}
private function findClientGatewayRecord() :?ClientGatewayToken
{
return ClientGatewayToken::where('client_id', $this->client->id)
->where('company_gateway_id', $this->company_gateway->id)
->first();
}
}

View File

@ -32,9 +32,6 @@ class BaseDriver extends AbstractPaymentDriver
/* The company gateway instance*/
public $company_gateway;
/* The Gateway Driver instance*/
protected $gateway;
/* The Invitation */
protected $invitation;

View File

@ -5,10 +5,25 @@ namespace Tests\Integration\PaymentDrivers;
use Tests\TestCase;
use net\authorize\api\constants\ANetEnvironment;
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\contract\v1\CreateCustomerPaymentProfileRequest;
use net\authorize\api\contract\v1\CreateTransactionRequest;
use net\authorize\api\contract\v1\CreditCardType;
use net\authorize\api\contract\v1\CustomerAddressType;
use net\authorize\api\contract\v1\CustomerPaymentProfileType;
use net\authorize\api\contract\v1\CustomerProfilePaymentType;
use net\authorize\api\contract\v1\CustomerProfileType;
use net\authorize\api\contract\v1\GetCustomerProfileIdsRequest;
use net\authorize\api\contract\v1\GetCustomerProfileRequest;
use net\authorize\api\contract\v1\GetMerchantDetailsRequest;
use net\authorize\api\contract\v1\MerchantAuthenticationType;
use net\authorize\api\contract\v1\PaymentProfileType;
use net\authorize\api\contract\v1\PaymentType;
use net\authorize\api\contract\v1\TransactionRequestType;
use net\authorize\api\controller\CreateCustomerPaymentProfileController;
use net\authorize\api\controller\CreateCustomerProfileController;
use net\authorize\api\controller\CreateTransactionController;
use net\authorize\api\controller\GetCustomerProfileController;
use net\authorize\api\controller\GetCustomerProfileIdsController;
use net\authorize\api\controller\GetMerchantDetailsController;
/**
@ -17,6 +32,10 @@ use net\authorize\api\controller\GetMerchantDetailsController;
class AuthorizeTest extends TestCase
{
public $customer_profile_id = 1512191314;
public $customer_payment_profile = 1512219932;
public function setUp() :void
{
parent::setUp();
@ -54,4 +73,305 @@ class AuthorizeTest extends TestCase
$this->assertNotNull($response->getPublicClientKey());
}
public function testProfileIdList()
{
error_reporting (E_ALL & ~E_DEPRECATED);
$vars = json_decode(config('ninja.testvars.authorize'));
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($vars->apiLoginId);
$merchantAuthentication->setTransactionKey($vars->transactionKey);
// Set the transaction's refId
$refId = 'ref' . time();
// Get all existing customer profile ID's
$request = new GetCustomerProfileIdsRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$controller = new GetCustomerProfileIdsController($request);
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") )
{
info("GetCustomerProfileId's SUCCESS: " . "\n");
info(print_r($response->getIds(),1));
}
else
{
info("GetCustomerProfileId's ERROR : Invalid response\n");
$errorMessages = $response->getMessages()->getMessage();
info("Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() . "\n");
}
$this->assertNotNull($response);
}
public function testCreateProfile()
{
error_reporting (E_ALL & ~E_DEPRECATED);
$vars = json_decode(config('ninja.testvars.authorize'));
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($vars->apiLoginId);
$merchantAuthentication->setTransactionKey($vars->transactionKey);
// Create the Bill To info for new payment type
$billTo = new CustomerAddressType();
$billTo->setFirstName("Ellen");
$billTo->setLastName("Johnson");
$billTo->setCompany("Souveniropolis");
$billTo->setAddress("14 Main Street");
$billTo->setCity("Pecan Springs");
$billTo->setState("TX");
$billTo->setZip("44628");
$billTo->setCountry("USA");
$billTo->setPhoneNumber("888-888-8888");
$billTo->setfaxNumber("999-999-9999");
// Create a customer shipping address
$customerShippingAddress = new CustomerAddressType();
$customerShippingAddress->setFirstName("James");
$customerShippingAddress->setLastName("White");
$customerShippingAddress->setCompany("Addresses R Us");
$customerShippingAddress->setAddress(rand() . " North Spring Street");
$customerShippingAddress->setCity("Toms River");
$customerShippingAddress->setState("NJ");
$customerShippingAddress->setZip("08753");
$customerShippingAddress->setCountry("USA");
$customerShippingAddress->setPhoneNumber("888-888-8888");
$customerShippingAddress->setFaxNumber("999-999-9999");
// Create an array of any shipping addresses
$shippingProfiles[] = $customerShippingAddress;
$refId = 'ref' . time();
$email = "test@gmail.com";
// // Create a new CustomerPaymentProfile object
// $paymentProfile = new AnetAPI\CustomerPaymentProfileType();
// $paymentProfile->setCustomerType('individual');
// $paymentProfile->setBillTo($billTo);
// $paymentProfile->setPayment($paymentCreditCard);
// $paymentProfiles[] = $paymentProfile;
// Create a new CustomerProfileType and add the payment profile object
$customerProfile = new CustomerProfileType();
$customerProfile->setDescription("Customer 2 Test PHP");
$customerProfile->setMerchantCustomerId("M_" . time());
$customerProfile->setEmail($email);
//$customerProfile->setpaymentProfiles($paymentProfiles);
$customerProfile->setShipToList($shippingProfiles);
// Assemble the complete transaction request
$request = new AnetAPI\CreateCustomerProfileRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setProfile($customerProfile);
// Create the controller and get the response
$controller = new CreateCustomerProfileController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
info("Succesfully created customer profile : " . $response->getCustomerProfileId() . "\n");
$paymentProfiles = $response->getCustomerPaymentProfileIdList();
info(print_r($paymentProfiles,1));
} else {
info("ERROR : Invalid response\n");
$errorMessages = $response->getMessages()->getMessage();
info("Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() . "\n");
}
$this->assertNotNull($response);
}
public function testGetCustomerProfileId()
{
error_reporting (E_ALL & ~E_DEPRECATED);
$vars = json_decode(config('ninja.testvars.authorize'));
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($vars->apiLoginId);
$merchantAuthentication->setTransactionKey($vars->transactionKey);
$request = new GetCustomerProfileRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setCustomerProfileId($this->customer_profile_id);
$controller = new GetCustomerProfileController($request);
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
info("got profile");
info(print_r($response->getProfile(),1));
} else {
info("ERROR : Invalid response\n");
}
$this->assertNotNull($response);
}
public function testCreateCustomerPaymentProfile()
{
info("test create customer payment profile");
error_reporting (E_ALL & ~E_DEPRECATED);
$vars = json_decode(config('ninja.testvars.authorize'));
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($vars->apiLoginId);
$merchantAuthentication->setTransactionKey($vars->transactionKey);
// Set the transaction's refId
$refId = 'ref' . time();
// Set credit card information for payment profile
$creditCard = new CreditCardType();
$creditCard->setCardNumber("4007000000027");
$creditCard->setExpirationDate("2038-12");
$creditCard->setCardCode("142");
$paymentCreditCard = new PaymentType();
$paymentCreditCard->setCreditCard($creditCard);
// Create the Bill To info for new payment type
$billto = new CustomerAddressType();
$billto->setFirstName("Ellen");
$billto->setLastName("Johnson");
$billto->setCompany("Souveniropolis");
$billto->setAddress("14 Main Street");
$billto->setCity("Pecan Springs");
$billto->setState("TX");
$billto->setZip("44628");
$billto->setCountry("USA");
$billto->setPhoneNumber("999-999-9999");
$billto->setfaxNumber("999-999-9999");
// Create a new Customer Payment Profile object
$paymentprofile = new CustomerPaymentProfileType();
$paymentprofile->setCustomerType('individual');
$paymentprofile->setBillTo($billto);
$paymentprofile->setPayment($paymentCreditCard);
$paymentprofile->setDefaultPaymentProfile(true);
$paymentprofiles[] = $paymentprofile;
// Assemble the complete transaction request
$paymentprofilerequest = new CreateCustomerPaymentProfileRequest();
$paymentprofilerequest->setMerchantAuthentication($merchantAuthentication);
// Add an existing profile id to the request
$paymentprofilerequest->setCustomerProfileId($this->customer_profile_id);
$paymentprofilerequest->setPaymentProfile($paymentprofile);
$paymentprofilerequest->setValidationMode("liveMode");
// Create the controller and get the response
$controller = new CreateCustomerPaymentProfileController($paymentprofilerequest);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") ) {
info("Create Customer Payment Profile SUCCESS: " . $response->getCustomerPaymentProfileId() . "\n");
} else {
info("Create Customer Payment Profile: ERROR Invalid response\n");
$errorMessages = $response->getMessages()->getMessage();
info("Response : " . $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText() . "\n");
}
$this->assertNotNull($response);
}
function testChargeCustomerProfile()
{
error_reporting (E_ALL & ~E_DEPRECATED);
$vars = json_decode(config('ninja.testvars.authorize'));
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName($vars->apiLoginId);
$merchantAuthentication->setTransactionKey($vars->transactionKey);
// Set the transaction's refId
$refId = 'ref' . time();
$profileToCharge = new CustomerProfilePaymentType();
$profileToCharge->setCustomerProfileId($this->customer_profile_id);
$paymentProfile = new PaymentProfileType();
$paymentProfile->setPaymentProfileId($this->customer_payment_profile);
$profileToCharge->setPaymentProfile($paymentProfile);
$transactionRequestType = new TransactionRequestType();
$transactionRequestType->setTransactionType( "authCaptureTransaction");
$transactionRequestType->setAmount(400);
$transactionRequestType->setProfile($profileToCharge);
$request = new CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId( $refId);
$request->setTransactionRequest( $transactionRequestType);
$controller = new CreateTransactionController($request);
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);
if ($response != null)
{
if($response->getMessages()->getResultCode() == "Ok")
{
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null)
{
info( " Transaction Response code : " . $tresponse->getResponseCode() . "\n");
info( "Charge Customer Profile APPROVED :" . "\n");
info( " Charge Customer Profile AUTH CODE : " . $tresponse->getAuthCode() . "\n");
info( " Charge Customer Profile TRANS ID : " . $tresponse->getTransId() . "\n");
info( " Code : " . $tresponse->getMessages()[0]->getCode() . "\n");
info( " Description : " . $tresponse->getMessages()[0]->getDescription() . "\n");
}
else
{
info( "Transaction Failed \n");
if($tresponse->getErrors() != null)
{
info( " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n");
info( " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n");
}
}
}
else
{
info( "Transaction Failed \n");
$tresponse = $response->getTransactionResponse();
if($tresponse != null && $tresponse->getErrors() != null)
{
info( " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n");
info( " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n");
}
else
{
info( " Error code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n");
info( " Error message : " . $response->getMessages()->getMessage()[0]->getText() . "\n");
}
}
}
else
{
info( "No response returned \n");
}
$this->assertNotNull($response);
}
}