2020-06-09 16:56:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-06-09 16:56:08 +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-09 16:56:08 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-06-09 16:56:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Stripe;
|
|
|
|
|
2020-12-21 11:46:10 +01:00
|
|
|
use App\Models\Payment;
|
2020-06-09 16:56:08 +02:00
|
|
|
use App\Models\SystemLog;
|
2024-09-05 05:57:03 +02:00
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Jobs\Util\SystemLogger;
|
|
|
|
use App\Exceptions\PaymentFailed;
|
2020-06-09 16:56:08 +02:00
|
|
|
use App\PaymentDrivers\StripePaymentDriver;
|
2024-09-05 05:57:03 +02:00
|
|
|
use Stripe\Exception\InvalidRequestException;
|
|
|
|
use App\PaymentDrivers\Common\LivewireMethodInterface;
|
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
|
|
|
use Throwable;
|
2020-06-09 16:56:08 +02:00
|
|
|
|
2024-08-26 23:42:39 +02:00
|
|
|
class Alipay implements LivewireMethodInterface
|
2020-06-09 16:56:08 +02:00
|
|
|
{
|
|
|
|
/** @var StripePaymentDriver */
|
|
|
|
public $stripe;
|
|
|
|
|
|
|
|
public function __construct(StripePaymentDriver $stripe)
|
|
|
|
{
|
|
|
|
$this->stripe = $stripe;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentView(array $data)
|
|
|
|
{
|
2024-09-05 05:57:03 +02:00
|
|
|
|
2024-08-26 23:42:39 +02:00
|
|
|
$data = $this->paymentData($data);
|
2024-09-05 05:57:03 +02:00
|
|
|
|
2020-06-09 16:56:08 +02:00
|
|
|
return render('gateways.stripe.alipay.pay', $data);
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:20:38 +01:00
|
|
|
private function buildReturnUrl(): string
|
2020-06-09 16:56:08 +02:00
|
|
|
{
|
|
|
|
return route('client.payments.response', [
|
|
|
|
'company_gateway_id' => $this->stripe->company_gateway->id,
|
2020-11-02 16:20:38 +01:00
|
|
|
'payment_hash' => $this->stripe->payment_hash->hash,
|
2020-10-12 06:10:34 +02:00
|
|
|
'payment_method_id' => GatewayType::ALIPAY,
|
2020-06-09 16:56:08 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:20:38 +01:00
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
2020-06-09 16:56:08 +02:00
|
|
|
{
|
2023-02-23 02:47:29 +01:00
|
|
|
$this->stripe->init();
|
|
|
|
|
|
|
|
$this->stripe->setPaymentHash($request->getPaymentHash());
|
|
|
|
$this->stripe->client = $this->stripe->payment_hash->fee_invoice->client;
|
|
|
|
|
2020-11-02 16:20:38 +01:00
|
|
|
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $request->all());
|
|
|
|
$this->stripe->payment_hash->save();
|
2020-06-09 16:56:08 +02:00
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
if ($request->payment_intent) {
|
2023-02-23 02:47:29 +01:00
|
|
|
$pi = \Stripe\PaymentIntent::retrieve(
|
|
|
|
$request->payment_intent,
|
|
|
|
$this->stripe->stripe_connect_auth
|
|
|
|
);
|
|
|
|
|
|
|
|
if (in_array($pi->status, ['succeeded', 'pending'])) {
|
|
|
|
return $this->processSuccesfulRedirect($pi);
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:07:32 +02:00
|
|
|
/** @phpstan-ignore-next-line */
|
|
|
|
if ($pi->status == 'requires_source_action' && $pi->next_action->alipay_handle_redirect) {
|
|
|
|
/** @phpstan-ignore-next-line */
|
2023-02-23 02:47:29 +01:00
|
|
|
return redirect($pi->next_action->alipay_handle_redirect->url);
|
|
|
|
}
|
2020-06-09 16:56:08 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:20:38 +01:00
|
|
|
return $this->processUnsuccesfulRedirect();
|
2020-06-09 16:56:08 +02:00
|
|
|
}
|
|
|
|
|
2023-02-23 02:47:29 +01:00
|
|
|
public function processSuccesfulRedirect($payment_intent)
|
2020-06-09 16:56:08 +02:00
|
|
|
{
|
|
|
|
$this->stripe->init();
|
|
|
|
|
|
|
|
$data = [
|
2023-02-23 02:47:29 +01:00
|
|
|
'payment_method' => $payment_intent->payment_method,
|
2020-11-02 16:20:38 +01:00
|
|
|
'payment_type' => PaymentType::ALIPAY,
|
2021-06-30 05:56:11 +02:00
|
|
|
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
|
2023-02-23 02:47:29 +01:00
|
|
|
'transaction_reference' => $payment_intent->id,
|
2021-01-27 11:38:28 +01:00
|
|
|
'gateway_type_id' => GatewayType::ALIPAY,
|
|
|
|
|
2020-06-09 16:56:08 +02:00
|
|
|
];
|
|
|
|
|
2023-02-23 02:47:29 +01:00
|
|
|
$payment = $this->stripe->createPayment($data, $payment_intent->status == 'pending' ? Payment::STATUS_PENDING : Payment::STATUS_COMPLETED);
|
2020-06-09 16:56:08 +02:00
|
|
|
|
2020-11-02 16:20:38 +01:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $this->stripe->payment_hash->data, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_STRIPE,
|
2021-05-19 03:12:23 +02:00
|
|
|
$this->stripe->client,
|
|
|
|
$this->stripe->client->company,
|
2020-11-02 16:20:38 +01:00
|
|
|
);
|
2020-06-09 16:56:08 +02:00
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:20:38 +01:00
|
|
|
public function processUnsuccesfulRedirect()
|
2020-06-09 16:56:08 +02:00
|
|
|
{
|
2020-11-02 16:20:38 +01:00
|
|
|
$server_response = $this->stripe->payment_hash->data;
|
|
|
|
|
2021-10-17 12:40:40 +02:00
|
|
|
$this->stripe->sendFailureMail($server_response->redirect_status);
|
2020-06-09 16:56:08 +02:00
|
|
|
|
|
|
|
$message = [
|
2020-11-02 16:20:38 +01:00
|
|
|
'server_response' => $server_response,
|
|
|
|
'data' => $this->stripe->payment_hash->data,
|
2020-06-09 16:56:08 +02:00
|
|
|
];
|
|
|
|
|
2020-11-02 16:20:38 +01:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
$message,
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_STRIPE,
|
2021-05-19 03:12:23 +02:00
|
|
|
$this->stripe->client,
|
|
|
|
$this->stripe->client->company,
|
2020-11-02 16:20:38 +01:00
|
|
|
);
|
2020-06-09 16:56:08 +02:00
|
|
|
|
2020-11-02 16:20:38 +01:00
|
|
|
throw new PaymentFailed('Failed to process the payment.', 500);
|
2020-06-09 16:56:08 +02:00
|
|
|
}
|
2024-08-26 23:42:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function livewirePaymentView(array $data): string
|
|
|
|
{
|
|
|
|
return 'gateways.stripe.alipay.pay_livewire';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function paymentData(array $data): array
|
|
|
|
{
|
2024-09-05 05:57:03 +02:00
|
|
|
try {
|
|
|
|
$intent = \Stripe\PaymentIntent::create([
|
|
|
|
'amount' => $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
|
|
|
|
'currency' => $this->stripe->client->currency()->code,
|
|
|
|
'payment_method_types' => ['alipay'],
|
|
|
|
'customer' => $this->stripe->findOrCreateCustomer(),
|
|
|
|
'description' => $this->stripe->getDescription(false),
|
|
|
|
'metadata' => [
|
|
|
|
'payment_hash' => $this->stripe->payment_hash->hash,
|
|
|
|
'gateway_type_id' => GatewayType::ALIPAY,
|
|
|
|
],
|
|
|
|
], $this->stripe->stripe_connect_auth);
|
|
|
|
}
|
|
|
|
catch(\Throwable $e){
|
2024-08-26 23:42:39 +02:00
|
|
|
|
2024-09-05 05:57:03 +02:00
|
|
|
throw new PaymentFailed($e->getMessage(), $e->getCode());
|
|
|
|
|
|
|
|
}
|
2024-08-26 23:42:39 +02:00
|
|
|
|
|
|
|
$data['gateway'] = $this->stripe;
|
|
|
|
$data['return_url'] = $this->buildReturnUrl();
|
|
|
|
$data['ci_intent'] = $intent->client_secret;
|
|
|
|
|
|
|
|
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency())]);
|
|
|
|
$this->stripe->payment_hash->save();
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2020-06-09 16:56:08 +02:00
|
|
|
}
|