1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #4036 from turbo124/v2

Refactor payments for gateway fees
This commit is contained in:
David Bomba 2020-09-04 08:37:15 +10:00 committed by GitHub
commit 50d38997e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 43 deletions

View File

@ -101,17 +101,22 @@ class AuthorizeCreditCard
}
private function tokenBilling($cgt, $amount, $invoice)
private function tokenBilling($cgt, $payment_hash)
{
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($cgt->gateway_customer_reference, $cgt->token, $amounts);
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($cgt->gateway_customer_reference, $cgt->token, $amount);
if($data['response'] != null && $data['response']->getMessages()->getResultCode() == "Ok") {
$payment = $this->createPaymentRecord($data, $amount);
$payment->meta = $cgt->meta;
$payment->save();
$this->authorize->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash);
$this->authorize->attachInvoices($payment, $invoice->hashed_id);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
$vars = [

View File

@ -16,6 +16,7 @@ use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\PaymentDrivers\Authorize\AuthorizeCreditCard;
use App\PaymentDrivers\Authorize\AuthorizePaymentMethod;
use App\PaymentDrivers\Authorize\ChargePaymentProfile;
@ -143,11 +144,11 @@ class AuthorizePaymentDriver extends BaseDriver
->first();
}
public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null)
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
$this->setPaymentMethod($cgt->gateway_type_id);
return $this->payment_method->tokenBilling($cgt, $amount, $invoice);
return $this->payment_method->tokenBilling($cgt, $payment_hash);
}
}

View File

@ -148,12 +148,11 @@ class BaseDriver extends AbstractPaymentDriver
/**
* Process an unattended payment
*
* @param ClientGatewayToken $cgt The client gateway token object
* @param float $amount The amount to bill
* @param Invoice $invoice Optional Invoice object being paid
* @return Response The payment response
* @param ClientGatewayToken $cgt The client gateway token object
* @param PaymentHash $payment_hash The Payment hash containing the payment meta data
* @return Response The payment response
*/
public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null) {}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) {}
/**
* When a successful payment is made, we need to append the gateway fee

View File

@ -18,6 +18,7 @@ use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\CheckoutCom\Utilities;
@ -109,6 +110,7 @@ class CheckoutComPaymentDriver extends BasePaymentDriver
'value' => $request->value,
'raw_value' => $request->raw_value,
'currency' => $request->currency,
'payment_hash' =>$request->payment_hash,
];
$state = array_merge($state, $request->all());
@ -163,10 +165,9 @@ class CheckoutComPaymentDriver extends BasePaymentDriver
];
$payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
$this->attachInvoices($payment, $state['hashed_ids']);
$payment->service()->updateInvoicePayment();
$payment_hash = PaymentHash::whereRaw("BINARY `hash`= ?", [$state['payment_hash']])->firstOrFail();
$this->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
@ -317,6 +318,6 @@ class CheckoutComPaymentDriver extends BasePaymentDriver
}
}
public function tokenBilling(ClientGatewayToken $cgt, float $amount) {}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) {}
}

View File

@ -18,6 +18,7 @@ use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\Utils\Ninja;
@ -91,7 +92,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
* @var $data['amount_with_fee']
* @var $data['token']
* @var $data['payment_method_id']
* @var $data['hashed_ids']
* @var $data['payment_hash']
*
* @param array $data variables required to build payment page
* @return view Gateway and payment method specific view
@ -163,10 +164,9 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
}
$payment = $this->createPayment($response->getData());
$this->attachInvoices($payment, $request->input('hashed_ids'));
$payment->service()->UpdateInvoicePayment();
$payment_hash = PaymentHash::whereRaw("BINARY `hash`= ?", [$request->input('payment_hash')])->firstOrFail();
$this->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
@ -194,7 +194,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
{
$url = $this->client->company->domain() . "/client/payments/process/response";
$url .= "?company_gateway_id={$this->company_gateway->id}&gateway_type_id=" . GatewayType::PAYPAL;
$url .= "&hashed_ids=" . implode(",", $input['hashed_ids']);
$url .= "&payment_hash=" . $input['payment_hash'];
$url .= "&amount=" . $input['amount'];
$url .= "&fee=" . $input['fee'];

View File

@ -16,6 +16,7 @@ use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\Invoice;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\StripePaymentDriver;
@ -35,9 +36,12 @@ class Charge
* Create a charge against a payment method
* @return bool success/failure
*/
public function tokenBilling(ClientGatewayToken $cgt, $amount, ?Invoice $invoice)
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$invoice = sInvoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->first();
if($invoice)
$description = "Invoice {$invoice->number} for {$amount} for client {$this->stripe->client->present()->name()}";
else
@ -169,11 +173,12 @@ class Charge
];
$payment = $this->stripe->createPaymentRecord($data, $amount);
$payment->meta = $cgt->meta;
$payment->save();
if($invoice)
$this->stripe->attachInvoices($payment, $invoice->hashed_id);
$this->stripe->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment();
$payment->service()->updateInvoicePayment($payment_hash);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));

View File

@ -23,6 +23,7 @@ use App\Models\CompanyGateway;
use App\Models\GatewayType;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\Stripe\ACH;
@ -367,9 +368,9 @@ class StripePaymentDriver extends BasePaymentDriver
return response([], 200);
}
public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null)
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
return (new Charge($this))->tokenBilling($cgt, $amount, $invoice);
return (new Charge($this))->tokenBilling($cgt, $payment_hash);
}
/**

View File

@ -17,10 +17,12 @@ use App\Factory\PaymentFactory;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Services\AbstractService;
use App\Services\Client\ClientService;
use App\Services\Payment\PaymentService;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Support\Str;
class AutoBillInvoice extends AbstractService
{
@ -55,30 +57,39 @@ class AutoBillInvoice extends AbstractService
if($this->invoice->partial > 0){
$fee = $gateway_token->gateway->calcGatewayFee($this->invoice->partial);
$amount = $this->invoice->partial + $fee;
// $amount = $this->invoice->partial + $fee;
$amount = $this->invoice->partial;
}
else{
$fee = $gateway_token->gateway->calcGatewayFee($this->invoice->balance);
$amount = $this->invoice->balance + $fee;
// $amount = $this->invoice->balance + $fee;
$amount = $this->invoice->balance;
}
$payment = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $amount, $this->invoice);
$payment_hash = PaymentHash::create([
'hash' => Str::random(128),
'data' => ['invoice_id' => $this->invoice->hashed_id, 'amount' => $amount],
'fee_total' => $fee,
'fee_invoice_id' => $this->invoice->id,
]);
if($payment){
$payment = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $payment_hash);
if($this->invoice->partial > 0)
$amount = $this->invoice->partial;
else
$amount = $this->invoice->balance;
//this is redundant - taken care of much further down.
// if($payment){
$this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $amount)->save();
// if($this->invoice->partial > 0)
// $amount = $this->invoice->partial;
// else
// $amount = $this->invoice->balance;
}
else
{
//TODO autobill failed
}
// $this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $amount)->save();
// }
// else
// {
// //TODO autobill failed
// }
return $this->invoice;
}