From 15bdb3ff3c4b3ed7a4c19e6d094c0fd72f8ef65b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 24 Jun 2021 15:46:10 +1000 Subject: [PATCH] Payfast --- app/Models/CompanyGateway.php | 7 ++ app/PaymentDrivers/PayFast/CreditCard.php | 66 ++++++++++++++++++- app/PaymentDrivers/PayFastPaymentDriver.php | 50 +++++++++++--- .../gateways/payfast/authorize.blade.php | 35 ++++------ 4 files changed, 125 insertions(+), 33 deletions(-) diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 35565ca410d3..a7860e982446 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -358,6 +358,11 @@ class CompanyGateway extends BaseModel return $fee; } + public function webhookUrl() + { + return route('payment_webhook', ['company_key' => $this->company->company_key, 'company_gateway_id' => $this->hashed_id]); + } + /** * we need to average out the gateway fees across all the invoices * so lets iterate. @@ -399,4 +404,6 @@ class CompanyGateway extends BaseModel return $this ->where('id', $this->decodePrimaryKey($value))->firstOrFail(); } + + } diff --git a/app/PaymentDrivers/PayFast/CreditCard.php b/app/PaymentDrivers/PayFast/CreditCard.php index 991ac41d6da4..85fef271eac9 100644 --- a/app/PaymentDrivers/PayFast/CreditCard.php +++ b/app/PaymentDrivers/PayFast/CreditCard.php @@ -32,11 +32,71 @@ class CreditCard $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); + */ + public function authorizeView($data) { - // $data['gateway'] = $this->wepay_payment_driver; - - // return render('gateways.wepay.authorize.authorize', $data); + $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'), + 'notify_url' => $this->company_gateway->webhookUrl(), + 'amount' => 0, + 'item_name' => 'pre-auth', + 'subscription_type' => 2, + 'passphrase' => $this->payfast->company_gateway->getConfigField('passphrase'), + ]; + + $data['signature'] = $this->payfast->generateSignature($data); + $data['gateway'] = $this->payfast; + + return render('gateways.payfast.authorize', $data); } public function authorizeResponse($request) diff --git a/app/PaymentDrivers/PayFastPaymentDriver.php b/app/PaymentDrivers/PayFastPaymentDriver.php index 5a3536fc7ace..3948b8cbbf44 100644 --- a/app/PaymentDrivers/PayFastPaymentDriver.php +++ b/app/PaymentDrivers/PayFastPaymentDriver.php @@ -18,6 +18,7 @@ use App\Models\PaymentHash; use App\Models\SystemLog; use App\PaymentDrivers\PayFast\CreditCard; use App\Utils\Traits\MakesHash; +use \PayFastPayment; class PayFastPaymentDriver extends BaseDriver { @@ -39,17 +40,33 @@ class PayFastPaymentDriver extends BaseDriver const SYSTEM_LOG_TYPE = SystemLog::TYPE_PAYFAST; + //Live: https://www.payfast.co.za/eng/process + //Sandbox: https://sandbox.payfast.co.za/eng/process + + public function gatewayTypes(): array + { + $types = []; + + $types[] = GatewayType::CREDIT_CARD; + + return $types; + } + public function init() { - $this->payfast = new PayFastPayment( - [ - 'merchantId' => $this->company_gateway->getConfigField('merchantId'), - 'merchantKey' => $this->company_gateway->getConfigField('merchantKey'), - 'passPhrase' => $this->company_gateway->getConfigField('passPhrase'), - 'testMode' => $this->company_gateway->getConfigField('testMode') - ] - ); + try{ + $this->payfast = new PayFastPayment( + [ + 'merchantId' => $this->company_gateway->getConfigField('merchantId'), + 'merchantKey' => $this->company_gateway->getConfigField('merchantKey'), + 'passPhrase' => $this->company_gateway->getConfigField('passPhrase'), + 'testMode' => $this->company_gateway->getConfigField('testMode') + ] + ); + } catch(Exception $e) { + echo 'There was an exception: '.$e->getMessage(); + } return $this; } @@ -90,4 +107,21 @@ class PayFastPaymentDriver extends BaseDriver { return $this->payment_method->yourTokenBillingImplmentation(); //this is your custom implementation from here } + + public function generateSignature($data, $passPhrase = null) + { + // Create parameter string + $pfOutput = ''; + foreach( $data as $key => $val ) { + if($val !== '') { + $pfOutput .= $key .'='. urlencode( trim( $val ) ) .'&'; + } + } + // Remove last ampersand + $getString = substr( $pfOutput, 0, -1 ); + if( $passPhrase !== null ) { + $getString .= '&passphrase='. urlencode( trim( $passPhrase ) ); + } + return md5( $getString ); + } } diff --git a/resources/views/portal/ninja2020/gateways/payfast/authorize.blade.php b/resources/views/portal/ninja2020/gateways/payfast/authorize.blade.php index 0ed71e8bf772..41a54580f5e4 100644 --- a/resources/views/portal/ninja2020/gateways/payfast/authorize.blade.php +++ b/resources/views/portal/ninja2020/gateways/payfast/authorize.blade.php @@ -1,32 +1,25 @@ @extends('portal.ninja2020.layout.payments', ['gateway_title' => ctrans('texts.credit_card'), 'card_title' => ctrans('texts.credit_card')]) @section('gateway_head') - - - - - - - - - - - @endsection @section('gateway_content') -
- @csrf + + + + + + + + + + + - - - - - -
+ + @if(!Request::isSecure())

{{ ctrans('texts.https_required') }}

@@ -38,8 +31,6 @@ {{ ctrans('texts.credit_card') }} @endcomponent - @include('portal.ninja2020.gateways.wepay.includes.credit_card') - @component('portal.ninja2020.gateways.includes.pay_now', ['id' => 'card_button']) {{ ctrans('texts.add_payment_method') }} @endcomponent