2019-09-26 07:14:07 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers;
|
|
|
|
|
|
|
|
use App\Models\ClientGatewayToken;
|
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Http\Request;
|
2019-09-30 01:26:37 +02:00
|
|
|
use Omnipay\Common\Item;
|
2019-09-26 07:14:07 +02:00
|
|
|
|
|
|
|
class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|
|
|
{
|
|
|
|
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
protected $refundable = false;
|
|
|
|
|
|
|
|
protected $token_billing = false;
|
|
|
|
|
|
|
|
protected $can_authorise_credit_card = false;
|
|
|
|
|
|
|
|
protected $customer_reference = '';
|
|
|
|
|
|
|
|
|
|
|
|
public function gatewayTypes()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
GatewayType::PAYPAL,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:47:37 +02:00
|
|
|
/**
|
|
|
|
* Processes the payment with this gateway
|
|
|
|
*
|
|
|
|
* @var $data['invoices']
|
|
|
|
* @var $data['amount']
|
|
|
|
* @var $data['fee']
|
|
|
|
* @var $data['amount_with_fee']
|
|
|
|
* @var $data['token']
|
|
|
|
* @var $data['payment_method_id']
|
|
|
|
* @var $data['hashed_ids']
|
|
|
|
*
|
|
|
|
* @param array $data variables required to build payment page
|
|
|
|
* @return view Gateway and payment method specific view
|
|
|
|
*/
|
2019-09-26 07:14:07 +02:00
|
|
|
public function processPaymentView(array $data)
|
|
|
|
{
|
2019-09-30 07:27:05 +02:00
|
|
|
$this->purchase($this->paymentDetails($data), $this->paymentItems($data));
|
2019-09-26 07:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function processPaymentResponse($request)
|
|
|
|
{
|
|
|
|
|
2019-09-30 03:15:57 +02:00
|
|
|
$response = $this->completePurchase($request->all());
|
2019-09-30 08:54:24 +02:00
|
|
|
\Log::error($request->all());
|
2019-09-30 07:27:05 +02:00
|
|
|
$transaction_reference = $response->getTransactionReference() ?: $request->input('token');
|
2019-09-30 03:15:57 +02:00
|
|
|
|
|
|
|
if ($response->isCancelled()) {
|
|
|
|
return false;
|
|
|
|
} elseif (! $response->isSuccessful()) {
|
|
|
|
throw new Exception($response->getMessage());
|
|
|
|
}
|
2019-09-30 08:54:24 +02:00
|
|
|
//\Log::error(print_r($response,1));
|
|
|
|
//\Log::error(print_r($response->getData()));
|
|
|
|
\Log::error($response->getData());
|
|
|
|
//dd($response);
|
|
|
|
$payment = $this->createPayment($response);
|
|
|
|
|
2019-09-30 03:15:57 +02:00
|
|
|
|
2019-09-26 07:14:07 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 01:26:37 +02:00
|
|
|
protected function paymentDetails($input)
|
2019-09-26 07:14:07 +02:00
|
|
|
{
|
2019-09-30 01:26:37 +02:00
|
|
|
$data = parent::paymentDetails($input);
|
2019-09-26 07:14:07 +02:00
|
|
|
|
2019-09-30 01:26:37 +02:00
|
|
|
$data['amount'] = $input['amount_with_fee'];
|
|
|
|
$data['returnUrl'] = $this->buildReturnUrl($input);
|
|
|
|
$data['cancelUrl'] = $this->buildCancelUrl($input);
|
|
|
|
$data['description'] = $this->buildDescription($input);
|
|
|
|
$data['transactionId'] = $this->buildTransactionId($input);
|
2019-09-26 07:47:37 +02:00
|
|
|
|
2019-09-26 07:14:07 +02:00
|
|
|
$data['ButtonSource'] = 'InvoiceNinja_SP';
|
|
|
|
$data['solutionType'] = 'Sole'; // show 'Pay with credit card' option
|
|
|
|
$data['transactionId'] = $data['transactionId'] . '-' . time();
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2019-09-29 23:49:43 +02:00
|
|
|
|
2019-09-30 01:26:37 +02:00
|
|
|
private function buildReturnUrl($input)
|
2019-09-29 23:49:43 +02:00
|
|
|
{
|
2019-09-30 03:15:57 +02:00
|
|
|
$url = $this->client->company->domain . "client/payments/process/response";
|
|
|
|
$url .= "?company_gateway_id={$this->company_gateway->id}&gateway_type_id=".GatewayType::PAYPAL;
|
|
|
|
$url .= "&hashed_ids=" . implode(",", $input['hashed_ids']);
|
2019-09-30 01:26:37 +02:00
|
|
|
$url .= "&amount=".$input['amount'];
|
|
|
|
$url .= "&fee=".$input['fee'];
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildCancelUrl($input)
|
|
|
|
{
|
|
|
|
$url = $this->client->company->domain . '/client/invoices';
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildDescription($input)
|
|
|
|
{
|
|
|
|
$invoice_numbers = "";
|
|
|
|
|
|
|
|
foreach($input['invoices'] as $invoice)
|
|
|
|
{
|
|
|
|
$invoice_numbers .= $invoice->invoice_number." ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctrans('texts.invoice_number'). ": {$invoice_numbers}";
|
|
|
|
|
2019-09-29 23:49:43 +02:00
|
|
|
}
|
2019-09-30 01:26:37 +02:00
|
|
|
|
|
|
|
private function buildTransactionId($input)
|
|
|
|
{
|
|
|
|
return implode(",", $input['hashed_ids']);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function paymentItems($input) : array
|
|
|
|
{
|
|
|
|
|
|
|
|
$items = [];
|
|
|
|
$total = 0;
|
|
|
|
|
|
|
|
foreach ($input['invoices'] as $invoice)
|
|
|
|
{
|
|
|
|
foreach($invoice->line_items as $invoiceItem)
|
|
|
|
{
|
|
|
|
// Some gateways require quantity is an integer
|
|
|
|
if (floatval($invoiceItem->quantity) != intval($invoiceItem->quantity)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = new Item([
|
|
|
|
'name' => $invoiceItem->product_key,
|
|
|
|
'description' => substr($invoiceItem->notes, 0, 100),
|
|
|
|
'price' => $invoiceItem->cost,
|
|
|
|
'quantity' => $invoiceItem->quantity,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$items[] = $item;
|
|
|
|
|
|
|
|
$total += $invoiceItem->cost * $invoiceItem->quantity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($total != $input['amount_with_fee']) {
|
|
|
|
$item = new Item([
|
|
|
|
'name' => trans('texts.taxes_and_fees'),
|
|
|
|
'description' => '',
|
|
|
|
'price' => $input['amount_with_fee'] - $total,
|
|
|
|
'quantity' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$items[] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
2019-09-30 08:54:24 +02:00
|
|
|
public function createPayment($data)
|
2019-09-30 07:27:05 +02:00
|
|
|
{
|
2019-09-30 08:54:24 +02:00
|
|
|
$payment = parent::createPayment($data);
|
|
|
|
|
|
|
|
$payment->amount = $this->convertFromStripeAmount($server_response->amount, $this->client->currency->precision);
|
|
|
|
$payment->payment_type_id = PaymentType::PAYPAL;
|
|
|
|
$payment->transaction_reference = $payment_method;
|
|
|
|
$payment->client_contact_id = $this->getContact();
|
|
|
|
|
|
|
|
|
2019-09-30 07:27:05 +02:00
|
|
|
}
|
2019-09-26 23:54:03 +02:00
|
|
|
}
|