55 * You (being anyone who is not PayFast (Pty) Ltd) may download and use this plugin / code in your own website in conjunction with a registered and active PayFast account. If your PayFast account is terminated for any reason, you may not use this plugin / code or part thereof.
66 * Except as expressly indicated in this licence, you may not use, copy, modify or distribute this plugin / code or part thereof in any way.
77 */
8-
98namespace Payfast \Payfast \Model ;
109
1110require_once dirname (__FILE__ ) . '/../Model/payfast_common.inc ' ;
3332use Magento \Store \Model \Store ;
3433use Magento \Store \Model \StoreManagerInterface ;
3534
36- /**
37- * PayFast Module.
38- *
39- * @method \Magento\Quote\Api\Data\PaymentMethodExtensionInterface getExtensionAttributes()
40- * @SuppressWarnings(PHPMD.TooManyFields)
41- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
42- */
35+ /**
36+ * PayFast Module.
37+ *
38+ * @method \Magento\Quote\Api\Data\PaymentMethodExtensionInterface getExtensionAttributes()
39+ * @SuppressWarnings(PHPMD.TooManyFields)
40+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
41+ */
42+
4343class Payfast
4444{
4545 /**
@@ -139,18 +139,18 @@ public function __construct(
139139 TransactionRepositoryInterface $ transactionRepository ,
140140 BuilderInterface $ transactionBuilder
141141 ) {
142- $ this ->_storeManager = $ storeManager ;
143- $ this ->_urlBuilder = $ urlBuilder ;
144- $ this ->_checkoutSession = $ checkoutSession ;
145- $ this ->_exception = $ exception ;
142+ $ this ->_storeManager = $ storeManager ;
143+ $ this ->_urlBuilder = $ urlBuilder ;
144+ $ this ->_checkoutSession = $ checkoutSession ;
145+ $ this ->_exception = $ exception ;
146146 $ this ->transactionRepository = $ transactionRepository ;
147- $ this ->transactionBuilder = $ transactionBuilder ;
147+ $ this ->transactionBuilder = $ transactionBuilder ;
148148
149- $ parameters = ['params ' => [$ this ->_code ] ];
149+ $ parameters = [ 'params ' => [ $ this ->_code ] ];
150150
151151 $ this ->_config = $ configFactory ->create ($ parameters );
152152
153- if (!defined ('PF_DEBUG ' )) {
153+ if (! defined ('PF_DEBUG ' )) {
154154 define ('PF_DEBUG ' , $ this ->_config ->getValue ('debug ' ));
155155 }
156156 }
@@ -191,8 +191,8 @@ public function canUseForCurrency($currencyCode)
191191 /**
192192 * Payment action getter compatible with payment model
193193 *
194- * @return string
195194 * @see \Magento\Sales\Model\Payment::place()
195+ * @return string
196196 */
197197 public function getConfigPaymentAction ()
198198 {
@@ -233,8 +233,9 @@ protected function getStoreName()
233233 * this where we compile data posted by the form to payfast
234234 *
235235 * @return array
236+ * @throws \Magento\Framework\Exception\NoSuchEntityException
236237 */
237- public function getStandardCheckoutFormFields (): array
238+ public function getStandardCheckoutFormFields ()
238239 {
239240 $ pre = __METHOD__ . ' : ' ;
240241 // Variable initialization
@@ -247,16 +248,13 @@ public function getStandardCheckoutFormFields(): array
247248
248249 // If NOT test mode, use normal credentials
249250 if ($ this ->_config ->getValue ('server ' ) == 'live ' ) {
250- $ merchantId = $ this ->_config ->getValue ('merchant_id ' );
251+ $ merchantId = $ this ->_config ->getValue ('merchant_id ' );
251252 $ merchantKey = $ this ->_config ->getValue ('merchant_key ' );
252- } // If test mode, use generic / specific sandbox credentials
253+ }
254+ // If test mode, use generic sandbox credentials
253255 else {
254- $ merchantId = !empty ($ this ->_config ->getValue ('merchant_id ' )) ?
255- $ this ->_config ->getValue ('merchant_id ' ) :
256- '10000100 ' ;
257- $ merchantKey = !empty ($ this ->_config ->getValue ('merchant_key ' )) ?
258- $ this ->_config ->getValue ('merchant_key ' ) :
259- '46f0cd694581a ' ;
256+ $ merchantId = '10000100 ' ;
257+ $ merchantKey = '46f0cd694581a ' ;
260258 }
261259
262260 // Create description
@@ -269,22 +267,22 @@ public function getStandardCheckoutFormFields(): array
269267 // Construct data for the form
270268 $ data = [
271269 // Merchant details
272- 'merchant_id ' => $ merchantId ,
273- 'merchant_key ' => $ merchantKey ,
274- 'return_url ' => $ this ->getPaidSuccessUrl (),
275- 'cancel_url ' => $ this ->getPaidCancelUrl (),
276- 'notify_url ' => $ this ->getPaidNotifyUrl (),
270+ 'merchant_id ' => $ merchantId ,
271+ 'merchant_key ' => $ merchantKey ,
272+ 'return_url ' => $ this ->getPaidSuccessUrl (),
273+ 'cancel_url ' => $ this ->getPaidCancelUrl (),
274+ 'notify_url ' => $ this ->getPaidNotifyUrl (),
277275
278276 // Buyer details
279- 'name_first ' => $ order ->getData ('customer_firstname ' ),
280- 'name_last ' => $ order ->getData ('customer_lastname ' ),
277+ 'name_first ' => $ order ->getData ('customer_firstname ' ),
278+ 'name_last ' => $ order ->getData ('customer_lastname ' ),
281279 'email_address ' => $ order ->getData ('customer_email ' ),
282280
283281 // Item details
284- 'm_payment_id ' => $ order ->getRealOrderId (),
285- 'amount ' => $ this ->getTotalAmount ($ order ),
286- 'item_name ' => ' Order # ' . $ order ->getRealOrderId (),
287- //this html special characters breaks signature.
282+ 'm_payment_id ' => $ order ->getRealOrderId (),
283+ 'amount ' => $ this ->getTotalAmount ($ order ),
284+ 'item_name ' => $ this -> _storeManager -> getStore ()-> getName () . ' , Order # ' . $ order ->getRealOrderId (),
285+ //this html special characters breaks signature.
288286 //'item_description' => $pfDescription,
289287 ];
290288
@@ -297,21 +295,21 @@ public function getStandardCheckoutFormFields(): array
297295 }
298296
299297 $ passPhrase = $ this ->_config ->getValue ('passphrase ' );
300- if (! empty ( $ passPhrase )) {
301- $ pfOutput .= ' passphrase= ' . urlencode ( $ passPhrase );
302- } else {
303- $ pfOutput = rtrim ( $ pfOutput, ' & ' );
298+ $ pfOutput = substr ( $ pfOutput , 0 , - 1 );
299+
300+ if (! empty ( $ passPhrase ) && $ this -> _config -> getValue ( ' server ' ) !== ' test ' ) {
301+ $ pfOutput = $ pfOutput . " &passphrase= " . urlencode ( $ passPhrase );
304302 }
305303
306304 pflog ($ pre . 'pfOutput for signature is : ' . $ pfOutput );
307305
308306 $ pfSignature = md5 ($ pfOutput );
309307
310- $ data ['signature ' ] = $ pfSignature ;
308+ $ data ['signature ' ] = $ pfSignature ;
311309 $ data ['user_agent ' ] = 'Magento ' . $ this ->getAppVersion ();
312310 pflog ($ pre . 'data is : ' . print_r ($ data , true ));
313311
314- return ($ data );
312+ return ($ data );
315313 }
316314
317315 /**
@@ -322,11 +320,10 @@ public function getStandardCheckoutFormFields(): array
322320 private function getAppVersion (): string
323321 {
324322 $ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
325- $ version = $ objectManager ->get ('Magento\Framework\App\ProductMetadataInterface ' )->getVersion ();
323+ $ version = $ objectManager ->get ('Magento\Framework\App\ProductMetadataInterface ' )->getVersion ();
326324
327- return (preg_match ('([0-9]) ' , $ version )) ? $ version : '2.0.0 ' ;
325+ return (preg_match ('([0-9]) ' , $ version )) ? $ version : '2.0.0 ' ;
328326 }
329-
330327 /**
331328 * getTotalAmount
332329 */
@@ -354,7 +351,7 @@ public function getNumberFormat($number)
354351 */
355352 public function getPaidSuccessUrl ()
356353 {
357- return $ this ->_urlBuilder ->getUrl ('payfast/redirect/success ' , ['_secure ' => true ]);
354+ return $ this ->_urlBuilder ->getUrl ('payfast/redirect/success ' , [ '_secure ' => true ]);
358355 }
359356
360357 /**
@@ -366,11 +363,7 @@ public function getPaidSuccessUrl()
366363 */
367364 protected function getOrderTransaction ($ payment )
368365 {
369- return $ this ->transactionRepository ->getByTransactionType (
370- Transaction::TYPE_ORDER ,
371- $ payment ->getId (),
372- $ payment ->getOrder ()->getId ()
373- );
366+ return $ this ->transactionRepository ->getByTransactionType (Transaction::TYPE_ORDER , $ payment ->getId (), $ payment ->getOrder ()->getId ());
374367 }
375368
376369 /*
@@ -383,13 +376,12 @@ public function getOrderPlaceRedirectUrl()
383376
384377 return $ this ->_urlBuilder ->getUrl ('payfast/redirect ' );
385378 }
386-
387379 /**
388380 * Checkout redirect URL getter for onepage checkout (hardcode)
389381 *
390- * @return string
391- * @see Quote\Payment::getCheckoutRedirectUrl()
392382 * @see \Magento\Checkout\Controller\Onepage::savePaymentAction()
383+ * @see Quote\Payment::getCheckoutRedirectUrl()
384+ * @return string
393385 */
394386 public function getCheckoutRedirectUrl ()
395387 {
@@ -405,15 +397,14 @@ public function getCheckoutRedirectUrl()
405397 */
406398 public function getPaidCancelUrl ()
407399 {
408- return $ this ->_urlBuilder ->getUrl ('payfast/redirect/cancel ' , ['_secure ' => true ]);
400+ return $ this ->_urlBuilder ->getUrl ('payfast/redirect/cancel ' , [ '_secure ' => true ]);
409401 }
410-
411402 /**
412403 * getPaidNotifyUrl
413404 */
414405 public function getPaidNotifyUrl ()
415406 {
416- return $ this ->_urlBuilder ->getUrl ('payfast/notify ' , ['_secure ' => true ]);
407+ return $ this ->_urlBuilder ->getUrl ('payfast/notify ' , [ '_secure ' => true ]);
417408 }
418409
419410 /**
@@ -423,7 +414,7 @@ public function getPaidNotifyUrl()
423414 */
424415 public function getPayFastUrl ()
425416 {
426- return ('https:// ' . $ this ->getPayfastHost ($ this ->_config ->getValue ('server ' )) . '/eng/process ' );
417+ return ('https:// ' . $ this ->getPayfastHost ($ this ->_config ->getValue ('server ' )) . '/eng/process ' );
427418 }
428419
429420 /**
@@ -433,7 +424,7 @@ public function getPayFastUrl()
433424 */
434425 public function getPayfastHost ($ serverMode )
435426 {
436- if (!in_array ($ serverMode , ['live ' , 'test ' ])) {
427+ if (!in_array ($ serverMode , [ 'live ' , 'test ' ])) {
437428 $ pfHost = "payfast. {$ serverMode }" ;
438429 } else {
439430 $ pfHost = (($ serverMode == 'live ' ) ? 'www ' : 'sandbox ' ) . '.payfast.co.za ' ;
0 commit comments