1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Allow single transactions with Auth.net

This commit is contained in:
David Bomba 2023-05-25 11:32:17 +10:00
parent 527c99eb5b
commit 3e0f821b65
2 changed files with 179 additions and 17 deletions

View File

@ -12,20 +12,21 @@
namespace App\PaymentDrivers\Authorize;
use App\Exceptions\PaymentFailed;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\SystemLog;
use App\Models\GatewayType;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\AuthorizePaymentDriver;
use App\Jobs\Util\SystemLogger;
use App\Utils\Traits\MakesHash;
use net\authorize\api\contract\v1\DeleteCustomerPaymentProfileRequest;
use App\Exceptions\PaymentFailed;
use App\Models\ClientGatewayToken;
use App\PaymentDrivers\AuthorizePaymentDriver;
use App\PaymentDrivers\Authorize\AuthorizeTransaction;
use net\authorize\api\contract\v1\DeleteCustomerProfileRequest;
use net\authorize\api\controller\DeleteCustomerPaymentProfileController;
use net\authorize\api\controller\DeleteCustomerProfileController;
use net\authorize\api\contract\v1\DeleteCustomerPaymentProfileRequest;
use net\authorize\api\controller\DeleteCustomerPaymentProfileController;
/**
* Class AuthorizeCreditCard.
@ -68,19 +69,26 @@ class AuthorizeCreditCard
$gateway_customer_reference = $authorise_create_customer->create($data);
$authorise_payment_method = new AuthorizePaymentMethod($this->authorize);
$payment_profile = $authorise_payment_method->addPaymentMethodToClient($gateway_customer_reference, $data);
$payment_profile_id = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId();
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($gateway_customer_reference, $payment_profile_id, $data['amount_with_fee']);
if ($request->has('store_card') && $request->input('store_card') === true) {
$authorise_payment_method = new AuthorizePaymentMethod($this->authorize);
$payment_profile = $authorise_payment_method->addPaymentMethodToClient($gateway_customer_reference, $data);
$payment_profile_id = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId();
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($gateway_customer_reference, $payment_profile_id, $data['amount_with_fee']);
$authorise_payment_method->payment_method = GatewayType::CREDIT_CARD;
$client_gateway_token = $authorise_payment_method->createClientGatewayToken($payment_profile, $gateway_customer_reference);
} else {
//remove the payment profile if we are not storing tokens in our system
$this->removePaymentProfile($gateway_customer_reference, $payment_profile_id);
$authorise_transaction = new AuthorizeTransaction($this->authorize);
$data = $authorise_transaction->chargeCustomer($gateway_customer_reference, $data);
$transaction_id = $data['transaction_id'];
nlog($transaction_id);
}
return $this->handleResponse($data, $request);

View File

@ -0,0 +1,154 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Authorize;
use App\Models\Invoice;
use App\Utils\Traits\MakesHash;
use net\authorize\api\contract\v1\OrderType;
use App\PaymentDrivers\AuthorizePaymentDriver;
use net\authorize\api\contract\v1\PaymentType;
use net\authorize\api\contract\v1\SettingType;
use net\authorize\api\contract\v1\OpaqueDataType;
use net\authorize\api\contract\v1\ExtendedAmountType;
use net\authorize\api\contract\v1\PaymentProfileType;
use net\authorize\api\contract\v1\TransactionRequestType;
use net\authorize\api\contract\v1\CreateTransactionRequest;
use net\authorize\api\contract\v1\CustomerProfilePaymentType;
use net\authorize\api\controller\CreateTransactionController;
/**
* Class AuthorizeTransaction.
*/
class AuthorizeTransaction
{
use MakesHash;
public function __construct(public AuthorizePaymentDriver $authorize)
{
$this->authorize = $authorize;
}
public function chargeCustomer(string $profile_id, array $data)
{
$this->authorize->init();
// Set the transaction's refId
$refId = 'ref'.time();
$op = new OpaqueDataType();
$op->setDataDescriptor($data['dataDescriptor']);
$op->setDataValue($data['dataValue']);
$paymentOne = new PaymentType();
$paymentOne->setOpaqueData($op);
$amount = $data['amount_with_fee'];
$invoice_numbers = '';
$po_numbers = '';
$taxAmount = 0;
$invoiceTotal = 0;
$invoiceTaxes = 0;
if ($this->authorize->payment_hash->data) {
$invoice_numbers = collect($this->authorize->payment_hash->data->invoices)->pluck('invoice_number')->implode(',');
$invObj = Invoice::whereIn('id', $this->transformKeys(array_column($this->authorize->payment_hash->invoices(), 'invoice_id')))->withTrashed()->get();
$po_numbers = $invObj->pluck('po_number')->implode(',');
$invoiceTotal = round($invObj->pluck('amount')->sum(), 2);
$invoiceTaxes = round($invObj->pluck('total_taxes')->sum(), 2);
if ($invoiceTotal != $amount) {
$taxRatio = $amount / $invoiceTotal;
$taxAmount = round($invoiceTaxes * $taxRatio, 2);
} else {
$taxAmount = $invoiceTaxes;
}
}
$description = "Invoices: {$invoice_numbers} for {$amount} for client {$this->authorize->client->present()->name()}";
$order = new OrderType();
$order->setInvoiceNumber(substr($invoice_numbers, 0, 19));
$order->setDescription(substr($description, 0, 255));
$order->setSupplierOrderReference(substr($po_numbers, 0, 19));// 04-03-2023
$tax = new ExtendedAmountType();
$tax->setName('tax');
$tax->setAmount($taxAmount);
// Add values for transaction settings
$duplicateWindowSetting = new SettingType();
$duplicateWindowSetting->setSettingName("duplicateWindow");
$duplicateWindowSetting->setSettingValue("60");
$transactionRequestType = new TransactionRequestType();
$transactionRequestType->setTransactionType('authCaptureTransaction');
$transactionRequestType->setAmount($amount);
$transactionRequestType->setTax($tax);
$transactionRequestType->setTaxExempt(empty($taxAmount));
$transactionRequestType->setOrder($order);
$transactionRequestType->addToTransactionSettings($duplicateWindowSetting);
$transactionRequestType->setPayment($paymentOne);
// $transactionRequestType->setProfile($profileToCharge);
$transactionRequestType->setCurrencyCode($this->authorize->client->currency()->code);
$request = new CreateTransactionRequest();
$request->setMerchantAuthentication($this->authorize->merchant_authentication);
$request->setRefId($refId);
$request->setTransactionRequest($transactionRequestType);
$controller = new CreateTransactionController($request);
$response = $controller->executeWithApiResponse($this->authorize->mode());
if ($response != null && $response->getMessages()->getResultCode() == 'Ok') {
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null) {
nlog(' Transaction Response code : '.$tresponse->getResponseCode());
nlog(' Charge Customer Profile APPROVED :');
nlog(' Charge Customer Profile AUTH CODE : '.$tresponse->getAuthCode());
nlog(' Charge Customer Profile TRANS ID : '.$tresponse->getTransId());
nlog(' Code : '.$tresponse->getMessages()[0]->getCode());
nlog(' Description : '.$tresponse->getMessages()[0]->getDescription());
nlog(print_r($tresponse->getMessages()[0], 1));
} else {
nlog('Transaction Failed ');
if ($tresponse->getErrors() != null) {
nlog(' Error code : '.$tresponse->getErrors()[0]->getErrorCode());
nlog(' Error message : '.$tresponse->getErrors()[0]->getErrorText());
nlog(print_r($tresponse->getErrors()[0], 1));
}
}
} else {
nlog('Transaction Failed ');
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getErrors() != null) {
nlog(' Error code : '.$tresponse->getErrors()[0]->getErrorCode());
nlog(' Error message : '.$tresponse->getErrors()[0]->getErrorText());
nlog(print_r($tresponse->getErrors()[0], 1));
} else {
nlog(' Error code : '.$response->getMessages()->getMessage()[0]->getCode());
nlog(' Error message : '.$response->getMessages()->getMessage()[0]->getText());
}
}
return [
'response' => $tresponse,
'amount' => $amount,
'profile_id' => $profile_id,
'transaction_id' => $tresponse->getTransId()
// 'payment_profile_id' => $payment_profile_id,
];
}
}