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;
|
|
|
|
|
2019-10-01 06:27:04 +02:00
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
2019-10-03 14:02:31 +02:00
|
|
|
use App\Jobs\Invoice\UpdateInvoicePayment;
|
2019-10-29 03:55:26 +01:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2019-09-26 07:14:07 +02:00
|
|
|
use App\Models\ClientGatewayToken;
|
|
|
|
use App\Models\GatewayType;
|
2019-10-02 00:44:13 +02:00
|
|
|
use App\Models\Payment;
|
2019-10-01 03:56:48 +02:00
|
|
|
use App\Models\PaymentType;
|
2019-11-05 00:26:15 +01:00
|
|
|
use App\Models\SystemLog;
|
2019-09-26 07:14:07 +02:00
|
|
|
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
|
|
|
|
2019-10-01 03:56:48 +02:00
|
|
|
/**
|
|
|
|
* Response array
|
|
|
|
* (
|
|
|
|
'TOKEN' => 'EC-50V302605X606694D',
|
|
|
|
'SUCCESSPAGEREDIRECTREQUESTED' => 'false',
|
|
|
|
'TIMESTAMP' => '2019-09-30T22:21:21Z',
|
|
|
|
'CORRELATIONID' => '9e0da63193090',
|
|
|
|
'ACK' => 'SuccessWithWarning',
|
|
|
|
'VERSION' => '119.0',
|
|
|
|
'BUILD' => '53688488',
|
|
|
|
'L_ERRORCODE0' => '11607',
|
|
|
|
'L_SHORTMESSAGE0' => 'Duplicate Request',
|
|
|
|
'L_LONGMESSAGE0' => 'A successful transaction has already been completed for this token.',
|
|
|
|
'L_SEVERITYCODE0' => 'Warning',
|
|
|
|
'INSURANCEOPTIONSELECTED' => 'false',
|
|
|
|
'SHIPPINGOPTIONISDEFAULT' => 'false',
|
|
|
|
'PAYMENTINFO_0_TRANSACTIONID' => '5JE20141KL116573G',
|
|
|
|
'PAYMENTINFO_0_TRANSACTIONTYPE' => 'expresscheckout',
|
|
|
|
'PAYMENTINFO_0_PAYMENTTYPE' => 'instant',
|
|
|
|
'PAYMENTINFO_0_ORDERTIME' => '2019-09-30T22:20:57Z',
|
|
|
|
'PAYMENTINFO_0_AMT' => '31260.37',
|
|
|
|
'PAYMENTINFO_0_TAXAMT' => '0.00',
|
|
|
|
'PAYMENTINFO_0_CURRENCYCODE' => 'USD',
|
|
|
|
'PAYMENTINFO_0_EXCHANGERATE' => '0.692213615971749',
|
|
|
|
'PAYMENTINFO_0_PAYMENTSTATUS' => 'Pending',
|
|
|
|
'PAYMENTINFO_0_PENDINGREASON' => 'unilateral',
|
|
|
|
'PAYMENTINFO_0_REASONCODE' => 'None',
|
|
|
|
'PAYMENTINFO_0_PROTECTIONELIGIBILITY' => 'Ineligible',
|
|
|
|
'PAYMENTINFO_0_PROTECTIONELIGIBILITYTYPE' => 'None',
|
|
|
|
'PAYMENTINFO_0_ERRORCODE' => '0',
|
|
|
|
'PAYMENTINFO_0_ACK' => 'Success',
|
2019-12-28 07:25:18 +01:00
|
|
|
)
|
2019-10-01 03:56:48 +02:00
|
|
|
*/
|
|
|
|
|
2019-09-26 07:14:07 +02:00
|
|
|
class PayPalExpressPaymentDriver extends BasePaymentDriver
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
use MakesHash;
|
2019-09-26 07:14:07 +02:00
|
|
|
|
|
|
|
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
|
2019-12-28 07:25:18 +01:00
|
|
|
*
|
2019-09-26 07:47:37 +02:00
|
|
|
* @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']
|
2019-12-28 07:25:18 +01:00
|
|
|
*
|
2019-09-26 07:47:37 +02:00
|
|
|
* @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-10-02 00:44:13 +02:00
|
|
|
$response = $this->purchase($this->paymentDetails($data), $this->paymentItems($data));
|
|
|
|
|
|
|
|
|
|
|
|
if ($response->isRedirect()) {
|
|
|
|
// redirect to offsite payment gateway
|
|
|
|
$response->redirect();
|
|
|
|
} elseif ($response->isSuccessful()) {
|
|
|
|
// payment was successful: update database
|
2019-12-28 07:25:18 +01:00
|
|
|
/* for this driver this method wont be hit*/
|
2019-10-02 00:44:13 +02:00
|
|
|
} else {
|
|
|
|
// payment failed: display message to customer
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
[
|
2019-10-02 00:44:13 +02:00
|
|
|
'server_response' => $response->getData(),
|
|
|
|
'data' => $data
|
2019-12-28 07:25:18 +01:00
|
|
|
],
|
2019-12-30 22:59:12 +01:00
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_PAYPAL,
|
|
|
|
$this->client
|
2019-10-29 03:55:26 +01:00
|
|
|
);
|
2019-10-02 00:44:13 +02:00
|
|
|
|
2019-10-04 08:22:22 +02:00
|
|
|
throw new \Exception("Error Processing Payment", 1);
|
2019-10-02 00:44:13 +02:00
|
|
|
}
|
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-10-01 03:56:48 +02:00
|
|
|
|
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()) {
|
2019-12-30 22:59:12 +01:00
|
|
|
return redirect()->route('client.invoices.index')->with('warning', ctrans('texts.status_voided'));
|
|
|
|
} elseif ($response->isSuccessful()) {
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
[
|
2019-11-05 00:26:15 +01:00
|
|
|
'server_response' => $response->getData(),
|
|
|
|
'data' => $request->all()
|
2019-12-28 07:25:18 +01:00
|
|
|
],
|
2019-12-30 22:59:12 +01:00
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_PAYPAL,
|
|
|
|
$this->client
|
2019-11-05 00:26:15 +01:00
|
|
|
);
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif (! $response->isSuccessful()) {
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
[
|
2019-10-29 03:55:26 +01:00
|
|
|
'data' => $request->all(),
|
2019-10-02 00:44:13 +02:00
|
|
|
'server_response' => $response->getData()
|
2019-12-28 07:25:18 +01:00
|
|
|
],
|
2019-12-30 22:59:12 +01:00
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_PAYPAL,
|
|
|
|
$this->client
|
|
|
|
);
|
2019-10-02 00:44:13 +02:00
|
|
|
|
2019-10-04 08:22:22 +02:00
|
|
|
throw new \Exception($response->getMessage());
|
2019-09-30 03:15:57 +02:00
|
|
|
}
|
2019-10-01 03:56:48 +02:00
|
|
|
|
2019-09-30 23:16:46 +02:00
|
|
|
$payment = $this->createPayment($response->getData());
|
2019-09-30 08:54:24 +02:00
|
|
|
|
2019-10-01 03:56:48 +02:00
|
|
|
$this->attachInvoices($payment, $request->input('hashed_ids'));
|
|
|
|
|
2019-12-28 07:25:18 +01:00
|
|
|
event(new PaymentWasCreated($payment, $payment->company));
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
UpdateInvoicePayment::dispatchNow($payment, $payment->company);
|
2019-10-01 06:27:04 +02:00
|
|
|
|
2019-10-01 03:56:48 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]);
|
2019-09-26 07:14:07 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 03:16:51 +02:00
|
|
|
protected function paymentDetails($input) :array
|
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 23:16:46 +02:00
|
|
|
private function buildReturnUrl($input) : string
|
2019-09-29 23:49:43 +02:00
|
|
|
{
|
2019-12-10 21:53:41 +01:00
|
|
|
$url = $this->client->company->domain() . "client/payments/process/response";
|
2019-09-30 03:15:57 +02:00
|
|
|
$url .= "?company_gateway_id={$this->company_gateway->id}&gateway_type_id=".GatewayType::PAYPAL;
|
2019-12-28 07:25:18 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:46 +02:00
|
|
|
private function buildCancelUrl($input) : string
|
2019-09-30 01:26:37 +02:00
|
|
|
{
|
2019-12-10 21:53:41 +01:00
|
|
|
$url = $this->client->company->domain() . '/client/invoices';
|
2019-09-30 01:26:37 +02:00
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:46 +02:00
|
|
|
private function buildDescription($input) : string
|
2019-09-30 01:26:37 +02:00
|
|
|
{
|
|
|
|
$invoice_numbers = "";
|
2019-12-28 07:25:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach ($input['invoices'] as $invoice) {
|
2019-11-27 11:27:24 +01:00
|
|
|
$invoice_numbers .= $invoice->number." ";
|
2019-09-30 01:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctrans('texts.invoice_number'). ": {$invoice_numbers}";
|
2019-09-29 23:49:43 +02:00
|
|
|
}
|
2019-09-30 01:26:37 +02:00
|
|
|
|
2019-09-30 23:16:46 +02:00
|
|
|
private function buildTransactionId($input) : string
|
2019-09-30 01:26:37 +02:00
|
|
|
{
|
2019-12-28 07:25:18 +01:00
|
|
|
return implode(",", $input['hashed_ids']);
|
2019-09-30 01:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function paymentItems($input) : array
|
|
|
|
{
|
|
|
|
$items = [];
|
|
|
|
$total = 0;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach ($input['invoices'] as $invoice) {
|
|
|
|
foreach ($invoice->line_items as $invoiceItem) {
|
2019-09-30 01:26:37 +02:00
|
|
|
// 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-12-28 07:25:18 +01:00
|
|
|
|
2019-10-02 00:44:13 +02:00
|
|
|
public function createPayment($data) : Payment
|
2019-09-30 07:27:05 +02:00
|
|
|
{
|
2019-09-30 08:54:24 +02:00
|
|
|
$payment = parent::createPayment($data);
|
|
|
|
|
2019-10-01 03:56:48 +02:00
|
|
|
$client_contact = $this->getContact();
|
|
|
|
$client_contact_id = $client_contact ? $client_contact->id : null;
|
|
|
|
|
|
|
|
$payment->amount = $data['PAYMENTINFO_0_AMT'];
|
2019-12-16 12:34:38 +01:00
|
|
|
$payment->type_id = PaymentType::PAYPAL;
|
2019-10-01 03:56:48 +02:00
|
|
|
$payment->transaction_reference = $data['PAYMENTINFO_0_TRANSACTIONID'];
|
|
|
|
$payment->client_contact_id = $client_contact_id;
|
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
return $payment;
|
2019-09-30 07:27:05 +02:00
|
|
|
}
|
2019-12-28 07:25:18 +01:00
|
|
|
}
|