1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/PaymentDrivers/PayPalExpressPaymentDriver.php

220 lines
6.7 KiB
PHP
Raw Normal View History

2019-09-26 07:14:07 +02:00
<?php
2019-09-26 07:14:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-09-26 07:14:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-09-26 07:14:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-09-26 07:14:07 +02:00
*/
namespace App\PaymentDrivers;
2020-11-26 16:36:43 +01:00
use App\Exceptions\PaymentFailed;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
2019-09-26 07:14:07 +02:00
use App\Models\GatewayType;
2020-11-26 16:36:43 +01:00
use App\Models\Invoice;
2019-10-01 03:56:48 +02:00
use App\Models\PaymentType;
use App\Models\SystemLog;
2019-09-26 07:14:07 +02:00
use App\Utils\Traits\MakesHash;
2019-09-30 01:26:37 +02:00
use Omnipay\Common\Item;
2020-11-26 16:36:43 +01:00
use Omnipay\Omnipay;
class PayPalExpressPaymentDriver extends BaseDriver
2019-09-26 07:14:07 +02:00
{
use MakesHash;
2019-09-26 07:14:07 +02:00
2020-11-26 16:36:43 +01:00
public $token_billing = false;
2019-09-26 07:14:07 +02:00
2020-11-26 16:36:43 +01:00
public $can_authorise_credit_card = false;
2019-09-26 07:14:07 +02:00
2020-11-26 16:36:43 +01:00
private $omnipay_gateway;
2019-09-26 07:14:07 +02:00
2020-11-26 16:36:43 +01:00
const SYSTEM_LOG_TYPE = SystemLog::TYPE_PAYPAL;
2019-09-26 07:14:07 +02:00
public function gatewayTypes()
{
return [
GatewayType::PAYPAL,
];
}
2020-11-26 16:36:43 +01:00
/**
* Initialize Omnipay PayPal_Express gateway.
2020-11-27 11:12:05 +01:00
*
* @return void
2020-11-26 16:36:43 +01:00
*/
private function initializeOmnipayGateway(): void
{
2020-11-26 16:36:43 +01:00
$this->omnipay_gateway = Omnipay::create(
$this->company_gateway->gateway->provider
);
2020-11-26 16:36:43 +01:00
$this->omnipay_gateway->initialize((array) $this->company_gateway->getConfig());
}
2020-11-26 16:36:43 +01:00
public function setPaymentMethod($payment_method_id)
{
2020-11-27 11:12:05 +01:00
// PayPal doesn't have multiple ways of paying.
2020-11-26 16:36:43 +01:00
// There's just one, off-site redirect.
2020-11-26 16:36:43 +01:00
return $this;
}
2020-11-26 16:36:43 +01:00
public function authorizeView($payment_method)
{
// PayPal doesn't support direct authorization.
return $this;
}
2020-11-26 16:36:43 +01:00
public function authorizeResponse($request)
{
2020-11-26 16:36:43 +01:00
// PayPal doesn't support direct authorization.
2020-11-26 16:36:43 +01:00
return $this;
}
2020-11-26 16:36:43 +01:00
public function processPaymentView($data)
2019-09-26 07:14:07 +02:00
{
2020-11-26 16:36:43 +01:00
$this->initializeOmnipayGateway();
2020-12-21 12:10:47 +01:00
$this->payment_hash->data = array_merge((array) $this->payment_hash->data, ['amount' => $data['total']['amount_with_fee']]);
2020-11-26 16:36:43 +01:00
$this->payment_hash->save();
2020-11-26 16:36:43 +01:00
$response = $this->omnipay_gateway
->purchase($this->generatePaymentDetails($data))
->setItems($this->generatePaymentItems($data))
->send();
2019-10-02 00:44:13 +02:00
if ($response->isRedirect()) {
2020-11-26 16:36:43 +01:00
return $response->redirect();
}
2019-10-02 00:44:13 +02:00
2020-12-21 12:10:47 +01:00
PaymentFailureMailer::dispatch($this->client, $response->getData(), $this->client->company, $data['total']['amount_with_fee']);
2019-10-02 00:44:13 +02:00
2020-11-26 16:36:43 +01:00
$message = [
'server_response' => $response->getMessage(),
2021-06-09 04:24:32 +02:00
'data' => $this->payment_hash->data,
2020-11-26 16:36:43 +01:00
];
2019-09-26 07:14:07 +02:00
2020-11-26 16:36:43 +01:00
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_PAYPAL,
$this->client,
$this->client->company,
2020-11-26 16:36:43 +01:00
);
2020-11-26 16:36:43 +01:00
throw new PaymentFailed($response->getMessage(), $response->getCode());
}
2019-09-26 07:14:07 +02:00
public function processPaymentResponse($request)
{
2020-11-26 16:36:43 +01:00
$this->initializeOmnipayGateway();
2019-10-01 03:56:48 +02:00
2020-11-26 16:36:43 +01:00
$response = $this->omnipay_gateway
2021-05-24 13:11:47 +02:00
->completePurchase(['amount' => $this->payment_hash->data->amount, 'currency' => $this->client->getCurrencyCode()])
2020-11-26 16:36:43 +01:00
->send();
2019-09-30 03:15:57 +02:00
if ($response->isCancelled()) {
2020-07-06 13:22:36 +02:00
return redirect()->route('client.invoices.index')->with('warning', ctrans('texts.status_cancelled'));
2020-11-26 16:36:43 +01:00
}
if ($response->isSuccessful()) {
$data = [
'payment_method' => $response->getData()['TOKEN'],
'payment_type' => PaymentType::PAYPAL,
'amount' => $this->payment_hash->data->amount,
'transaction_reference' => $response->getTransactionReference(),
2021-01-27 11:38:28 +01:00
'gateway_type_id' => GatewayType::PAYPAL,
2020-11-26 16:36:43 +01:00
];
$payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED);
SystemLogger::dispatch(
2021-03-15 11:14:09 +01:00
['response' => (array)$response->getData(), 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_PAYPAL,
$this->client,
$this->client->company,
);
2020-11-26 16:36:43 +01:00
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
}
if (!$response->isSuccessful()) {
$data = $response->getData();
PaymentFailureMailer::dispatch($this->client, $response->getMessage(), $this->client->company, $this->payment_hash->data->amount);
2020-11-26 16:36:43 +01:00
$message = [
'server_response' => $data['L_LONGMESSAGE0'],
2020-11-26 16:36:43 +01:00
'data' => $this->payment_hash->data,
];
SystemLogger::dispatch(
2020-11-26 16:36:43 +01:00
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_PAYPAL,
$this->client,
$this->client->company,
);
2019-10-02 00:44:13 +02:00
2020-11-26 16:36:43 +01:00
throw new PaymentFailed($response->getMessage(), $response->getCode());
2019-09-30 01:26:37 +02:00
}
2019-09-29 23:49:43 +02:00
}
2019-09-30 01:26:37 +02:00
2020-11-26 16:36:43 +01:00
public function generatePaymentDetails(array $data)
2019-09-30 01:26:37 +02:00
{
2020-11-26 16:36:43 +01:00
return [
'currency' => $this->client->getCurrencyCode(),
'transactionType' => 'Purchase',
'clientIp' => request()->getClientIp(),
2020-12-21 12:10:47 +01:00
'amount' => $data['total']['amount_with_fee'],
2020-11-26 16:36:43 +01:00
'returnUrl' => route('client.payments.response', [
'company_gateway_id' => $this->company_gateway->id,
'payment_hash' => $this->payment_hash->hash,
'payment_method_id' => GatewayType::PAYPAL,
]),
'cancelUrl' => $this->client->company->domain() . '/client/invoices',
'description' => implode(',', collect($this->payment_hash->data->invoices)
->map(function ($invoice) {
2021-06-08 02:29:43 +02:00
return sprintf('%s: %s', ctrans('texts.invoice_number'), $invoice->invoice_number);
2020-11-26 16:36:43 +01:00
})->toArray()),
'transactionId' => $this->payment_hash->hash . '-' . time(),
'ButtonSource' => 'InvoiceNinja_SP',
'solutionType' => 'Sole',
];
2019-09-30 01:26:37 +02:00
}
2020-11-26 16:36:43 +01:00
public function generatePaymentItems(array $data)
2019-09-30 01:26:37 +02:00
{
$_invoice = collect($this->payment_hash->data->invoices)->first();
$invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($_invoice->invoice_id));
$line_item = collect($invoice->line_items)->first();
$items = [];
$items[] = new Item([
'name' => " ",
'description' => ctrans('texts.invoice_number') . "# " . $invoice->number,
'price' => $data['total']['amount_with_fee'],
2019-09-30 01:26:37 +02:00
'quantity' => 1,
]);
return $items;
}
}