diff --git a/app/PaymentDrivers/PayFast/Token.php b/app/PaymentDrivers/PayFast/Token.php index 6e2dfe8dc5..cb8f02513f 100644 --- a/app/PaymentDrivers/PayFast/Token.php +++ b/app/PaymentDrivers/PayFast/Token.php @@ -79,26 +79,17 @@ class Token $header =[ 'merchant-id' => $this->payfast->company_gateway->getConfigField('merchantId'), - 'version' => 'v1', 'timestamp' => now()->format('c'), + 'version' => 'v1', ]; - $body = [ - '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->payfast->genericWebhookUrl(), - 'm_payment_id' => $payment_hash->hash, + $body['body'] = [ 'amount' => $amount, 'item_name' => 'purchase', 'item_description' => 'Purchase', - 'subscription_type' => 1, - 'passphrase' => $this->payfast->company_gateway->getConfigField('passphrase'), - ]; - $header['signature'] = $this->payfast->generateSignature($body); + $header['signature'] = $this->generate_parameter_string(array_merge($header, $body)); //$header['signature'] = $this->genSig($body); nlog($this->payfast->company_gateway->getConfigField('merchantId')); @@ -147,6 +138,43 @@ class Token // } } + + protected function generate_parameter_string( $api_data, $sort_data_before_merge = true, $skip_empty_values = true ) { + + // if sorting is required the passphrase should be added in before sort. + if ( ! empty( $this->pass_phrase ) && $sort_data_before_merge ) { + $api_data['passphrase'] = $this->payfast->company_gateway->getConfigField('passPhrase'); + } + + if ( $sort_data_before_merge ) { + ksort( $api_data ); + } + + // concatenate the array key value pairs. + $parameter_string = ''; + foreach ( $api_data as $key => $val ) { + + if ( $skip_empty_values && empty( $val ) ) { + continue; + } + + if ( 'signature' !== $key ) { + $val = urlencode( $val ); + $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->pass_phrase ); + } else { + $parameter_string = rtrim( $parameter_string, '&' ); + } + + return $parameter_string; + } + private function genSig($data) { $fields = [];