2021-10-07 16:35:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2021-10-07 16:35:58 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-10-07 16:35:58 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Razorpay;
|
|
|
|
|
2021-10-07 18:01:36 +02:00
|
|
|
use App\Exceptions\PaymentFailed;
|
2021-10-07 16:35:58 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2021-10-07 18:01:36 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Models\SystemLog;
|
2024-08-09 01:08:00 +02:00
|
|
|
use App\PaymentDrivers\Common\LivewireMethodInterface;
|
2021-10-07 16:35:58 +02:00
|
|
|
use App\PaymentDrivers\Common\MethodInterface;
|
2021-10-07 16:52:41 +02:00
|
|
|
use App\PaymentDrivers\RazorpayPaymentDriver;
|
2021-10-07 16:54:56 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2022-06-21 11:57:17 +02:00
|
|
|
use Illuminate\Http\Request;
|
2021-10-07 16:54:56 +02:00
|
|
|
use Illuminate\View\View;
|
2021-10-07 18:01:36 +02:00
|
|
|
use Razorpay\Api\Errors\SignatureVerificationError;
|
2021-10-07 16:35:58 +02:00
|
|
|
|
2024-08-09 01:08:00 +02:00
|
|
|
class Hosted implements MethodInterface, LivewireMethodInterface
|
2021-10-07 16:35:58 +02:00
|
|
|
{
|
2021-10-07 16:52:41 +02:00
|
|
|
protected RazorpayPaymentDriver $razorpay;
|
|
|
|
|
|
|
|
public function __construct(RazorpayPaymentDriver $razorpay)
|
|
|
|
{
|
|
|
|
$this->razorpay = $razorpay;
|
|
|
|
|
|
|
|
$this->razorpay->init();
|
|
|
|
}
|
|
|
|
|
2021-10-07 16:54:56 +02:00
|
|
|
/**
|
|
|
|
* Show the authorization page for Razorpay.
|
|
|
|
*
|
|
|
|
* @param array $data
|
2023-10-26 04:57:44 +02:00
|
|
|
* @return \Illuminate\View\View
|
2021-10-07 16:54:56 +02:00
|
|
|
*/
|
|
|
|
public function authorizeView(array $data): View
|
|
|
|
{
|
|
|
|
return render('gateways.razorpay.hosted.authorize', $data);
|
|
|
|
}
|
2021-10-07 16:35:58 +02:00
|
|
|
|
2021-10-07 16:54:56 +02:00
|
|
|
/**
|
|
|
|
* Handle the authorization page for Razorpay.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2021-10-07 16:54:56 +02:00
|
|
|
*/
|
|
|
|
public function authorizeResponse(Request $request): RedirectResponse
|
|
|
|
{
|
|
|
|
return redirect()->route('client.payment_methods.index');
|
|
|
|
}
|
2021-10-07 16:35:58 +02:00
|
|
|
|
2021-10-07 18:01:36 +02:00
|
|
|
/**
|
|
|
|
* Payment view for the Razorpay.
|
2022-06-21 11:57:17 +02:00
|
|
|
*
|
|
|
|
* @param array $data
|
2023-10-26 04:57:44 +02:00
|
|
|
* @return \Illuminate\View\View
|
2021-10-07 18:01:36 +02:00
|
|
|
*/
|
|
|
|
public function paymentView(array $data): View
|
2021-10-07 16:54:56 +02:00
|
|
|
{
|
2024-08-09 01:08:00 +02:00
|
|
|
$data = $this->paymentData($data);
|
2021-10-07 18:01:36 +02:00
|
|
|
|
|
|
|
return render('gateways.razorpay.hosted.pay', $data);
|
2021-10-07 16:54:56 +02:00
|
|
|
}
|
2021-10-07 16:35:58 +02:00
|
|
|
|
2021-10-07 18:01:36 +02:00
|
|
|
/**
|
|
|
|
* Handle payments page for Razorpay.
|
2022-06-21 11:57:17 +02:00
|
|
|
*
|
|
|
|
* @param PaymentResponseRequest $request
|
|
|
|
* @return void
|
2021-10-07 18:01:36 +02:00
|
|
|
*/
|
2021-10-07 16:54:56 +02:00
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
2021-10-07 18:01:36 +02:00
|
|
|
$request->validate([
|
|
|
|
'payment_hash' => ['required'],
|
|
|
|
'razorpay_payment_id' => ['required'],
|
|
|
|
'razorpay_signature' => ['required'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (! property_exists($this->razorpay->payment_hash->data, 'order_id')) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->razorpay->sendFailureMail('Missing [order_id] property. ');
|
2021-10-17 13:21:56 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
throw new PaymentFailed('Missing [order_id] property. Please contact the administrator. Reference: '.$this->razorpay->payment_hash->hash);
|
2021-10-07 18:01:36 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-07 18:01:36 +02:00
|
|
|
try {
|
|
|
|
$attributes = [
|
|
|
|
'razorpay_order_id' => $this->razorpay->payment_hash->data->order_id,
|
|
|
|
'razorpay_payment_id' => $request->razorpay_payment_id,
|
|
|
|
'razorpay_signature' => $request->razorpay_signature,
|
|
|
|
];
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-07 18:01:36 +02:00
|
|
|
$this->razorpay->gateway->utility->verifyPaymentSignature($attributes);
|
|
|
|
|
|
|
|
return $this->processSuccessfulPayment($request->razorpay_payment_id);
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (SignatureVerificationError $exception) {
|
2021-10-07 18:01:36 +02:00
|
|
|
return $this->processUnsuccessfulPayment($exception);
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-07 18:01:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the successful payment for Razorpay.
|
|
|
|
*
|
|
|
|
* @param string $payment_id
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2021-10-07 18:01:36 +02:00
|
|
|
*/
|
|
|
|
public function processSuccessfulPayment(string $payment_id): RedirectResponse
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'gateway_type_id' => GatewayType::HOSTED_PAGE,
|
|
|
|
'amount' => array_sum(array_column($this->razorpay->payment_hash->invoices(), 'amount')) + $this->razorpay->payment_hash->fee_total,
|
|
|
|
'payment_type' => PaymentType::HOSTED_PAGE,
|
|
|
|
'transaction_reference' => $payment_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment_record = $this->razorpay->createPayment($data, Payment::STATUS_COMPLETED);
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $payment_id, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_RAZORPAY,
|
|
|
|
$this->razorpay->client,
|
|
|
|
$this->razorpay->client->company,
|
|
|
|
);
|
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->razorpay->encodePrimaryKey($payment_record->id)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle unsuccessful payment for Razorpay.
|
|
|
|
*
|
|
|
|
* @param Exception $exception
|
|
|
|
* @throws PaymentFailed
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function processUnsuccessfulPayment(\Exception $exception): void
|
|
|
|
{
|
2021-10-17 12:40:40 +02:00
|
|
|
$this->razorpay->sendFailureMail($exception->getMessage());
|
2021-10-07 18:01:36 +02:00
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$exception->getMessage(),
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_RAZORPAY,
|
|
|
|
$this->razorpay->client,
|
|
|
|
$this->razorpay->client->company,
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new PaymentFailed($exception->getMessage(), $exception->getCode());
|
2021-10-07 16:54:56 +02:00
|
|
|
}
|
2024-08-09 01:08:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function livewirePaymentView(array $data): string
|
|
|
|
{
|
|
|
|
return 'gateways.razorpay.hosted.pay_livewire';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function paymentData(array $data): array
|
|
|
|
{
|
|
|
|
$order = $this->razorpay->gateway->order->create([
|
|
|
|
'currency' => $this->razorpay->client->currency()->code,
|
|
|
|
'amount' => $this->razorpay->convertToRazorpayAmount((float) $this->razorpay->payment_hash->data->amount_with_fee),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->razorpay->payment_hash->withData('order_id', $order->id);
|
|
|
|
$this->razorpay->payment_hash->withData('order_amount', $order->amount);
|
|
|
|
|
|
|
|
$data['gateway'] = $this->razorpay;
|
|
|
|
|
|
|
|
$data['options'] = [
|
|
|
|
'key' => $this->razorpay->company_gateway->getConfigField('apiKey'),
|
|
|
|
'amount' => $this->razorpay->convertToRazorpayAmount((float) $this->razorpay->payment_hash->data->amount_with_fee),
|
|
|
|
'currency' => $this->razorpay->client->currency()->code,
|
|
|
|
'name' => $this->razorpay->company_gateway->company->present()->name(),
|
|
|
|
'order_id' => $order->id,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2021-10-07 16:35:58 +02:00
|
|
|
}
|