1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/PaymentDrivers/PayFast/Token.php

127 lines
3.8 KiB
PHP
Raw Normal View History

2021-07-06 12:02:47 +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-07-06 12:02:47 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\PayFast;
use App\Models\ClientGatewayToken;
use App\Models\PaymentHash;
use App\PaymentDrivers\PayFastPaymentDriver;
use GuzzleHttp\RequestOptions;
2021-07-06 12:02:47 +02:00
class Token
{
public $payfast;
public function __construct(PayFastPaymentDriver $payfast)
{
$this->payfast = $payfast;
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$amount = round(($amount * pow(10, $this->payfast->client->currency()->precision)), 0);
2021-07-06 12:02:47 +02:00
$header = [
2021-09-29 02:26:21 +02:00
'merchant-id' => $this->payfast->company_gateway->getConfigField('merchantId'),
'version' => 'v1',
'timestamp' => now()->format('c'),
];
2021-09-29 02:26:21 +02:00
$body = [
'amount' => $amount,
'item_name' => 'purchase',
'item_description' => ctrans('texts.invoices').': '.collect($payment_hash->invoices())->pluck('invoice_number'),
2021-09-29 02:26:21 +02:00
'm_payment_id' => $payment_hash->hash,
];
2021-09-29 02:34:02 +02:00
$header['signature'] = $this->payfast->generateTokenSignature(array_merge($body, $header));
2021-09-29 02:26:21 +02:00
2022-02-17 04:02:23 +01:00
// nlog($header['signature']);
2021-09-29 02:26:21 +02:00
$result = $this->send($header, $body, $cgt->token);
2021-07-06 12:02:47 +02:00
}
protected function generate_parameter_string($api_data, $sort_data_before_merge = true, $skip_empty_values = true)
{
2021-09-23 06:08:55 +02:00
// if sorting is required the passphrase should be added in before sort.
if (! empty($this->payfast->company_gateway->getConfigField('passphrase')) && $sort_data_before_merge) {
2021-09-29 02:52:09 +02:00
$api_data['passphrase'] = $this->payfast->company_gateway->getConfigField('passphrase');
}
2021-09-23 06:08:55 +02:00
if ($sort_data_before_merge) {
ksort($api_data);
2021-09-23 06:08:55 +02:00
}
// concatenate the array key value pairs.
$parameter_string = '';
foreach ($api_data as $key => $val) {
if ($skip_empty_values && empty($val)) {
2021-09-23 06:08:55 +02:00
continue;
}
if ('signature' !== $key) {
$val = urlencode($val);
2021-09-23 06:08:55 +02:00
$parameter_string .= "$key=$val&";
}
}
// when not sorting passphrase should be added to the end before md5
if ($sort_data_before_merge) {
$parameter_string = rtrim($parameter_string, '&');
} elseif (! empty($this->pass_phrase)) {
$parameter_string .= 'passphrase='.urlencode($this->payfast->company_gateway->getConfigField('passphrase'));
2021-09-23 06:08:55 +02:00
} else {
$parameter_string = rtrim($parameter_string, '&');
2021-09-23 06:08:55 +02:00
}
2022-02-17 04:02:23 +01:00
// nlog($parameter_string);
2021-09-23 07:34:20 +02:00
return $parameter_string;
2021-09-23 06:08:55 +02:00
}
2021-07-06 12:02:47 +02:00
private function genSig($data)
{
$fields = [];
2021-07-06 12:02:47 +02:00
ksort($data);
2021-07-06 12:02:47 +02:00
foreach ($data as $key => $value) {
if (! empty($data[$key])) {
2021-07-06 12:02:47 +02:00
$fields[$key] = $data[$key];
}
}
2021-09-29 02:28:22 +02:00
nlog(http_build_query($fields));
2021-09-29 02:29:26 +02:00
2021-07-06 12:02:47 +02:00
return md5(http_build_query($fields));
}
private function send($headers, $body, $token)
{
$client = new \GuzzleHttp\Client(
2023-02-16 02:36:09 +01:00
[
'headers' => $headers,
]
);
2021-07-06 12:02:47 +02:00
try {
$response = $client->post("https://api.payfast.co.za/subscriptions/{$token}/adhoc?testing=true", [
RequestOptions::JSON => ['body' => $body], RequestOptions::ALLOW_REDIRECTS => false,
2021-09-23 06:20:04 +02:00
]);
2021-07-06 12:02:47 +02:00
return json_decode($response->getBody(), true);
} catch (\Exception $e) {
nlog($e->getMessage());
2021-07-06 12:02:47 +02:00
}
}
}