mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-18 09:04:35 +01:00
125 lines
4.2 KiB
PHP
125 lines
4.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
*
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
*
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
*
|
|
* @license https://opensource.org/licenses/AAL
|
|
*/
|
|
|
|
namespace App\PaymentDrivers\Stripe;
|
|
|
|
use App\Exceptions\PaymentFailed;
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
|
use App\Jobs\Mail\PaymentFailureMailer;
|
|
use App\Jobs\Util\SystemLogger;
|
|
use App\Models\GatewayType;
|
|
use App\Models\Payment;
|
|
use App\Models\PaymentType;
|
|
use App\Models\SystemLog;
|
|
use App\PaymentDrivers\StripePaymentDriver;
|
|
|
|
class Alipay
|
|
{
|
|
/** @var StripePaymentDriver */
|
|
public $stripe;
|
|
|
|
public function __construct(StripePaymentDriver $stripe)
|
|
{
|
|
$this->stripe = $stripe;
|
|
}
|
|
|
|
public function paymentView(array $data)
|
|
{
|
|
$data['gateway'] = $this->stripe;
|
|
$data['return_url'] = $this->buildReturnUrl();
|
|
$data['currency'] = $this->stripe->client->getCurrencyCode();
|
|
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision);
|
|
$data['invoices'] = $this->stripe->payment_hash->invoices();
|
|
|
|
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]);
|
|
$this->stripe->payment_hash->save();
|
|
|
|
return render('gateways.stripe.alipay.pay', $data);
|
|
}
|
|
|
|
private function buildReturnUrl(): string
|
|
{
|
|
return route('client.payments.response', [
|
|
'company_gateway_id' => $this->stripe->company_gateway->id,
|
|
'payment_hash' => $this->stripe->payment_hash->hash,
|
|
'payment_method_id' => GatewayType::ALIPAY,
|
|
]);
|
|
}
|
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
{
|
|
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $request->all());
|
|
$this->stripe->payment_hash->save();
|
|
|
|
if ($request->redirect_status == 'succeeded') {
|
|
return $this->processSuccesfulRedirect($request->source);
|
|
}
|
|
|
|
return $this->processUnsuccesfulRedirect();
|
|
}
|
|
|
|
public function processSuccesfulRedirect(string $source)
|
|
{
|
|
$this->stripe->init();
|
|
|
|
$data = [
|
|
'payment_method' => $this->stripe->payment_hash->data->source,
|
|
'payment_type' => PaymentType::ALIPAY,
|
|
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision),
|
|
'transaction_reference' => $source,
|
|
'gateway_type_id' => GatewayType::ALIPAY,
|
|
|
|
];
|
|
|
|
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
|
|
|
|
SystemLogger::dispatch(
|
|
['response' => $this->stripe->payment_hash->data, 'data' => $data],
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
SystemLog::TYPE_STRIPE,
|
|
$this->stripe->client
|
|
);
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
|
|
}
|
|
|
|
public function processUnsuccesfulRedirect()
|
|
{
|
|
$server_response = $this->stripe->payment_hash->data;
|
|
|
|
PaymentFailureMailer::dispatch($this->stripe->client, $server_response->redirect_status, $this->stripe->client->company, $server_response->amount);
|
|
|
|
PaymentFailureMailer::dispatch(
|
|
$this->stripe->client,
|
|
$server_response,
|
|
$this->stripe->client->company,
|
|
$this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision)
|
|
);
|
|
|
|
$message = [
|
|
'server_response' => $server_response,
|
|
'data' => $this->stripe->payment_hash->data,
|
|
];
|
|
|
|
SystemLogger::dispatch(
|
|
$message,
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
SystemLog::TYPE_STRIPE,
|
|
$this->stripe->client
|
|
);
|
|
|
|
throw new PaymentFailed('Failed to process the payment.', 500);
|
|
}
|
|
}
|