1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/PaymentDrivers/PayFast/CreditCard.php

265 lines
9.6 KiB
PHP
Raw Normal View History

2021-06-24 06:43:14 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-06-24 06:43:14 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\PayFast;
use App\Exceptions\PaymentFailed;
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\PayFastPaymentDriver;
2021-07-05 12:45:00 +02:00
use Illuminate\Http\Request;
2021-07-06 12:02:47 +02:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
2021-06-24 06:43:14 +02:00
class CreditCard
{
public $payfast;
public function __construct(PayFastPaymentDriver $payfast)
{
$this->payfast = $payfast;
}
/*
$data = array();
$data['merchant_id'] = $this->getMerchantId();
$data['merchant_key'] = $this->getMerchantKey();
$data['return_url'] = $this->getReturnUrl();
$data['cancel_url'] = $this->getCancelUrl();
$data['notify_url'] = $this->getNotifyUrl();
if ($this->getCard()) {
$data['name_first'] = $this->getCard()->getFirstName();
$data['name_last'] = $this->getCard()->getLastName();
$data['email_address'] = $this->getCard()->getEmail();
}
$data['m_payment_id'] = $this->getTransactionId();
$data['amount'] = $this->getAmount();
$data['item_name'] = $this->getDescription();
$data['custom_int1'] = $this->getCustomInt1();
$data['custom_int2'] = $this->getCustomInt2();
$data['custom_int3'] = $this->getCustomInt3();
$data['custom_int4'] = $this->getCustomInt4();
$data['custom_int5'] = $this->getCustomInt5();
$data['custom_str1'] = $this->getCustomStr1();
$data['custom_str2'] = $this->getCustomStr2();
$data['custom_str3'] = $this->getCustomStr3();
$data['custom_str4'] = $this->getCustomStr4();
$data['custom_str5'] = $this->getCustomStr5();
if ($this->getPaymentMethod()) {
$data['payment_method'] = $this->getPaymentMethod();
}
if (1 == $this->getSubscriptionType()) {
$data['subscription_type'] = $this->getSubscriptionType();
$data['billing_date'] = $this->getBillingDate();
$data['recurring_amount'] = $this->getRecurringAmount();
$data['frequency'] = $this->getFrequency();
$data['cycles'] = $this->getCycles();
}
if (2 == $this->getSubscriptionType()) {
$data['subscription_type'] = $this->getSubscriptionType();
}
$data['passphrase'] = $this->getParameter('passphrase'); 123456789012aV
$data['signature'] = $this->generateSignature($data);
*/
2021-06-24 07:46:10 +02:00
2021-06-24 06:43:14 +02:00
public function authorizeView($data)
{
2021-07-05 06:07:43 +02:00
$hash = Str::random(32);
2021-07-05 06:07:43 +02:00
Cache::put($hash, 'cc_auth', 300);
2021-07-05 05:47:45 +02:00
2021-06-24 07:46:10 +02:00
$data = [
'merchant_id' => $this->payfast->company_gateway->getConfigField('merchantId'),
'merchant_key' => $this->payfast->company_gateway->getConfigField('merchantKey'),
'return_url' => route('client.payment_methods.index'),
'cancel_url' => route('client.payment_methods.index'),
2021-07-05 00:59:52 +02:00
'notify_url' => $this->payfast->genericWebhookUrl(),
2021-07-05 06:07:43 +02:00
'm_payment_id' => $hash,
2021-07-05 06:19:17 +02:00
'amount' => 5,
2021-06-24 07:46:10 +02:00
'item_name' => 'pre-auth',
2021-07-05 06:07:43 +02:00
'item_description' => 'Credit Card Pre Authorization',
2021-06-24 07:46:10 +02:00
'subscription_type' => 2,
'passphrase' => $this->payfast->company_gateway->getConfigField('passphrase'),
];
2021-06-24 07:46:10 +02:00
$data['signature'] = $this->payfast->generateSignature($data);
$data['gateway'] = $this->payfast;
2021-06-24 07:51:30 +02:00
$data['payment_endpoint_url'] = $this->payfast->endpointUrl();
2021-06-24 07:46:10 +02:00
return render('gateways.payfast.authorize', $data);
2021-06-24 06:43:14 +02:00
}
2021-07-05 05:36:12 +02:00
/*
'm_payment_id' => NULL,
'pf_payment_id' => '1409993',
'payment_status' => 'COMPLETE',
2021-07-05 05:36:12 +02:00
'item_name' => 'pre-auth',
'item_description' => NULL,
'amount_gross' => '5.00',
'amount_fee' => '-2.53',
'amount_net' => '2.47',
'custom_str1' => NULL,
'custom_str2' => NULL,
'custom_str3' => NULL,
'custom_str4' => NULL,
'custom_str5' => NULL,
'custom_int1' => NULL,
'custom_int2' => NULL,
'custom_int3' => NULL,
'custom_int4' => NULL,
'custom_int5' => NULL,
'name_first' => NULL,
'name_last' => NULL,
'email_address' => NULL,
'merchant_id' => '10023100',
'token' => '34b66bc2-3c54-9590-03ea-42ee8b89922a',
'billing_date' => '2021-07-05',
'signature' => 'ebdb4ca937d0e3f43462841c0afc6ad9',
'q' => '/payment_notification_webhook/EhbnVYyzJZyccY85hcHIkIzNPI2rtHzznAyyyG73oSxZidAdN9gf8BvAKDomqeHp/4openRe7Az/WPe99p3eLy',
*/
public function authorizeResponse($request)
{
2021-07-05 04:54:43 +02:00
$data = $request->all();
$cgt = [];
$cgt['token'] = $data['token'];
$cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
$payment_meta = new \stdClass;
$payment_meta->exp_month = 'xx';
$payment_meta->exp_year = 'xx';
$payment_meta->brand = 'CC';
$payment_meta->last4 = 'xxxx';
$payment_meta->type = GatewayType::CREDIT_CARD;
2021-07-05 05:01:30 +02:00
$cgt['payment_meta'] = $payment_meta;
2021-07-05 04:54:43 +02:00
2021-07-05 05:36:12 +02:00
$token = $this->payfast->storeGatewayToken($cgt, []);
2021-07-05 07:13:04 +02:00
return response()->json([], 200);
}
2021-06-24 06:43:14 +02:00
2021-07-05 07:13:04 +02:00
public function paymentView($data)
{
2021-07-05 12:55:01 +02:00
$payfast_data = [
2021-07-05 07:13:04 +02:00
'merchant_id' => $this->payfast->company_gateway->getConfigField('merchantId'),
'merchant_key' => $this->payfast->company_gateway->getConfigField('merchantKey'),
2021-07-05 15:06:25 +02:00
'return_url' => route('client.payments.index'),
2021-07-05 07:13:04 +02:00
'cancel_url' => route('client.payment_methods.index'),
'notify_url' => $this->payfast->genericWebhookUrl(),
2021-07-05 07:47:27 +02:00
'm_payment_id' => $data['payment_hash'],
2021-07-05 07:13:04 +02:00
'amount' => $data['amount_with_fee'],
'item_name' => 'purchase',
'item_description' => ctrans('texts.invoices').': '.collect($data['invoices'])->pluck('invoice_number'),
2021-07-05 07:13:04 +02:00
'passphrase' => $this->payfast->company_gateway->getConfigField('passphrase'),
];
2021-07-05 07:13:04 +02:00
2021-07-05 12:55:01 +02:00
$payfast_data['signature'] = $this->payfast->generateSignature($payfast_data);
$payfast_data['gateway'] = $this->payfast;
$payfast_data['payment_endpoint_url'] = $this->payfast->endpointUrl();
2021-07-05 07:13:04 +02:00
2021-07-05 12:55:01 +02:00
return render('gateways.payfast.pay', array_merge($data, $payfast_data));
2021-07-05 07:13:04 +02:00
}
2021-07-05 12:45:00 +02:00
2021-07-05 13:57:59 +02:00
/*
[2021-07-05 11:21:24] local.INFO: array (
'm_payment_id' => 'B7G9Q2vPhqkLEoMwwY1paXvPGuFxpbDe',
'pf_payment_id' => '1410364',
'payment_status' => 'COMPLETE',
'item_name' => 'purchase',
'item_description' => 'Invoices: ["0001"]',
'amount_gross' => '100.00',
'amount_fee' => '-2.30',
'amount_net' => '97.70',
'custom_str1' => NULL,
'custom_str2' => NULL,
'custom_str3' => NULL,
'custom_str4' => NULL,
'custom_str5' => NULL,
'custom_int1' => NULL,
'custom_int2' => NULL,
'custom_int3' => NULL,
'custom_int4' => NULL,
'custom_int5' => NULL,
'name_first' => NULL,
'name_last' => NULL,
'email_address' => NULL,
'merchant_id' => '10023100',
'signature' => '3ed27638479fd65cdffb0f4910679d10',
'q' => '/payment_notification_webhook/EhbnVYyzJZyccY85hcHIkIzNPI2rtHzznAyyyG73oSxZidAdN9gf8BvAKDomqeHp/4openRe7Az/WPe99p3eLy',
)
*/
2021-07-05 13:20:57 +02:00
public function paymentResponse(Request $request)
2021-07-05 12:45:00 +02:00
{
2021-07-05 13:57:59 +02:00
$response_array = $request->all();
$state = [
'server_response' => $request->all(),
'payment_hash' => $request->input('m_payment_id'),
];
2021-07-05 15:05:30 +02:00
$this->payfast->payment_hash->data = array_merge((array) $this->payfast->payment_hash->data, $state);
2021-07-05 13:57:59 +02:00
$this->payfast->payment_hash->save();
if ($response_array['payment_status'] == 'COMPLETE') {
2021-07-05 15:07:30 +02:00
$this->payfast->logSuccessfulGatewayResponse(['response' => $response_array, 'data' => $this->payfast->payment_hash], SystemLog::TYPE_PAYFAST);
2021-07-05 13:57:59 +02:00
2021-07-05 15:00:12 +02:00
return $this->processSuccessfulPayment($response_array);
} else {
2021-07-05 13:57:59 +02:00
$this->processUnsuccessfulPayment($response_array);
}
2021-07-05 12:45:00 +02:00
}
2021-06-24 06:43:14 +02:00
2021-07-05 15:00:12 +02:00
private function processSuccessfulPayment($response_array)
2021-07-05 13:57:59 +02:00
{
2021-07-05 15:00:12 +02:00
$payment_record = [];
$payment_record['amount'] = $response_array['amount_gross'];
$payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER;
$payment_record['gateway_type_id'] = GatewayType::CREDIT_CARD;
$payment_record['transaction_reference'] = $response_array['pf_payment_id'];
2021-07-05 13:57:59 +02:00
2021-07-05 15:00:12 +02:00
$payment = $this->payfast->createPayment($payment_record, Payment::STATUS_COMPLETED);
2021-07-05 13:57:59 +02:00
//return redirect()->route('client.payments.show', ['payment' => $this->payfast->encodePrimaryKey($payment->id)]);
2021-07-05 13:57:59 +02:00
}
private function processUnsuccessfulPayment($server_response)
{
2021-10-17 12:40:40 +02:00
$this->payfast->sendFailureMail($server_response->cancellation_reason);
2021-07-05 13:57:59 +02:00
$message = [
'server_response' => $server_response,
2021-07-05 15:00:12 +02:00
'data' => $this->payfast->payment_hash->data,
2021-07-05 13:57:59 +02:00
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
2021-07-05 15:00:12 +02:00
SystemLog::TYPE_PAYFAST,
$this->payfast->client,
$this->payfast->client->company,
2021-07-05 13:57:59 +02:00
);
throw new PaymentFailed('Failed to process the payment.', 500);
}
}