2020-06-12 00:32:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-06-12 00:32:56 +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-12 00:32:56 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-06-12 00:32:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Authorize;
|
|
|
|
|
2022-05-30 02:26:03 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2024-05-04 02:35:46 +02:00
|
|
|
use App\PaymentDrivers\Authorize\FDSReview;
|
2022-03-06 23:19:55 +01:00
|
|
|
use net\authorize\api\contract\v1\OrderType;
|
2024-05-04 02:35:46 +02:00
|
|
|
use App\PaymentDrivers\AuthorizePaymentDriver;
|
|
|
|
use net\authorize\api\contract\v1\ExtendedAmountType;
|
2020-06-12 00:32:56 +02:00
|
|
|
use net\authorize\api\contract\v1\PaymentProfileType;
|
|
|
|
use net\authorize\api\contract\v1\TransactionRequestType;
|
2024-05-04 02:35:46 +02:00
|
|
|
use net\authorize\api\contract\v1\CreateTransactionRequest;
|
|
|
|
use net\authorize\api\contract\v1\CustomerProfilePaymentType;
|
2020-06-12 00:32:56 +02:00
|
|
|
use net\authorize\api\controller\CreateTransactionController;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class ChargePaymentProfile.
|
2020-06-12 00:32:56 +02:00
|
|
|
*/
|
|
|
|
class ChargePaymentProfile
|
|
|
|
{
|
2022-05-30 02:26:03 +02:00
|
|
|
use MakesHash;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-03-03 22:45:41 +01:00
|
|
|
public function __construct(public AuthorizePaymentDriver $authorize)
|
2020-06-12 00:32:56 +02:00
|
|
|
{
|
|
|
|
$this->authorize = $authorize;
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function chargeCustomerProfile($profile_id, $payment_profile_id, $amount)
|
|
|
|
{
|
|
|
|
$this->authorize->init();
|
|
|
|
|
|
|
|
// Set the transaction's refId
|
|
|
|
$refId = 'ref'.time();
|
|
|
|
|
|
|
|
$profileToCharge = new CustomerProfilePaymentType();
|
|
|
|
$profileToCharge->setCustomerProfileId($profile_id);
|
|
|
|
$paymentProfile = new PaymentProfileType();
|
|
|
|
$paymentProfile->setPaymentProfileId($payment_profile_id);
|
|
|
|
$profileToCharge->setPaymentProfile($paymentProfile);
|
|
|
|
|
2022-03-06 23:19:55 +01:00
|
|
|
$invoice_numbers = '';
|
2023-03-03 22:45:41 +01:00
|
|
|
$po_numbers = '';
|
2022-05-30 02:26:03 +02:00
|
|
|
$taxAmount = 0;
|
|
|
|
$invoiceTotal = 0;
|
|
|
|
$invoiceTaxes = 0;
|
2022-03-06 23:19:55 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->authorize->payment_hash->data) {
|
|
|
|
$invoice_numbers = collect($this->authorize->payment_hash->data->invoices)->pluck('invoice_number')->implode(',');
|
2023-08-06 09:03:12 +02:00
|
|
|
$invObj = Invoice::query()->whereIn('id', $this->transformKeys(array_column($this->authorize->payment_hash->invoices(), 'invoice_id')))->withTrashed()->get();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-03-03 22:45:41 +01:00
|
|
|
$po_numbers = $invObj->pluck('po_number')->implode(',');
|
|
|
|
|
2022-05-30 02:26:03 +02:00
|
|
|
$invoiceTotal = round($invObj->pluck('amount')->sum(), 2);
|
|
|
|
$invoiceTaxes = round($invObj->pluck('total_taxes')->sum(), 2);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-05-30 02:26:03 +02:00
|
|
|
if ($invoiceTotal != $amount) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$taxRatio = $amount / $invoiceTotal;
|
|
|
|
$taxAmount = round($invoiceTaxes * $taxRatio, 2);
|
2022-05-30 02:26:03 +02:00
|
|
|
} else {
|
|
|
|
$taxAmount = $invoiceTaxes;
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-03-06 23:19:55 +01:00
|
|
|
$description = "Invoices: {$invoice_numbers} for {$amount} for client {$this->authorize->client->present()->name()}";
|
|
|
|
|
|
|
|
$order = new OrderType();
|
2022-06-21 11:57:17 +02:00
|
|
|
$order->setInvoiceNumber(substr($invoice_numbers, 0, 19));
|
|
|
|
$order->setDescription(substr($description, 0, 255));
|
2023-03-03 22:45:41 +01:00
|
|
|
$order->setSupplierOrderReference(substr($po_numbers, 0, 19));// 04-03-2023
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-05-30 02:26:03 +02:00
|
|
|
$tax = new ExtendedAmountType();
|
|
|
|
$tax->setName('tax');
|
|
|
|
$tax->setAmount($taxAmount);
|
2022-03-06 23:19:55 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$transactionRequestType = new TransactionRequestType();
|
|
|
|
$transactionRequestType->setTransactionType('authCaptureTransaction');
|
|
|
|
$transactionRequestType->setAmount($amount);
|
2022-05-30 02:26:03 +02:00
|
|
|
$transactionRequestType->setTax($tax);
|
|
|
|
$transactionRequestType->setTaxExempt(empty($taxAmount));
|
2022-03-06 23:19:55 +01:00
|
|
|
$transactionRequestType->setOrder($order);
|
2020-09-06 11:38:10 +02:00
|
|
|
$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) {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog(' Transaction Response code : '.$tresponse->getResponseCode());
|
2023-03-03 22:45:41 +01:00
|
|
|
nlog(' Charge Customer Profile APPROVED :');
|
2020-12-29 22:10:03 +01:00
|
|
|
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));
|
2024-05-04 02:35:46 +02:00
|
|
|
|
|
|
|
if($tresponse->getResponseCode() == "4") {
|
|
|
|
//notify user that this transaction is being held under FDS review:
|
|
|
|
FDSReview::dispatch((string)$tresponse->getTransId(), $this->authorize?->payment_hash, $this->authorize->company_gateway->company->db);
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog('Transaction Failed ');
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($tresponse->getErrors() != null) {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog(' Error code : '.$tresponse->getErrors()[0]->getErrorCode());
|
|
|
|
nlog(' Error message : '.$tresponse->getErrors()[0]->getErrorText());
|
|
|
|
nlog(print_r($tresponse->getErrors()[0], 1));
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog('Transaction Failed ');
|
2020-09-06 11:38:10 +02:00
|
|
|
$tresponse = $response->getTransactionResponse();
|
|
|
|
if ($tresponse != null && $tresponse->getErrors() != null) {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog(' Error code : '.$tresponse->getErrors()[0]->getErrorCode());
|
|
|
|
nlog(' Error message : '.$tresponse->getErrors()[0]->getErrorText());
|
|
|
|
nlog(print_r($tresponse->getErrors()[0], 1));
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog(' Error code : '.$response->getMessages()->getMessage()[0]->getCode());
|
|
|
|
nlog(' Error message : '.$response->getMessages()->getMessage()[0]->getText());
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-06-12 00:32:56 +02:00
|
|
|
}
|
2020-06-16 07:36:09 +02:00
|
|
|
|
2020-06-16 15:31:08 +02:00
|
|
|
return [
|
2022-06-21 11:57:17 +02:00
|
|
|
'response' => $tresponse,
|
|
|
|
'amount' => $amount,
|
|
|
|
'profile_id' => $profile_id,
|
|
|
|
'payment_profile_id' => $payment_profile_id,
|
2020-06-16 15:31:08 +02:00
|
|
|
];
|
2020-06-12 00:32:56 +02:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|