diff --git a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php index b2228f720c9b..36fcc60deed5 100644 --- a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php +++ b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php @@ -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 = [ diff --git a/app/PaymentDrivers/AuthorizePaymentDriver.php b/app/PaymentDrivers/AuthorizePaymentDriver.php index f0644e642c07..17dabf1d3aaa 100644 --- a/app/PaymentDrivers/AuthorizePaymentDriver.php +++ b/app/PaymentDrivers/AuthorizePaymentDriver.php @@ -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); } } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 8572ed559345..93f34e02328a 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -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 diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index c865eba9da56..667672bc4843 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -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) {} } diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 4b4808389a0d..7a0d0a11c8db 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -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']; diff --git a/app/PaymentDrivers/Stripe/Charge.php b/app/PaymentDrivers/Stripe/Charge.php index ca3668922e72..7ac60d47124d 100644 --- a/app/PaymentDrivers/Stripe/Charge.php +++ b/app/PaymentDrivers/Stripe/Charge.php @@ -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())); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index f441b6a0075e..e40cfd18a55a 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -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); } /** diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 64ab746d9533..068a2d323230 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -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; }