1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/PaymentDrivers/Stripe/Alipay.php

122 lines
4.1 KiB
PHP
Raw Normal View History

2020-06-09 16:56:08 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-06-09 16:56:08 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. 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;
2020-06-09 16:56:08 +02:00
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
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();
2020-06-09 16:56:08 +02:00
$data['currency'] = $this->stripe->client->getCurrencyCode();
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['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();
2020-06-09 16:56:08 +02:00
return render('gateways.stripe.alipay.pay', $data);
}
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,
'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
]);
}
public function paymentResponse(PaymentResponseRequest $request)
2020-06-09 16:56:08 +02: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
if ($request->redirect_status == 'succeeded') {
return $this->processSuccesfulRedirect();
2020-06-09 16:56:08 +02:00
}
return $this->processUnsuccesfulRedirect();
2020-06-09 16:56:08 +02:00
}
public function processSuccesfulRedirect()
2020-06-09 16:56:08 +02:00
{
$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' => ctrans('texts.n/a'),
2020-06-09 16:56:08 +02:00
];
$payment = $this->stripe->createPayment($data, \App\Models\Payment::STATUS_PENDING);
2020-06-09 16:56:08 +02:00
SystemLogger::dispatch(
['response' => $this->stripe->payment_hash->data, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_STRIPE,
$this->stripe->client
);
2020-06-09 16:56:08 +02:00
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
}
public function processUnsuccesfulRedirect()
2020-06-09 16:56:08 +02:00
{
$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)
);
2020-06-09 16:56:08 +02:00
$message = [
'server_response' => $server_response,
'data' => $this->stripe->payment_hash->data,
2020-06-09 16:56:08 +02:00
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->stripe->client
);
2020-06-09 16:56:08 +02:00
throw new PaymentFailed('Failed to process the payment.', 500);
2020-06-09 16:56:08 +02:00
}
}