mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 15:14:35 -04:00
Payment refunds (#3687)
* Fix whereClientId when starting payment * Refunding using Paypal * Refunding engine * Cleanup and making refund method work * Remove "refund" method from BasePaymentController * Add "refund" to PaypalExpressPaymentDriver * Extract refunding into own classes * Apply php-cs-fixer to PaypalExpress * Refunding with stripe
This commit is contained in:
parent
14577fdfd0
commit
a613cfed7c
31
app/Exceptions/PaymentRefundFailed.php
Normal file
31
app/Exceptions/PaymentRefundFailed.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class PaymentRefundFailed extends Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Report the exception.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function report()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the exception into an HTTP response.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function render($request)
|
||||||
|
{
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Unable to refund the transaction'
|
||||||
|
], 401);
|
||||||
|
}
|
||||||
|
}
|
@ -72,7 +72,7 @@ class PaymentController extends Controller
|
|||||||
public function process()
|
public function process()
|
||||||
{
|
{
|
||||||
$invoices = Invoice::whereIn('id', $this->transformKeys(request()->invoices))
|
$invoices = Invoice::whereIn('id', $this->transformKeys(request()->invoices))
|
||||||
->whereClientId(auth('contact')->user()->company()->id)
|
->where('company_id', auth('contact')->user()->company->id)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$amount = $invoices->sum('balance');
|
$amount = $invoices->sum('balance');
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoice Ninja (https://invoiceninja.com)
|
* Invoice Ninja (https://invoiceninja.com)
|
||||||
*
|
*
|
||||||
@ -20,7 +21,6 @@ use App\Models\Payment;
|
|||||||
use App\Models\PaymentType;
|
use App\Models\PaymentType;
|
||||||
use App\Models\SystemLog;
|
use App\Models\SystemLog;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Omnipay\Common\Item;
|
use Omnipay\Common\Item;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,7 +61,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|||||||
{
|
{
|
||||||
use MakesHash;
|
use MakesHash;
|
||||||
|
|
||||||
protected $refundable = false;
|
protected $refundable = true;
|
||||||
|
|
||||||
protected $token_billing = false;
|
protected $token_billing = false;
|
||||||
|
|
||||||
@ -107,9 +107,9 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|||||||
|
|
||||||
SystemLogger::dispatch(
|
SystemLogger::dispatch(
|
||||||
[
|
[
|
||||||
'server_response' => $response->getData(),
|
'server_response' => $response->getData(),
|
||||||
'data' => $data
|
'data' => $data
|
||||||
],
|
],
|
||||||
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||||
SystemLog::EVENT_GATEWAY_FAILURE,
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
||||||
SystemLog::TYPE_PAYPAL,
|
SystemLog::TYPE_PAYPAL,
|
||||||
@ -131,20 +131,20 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|||||||
} elseif ($response->isSuccessful()) {
|
} elseif ($response->isSuccessful()) {
|
||||||
SystemLogger::dispatch(
|
SystemLogger::dispatch(
|
||||||
[
|
[
|
||||||
'server_response' => $response->getData(),
|
'server_response' => $response->getData(),
|
||||||
'data' => $request->all()
|
'data' => $request->all()
|
||||||
],
|
],
|
||||||
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||||
SystemLog::EVENT_GATEWAY_SUCCESS,
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
||||||
SystemLog::TYPE_PAYPAL,
|
SystemLog::TYPE_PAYPAL,
|
||||||
$this->client
|
$this->client
|
||||||
);
|
);
|
||||||
} elseif (! $response->isSuccessful()) {
|
} elseif (!$response->isSuccessful()) {
|
||||||
SystemLogger::dispatch(
|
SystemLogger::dispatch(
|
||||||
[
|
[
|
||||||
'data' => $request->all(),
|
'data' => $request->all(),
|
||||||
'server_response' => $response->getData()
|
'server_response' => $response->getData()
|
||||||
],
|
],
|
||||||
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||||
SystemLog::EVENT_GATEWAY_FAILURE,
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
||||||
SystemLog::TYPE_PAYPAL,
|
SystemLog::TYPE_PAYPAL,
|
||||||
@ -159,13 +159,13 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|||||||
$this->attachInvoices($payment, $request->input('hashed_ids'));
|
$this->attachInvoices($payment, $request->input('hashed_ids'));
|
||||||
|
|
||||||
$payment->service()->UpdateInvoicePayment();
|
$payment->service()->UpdateInvoicePayment();
|
||||||
|
|
||||||
event(new PaymentWasCreated($payment, $payment->company));
|
event(new PaymentWasCreated($payment, $payment->company));
|
||||||
|
|
||||||
return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]);
|
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function paymentDetails($input) :array
|
protected function paymentDetails($input): array
|
||||||
{
|
{
|
||||||
$data = parent::paymentDetails($input);
|
$data = parent::paymentDetails($input);
|
||||||
|
|
||||||
@ -182,41 +182,41 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildReturnUrl($input) : string
|
private function buildReturnUrl($input): string
|
||||||
{
|
{
|
||||||
$url = $this->client->company->domain() . "/client/payments/process/response";
|
$url = $this->client->company->domain() . "/client/payments/process/response";
|
||||||
$url .= "?company_gateway_id={$this->company_gateway->id}&gateway_type_id=".GatewayType::PAYPAL;
|
$url .= "?company_gateway_id={$this->company_gateway->id}&gateway_type_id=" . GatewayType::PAYPAL;
|
||||||
$url .= "&hashed_ids=" . implode(",", $input['hashed_ids']);
|
$url .= "&hashed_ids=" . implode(",", $input['hashed_ids']);
|
||||||
$url .= "&amount=".$input['amount'];
|
$url .= "&amount=" . $input['amount'];
|
||||||
$url .= "&fee=".$input['fee'];
|
$url .= "&fee=" . $input['fee'];
|
||||||
|
|
||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildCancelUrl($input) : string
|
private function buildCancelUrl($input): string
|
||||||
{
|
{
|
||||||
$url = $this->client->company->domain() . '/client/invoices';
|
$url = $this->client->company->domain() . '/client/invoices';
|
||||||
|
|
||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildDescription($input) : string
|
private function buildDescription($input): string
|
||||||
{
|
{
|
||||||
$invoice_numbers = "";
|
$invoice_numbers = "";
|
||||||
|
|
||||||
foreach ($input['invoices'] as $invoice) {
|
foreach ($input['invoices'] as $invoice) {
|
||||||
$invoice_numbers .= $invoice->number." ";
|
$invoice_numbers .= $invoice->number . " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctrans('texts.invoice_number'). ": {$invoice_numbers}";
|
return ctrans('texts.invoice_number') . ": {$invoice_numbers}";
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildTransactionId($input) : string
|
private function buildTransactionId($input): string
|
||||||
{
|
{
|
||||||
return implode(",", $input['hashed_ids']);
|
return implode(",", $input['hashed_ids']);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function paymentItems($input) : array
|
private function paymentItems($input): array
|
||||||
{
|
{
|
||||||
$items = [];
|
$items = [];
|
||||||
$total = 0;
|
$total = 0;
|
||||||
@ -255,7 +255,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|||||||
return $items;
|
return $items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createPayment($data) : Payment
|
public function createPayment($data): Payment
|
||||||
{
|
{
|
||||||
$payment = parent::createPayment($data);
|
$payment = parent::createPayment($data);
|
||||||
|
|
||||||
@ -270,4 +270,41 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|||||||
|
|
||||||
return $payment;
|
return $payment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function refund(Payment $payment, $amount = null)
|
||||||
|
{
|
||||||
|
$this->gateway();
|
||||||
|
|
||||||
|
$response = $this->gateway
|
||||||
|
->refund(['transactionReference' => $payment->transaction_reference, 'amount' => $amount ?? $payment->amount])
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($response->isSuccessful()) {
|
||||||
|
SystemLogger::dispatch(
|
||||||
|
[
|
||||||
|
'server_response' => $response->getMessage(),
|
||||||
|
'data' => request()->all(),
|
||||||
|
],
|
||||||
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||||
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
||||||
|
SystemLog::TYPE_PAYPAL,
|
||||||
|
$this->client
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
SystemLogger::dispatch(
|
||||||
|
[
|
||||||
|
'server_response' => $response->getMessage(),
|
||||||
|
'data' => request()->all(),
|
||||||
|
],
|
||||||
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||||
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
||||||
|
SystemLog::TYPE_PAYPAL,
|
||||||
|
$this->client
|
||||||
|
);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -471,6 +471,42 @@ class StripePaymentDriver extends BasePaymentDriver
|
|||||||
return $customer;
|
return $customer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function refund(Payment $payment, $amount = null)
|
||||||
|
{
|
||||||
|
$this->gateway();
|
||||||
|
|
||||||
|
$response = $this->gateway
|
||||||
|
->refund(['transactionReference' => $payment->transaction_reference, 'amount' => $amount ?? $payment->amount])
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($response->isSuccessful()) {
|
||||||
|
SystemLogger::dispatch(
|
||||||
|
[
|
||||||
|
'server_response' => $response->getMessage(),
|
||||||
|
'data' => request()->all(),
|
||||||
|
],
|
||||||
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||||
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
||||||
|
SystemLog::TYPE_PAYPAL,
|
||||||
|
$this->client
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
SystemLogger::dispatch(
|
||||||
|
[
|
||||||
|
'server_response' => $response->getMessage(),
|
||||||
|
'data' => request()->all(),
|
||||||
|
],
|
||||||
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
||||||
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
||||||
|
SystemLog::TYPE_PAYPAL,
|
||||||
|
$this->client
|
||||||
|
);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/************************************** Omnipay API methods **********************************************************/
|
/************************************** Omnipay API methods **********************************************************/
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,11 @@
|
|||||||
|
|
||||||
namespace App\Utils\Traits\Payment;
|
namespace App\Utils\Traits\Payment;
|
||||||
|
|
||||||
|
use App\Exceptions\PaymentRefundFailed;
|
||||||
use App\Factory\CreditFactory;
|
use App\Factory\CreditFactory;
|
||||||
use App\Factory\InvoiceItemFactory;
|
use App\Factory\InvoiceItemFactory;
|
||||||
use App\Models\Activity;
|
use App\Models\Activity;
|
||||||
|
use App\Models\CompanyGateway;
|
||||||
use App\Models\Credit;
|
use App\Models\Credit;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
@ -163,11 +165,18 @@ trait Refundable
|
|||||||
$credit_note->number = $this->client->getNextCreditNumber($this->client);
|
$credit_note->number = $this->client->getNextCreditNumber($this->client);
|
||||||
$credit_note->save();
|
$credit_note->save();
|
||||||
|
|
||||||
//determine if we need to refund via gateway
|
|
||||||
if ($data['gateway_refund'] !== false) {
|
if ($data['gateway_refund'] !== false) {
|
||||||
//todo process gateway refund, on success, reduce the credit note balance to 0
|
$gateway = CompanyGateway::find($this->company_gateway_id);
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($gateway) {
|
||||||
|
$amount = request()->has('amount') ? request()->amount : null;
|
||||||
|
$response = $gateway->driver($this->client)->refund($this, $amount);
|
||||||
|
|
||||||
|
if (!$response) {
|
||||||
|
throw new PaymentRefundFailed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($total_refund > 0) {
|
if ($total_refund > 0) {
|
||||||
$this->refunded += $total_refund;
|
$this->refunded += $total_refund;
|
||||||
|
@ -78,6 +78,7 @@
|
|||||||
href="{{ route('client.invoice.show', ['invoice' => $invoice->hashed_id])}}">
|
href="{{ route('client.invoice.show', ['invoice' => $invoice->hashed_id])}}">
|
||||||
{{ $invoice->number }}
|
{{ $invoice->number }}
|
||||||
</a>
|
</a>
|
||||||
|
<span>Payment: {{ $payment->hashed_id }} Invoice: {{ $invoice->hashed_id }} Amount: {{ $payment->amount }}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@endforeach
|
@endforeach
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
* Signup Routes
|
* Signup Routes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use Omnipay\Omnipay;
|
||||||
|
|
||||||
Route::get('/', 'BaseController@flutterRoute')->middleware('guest');
|
Route::get('/', 'BaseController@flutterRoute')->middleware('guest');
|
||||||
Route::get('setup', 'SetupController@index')->middleware('guest');
|
Route::get('setup', 'SetupController@index')->middleware('guest');
|
||||||
Route::post('setup/check_db', 'SetupController@checkDB')->middleware('guest');
|
Route::post('setup/check_db', 'SetupController@checkDB')->middleware('guest');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user