1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/PaymentDrivers/Stripe/ApplePay.php

107 lines
4.0 KiB
PHP
Raw Normal View History

2021-08-16 00:54:11 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2021-08-16 00:54:11 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Stripe;
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\Stripe\CreditCard;
use App\PaymentDrivers\StripePaymentDriver;
2021-08-16 02:58:57 +02:00
use App\Utils\Ninja;
2021-08-16 00:54:11 +02:00
class ApplePay
{
/** @var StripePaymentDriver */
public $stripe_driver;
public function __construct(StripePaymentDriver $stripe_driver)
{
$this->stripe_driver = $stripe_driver;
}
public function paymentView(array $data)
{
2021-08-16 02:58:57 +02:00
$this->registerDomain();
2021-08-16 00:54:11 +02:00
$data['gateway'] = $this->stripe_driver;
$data['payment_hash'] = $this->stripe_driver->payment_hash->hash;
$data['payment_method_id'] = GatewayType::APPLE_PAY;
$data['country'] = $this->stripe_driver->client->country;
$data['currency'] = $this->stripe_driver->client->currency()->code;
$data['stripe_amount'] = $this->stripe_driver->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe_driver->client->currency()->precision, $this->stripe_driver->client->currency());
$data['invoices'] = $this->stripe_driver->payment_hash->invoices();
$data['intent'] = \Stripe\PaymentIntent::create([
'amount' => $data['stripe_amount'],
'currency' => $this->stripe_driver->client->getCurrencyCode(),
'metadata' => [
'payment_hash' => $this->stripe_driver->payment_hash->hash,
'gateway_type_id' => GatewayType::APPLE_PAY,
],
], $this->stripe_driver->stripe_connect_auth);
2021-08-16 00:54:11 +02:00
$this->stripe_driver->payment_hash->data = array_merge((array) $this->stripe_driver->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]);
$this->stripe_driver->payment_hash->save();
return render('gateways.stripe.applepay.pay', $data);
}
public function paymentResponse(PaymentResponseRequest $request)
{
$this->stripe_driver->init();
$state = [
'server_response' => json_decode($request->gateway_response),
'payment_hash' => $request->payment_hash,
];
$state['payment_intent'] = \Stripe\PaymentIntent::retrieve($state['server_response']->id, $this->stripe_driver->stripe_connect_auth);
$state['customer'] = $state['payment_intent']->customer;
$this->stripe_driver->payment_hash->data = array_merge((array) $this->stripe_driver->payment_hash->data, $state);
$this->stripe_driver->payment_hash->save();
$server_response = $this->stripe_driver->payment_hash->data->server_response;
$response_handler = new CreditCard($this->stripe_driver);
2021-08-16 02:58:57 +02:00
2021-08-16 00:54:11 +02:00
if ($server_response->status == 'succeeded') {
$this->stripe_driver->logSuccessfulGatewayResponse(['response' => json_decode($request->gateway_response), 'data' => $this->stripe_driver->payment_hash], SystemLog::TYPE_STRIPE);
return $response_handler->processSuccessfulPayment();
}
return $response_handler->processUnsuccessfulPayment($server_response);
}
2021-08-16 02:58:57 +02:00
private function registerDomain()
{
if (Ninja::isHosted()) {
2021-08-16 02:58:57 +02:00
$domain = isset($this->stripe_driver->company_gateway->company->portal_domain) ? $this->stripe_driver->company_gateway->company->portal_domain : $this->stripe_driver->company_gateway->company->domain();
\Stripe\ApplePayDomain::create([
'domain_name' => $domain,
2021-08-16 02:58:57 +02:00
], $this->stripe_driver->stripe_connect_auth);
} else {
2021-08-16 02:58:57 +02:00
\Stripe\ApplePayDomain::create([
'domain_name' => config('ninja.app_url'),
2021-08-16 02:58:57 +02:00
]);
}
}
2021-08-16 00:54:11 +02:00
}