@@ -604,29 +604,82 @@ public function isPaymentOK($parameters, &$object, &$action, $hookmanager)
604604 {
605605 global $ langs ;
606606
607- $ error = 0 ; // Error counter
608- $ ispaymentok = false ;
607+ require_once DOL_DOCUMENT_ROOT ."/core/lib/geturl.lib.php " ;
608+ include_once DOL_DOCUMENT_ROOT .'/core/lib/security.lib.php ' ;
609+ dol_include_once ('payplugdolicloud/lib/payplugdolicloud.lib.php ' );
610+ $ langs ->load ("payplugdolicloud@payplugdolicloud " );
609611
612+ $ error = 0 ;
610613 if (in_array ($ parameters ['paymentmethod ' ], array ('payplug ' ))){
611614 $ code = GETPOST ("code " );
612615
613616 if ($ code == "refused " ) {
614- $ ispaymentok = false ;
615617 $ error ++;
618+ $ this ->errors [] = $ langs ->trans ("ErrorPaymentRefused " );
616619 } else {
617- // TODO Do a check with payplug api call
620+ // Ensure the amount matches the requested value
621+ // Compare session amount from doPayment with PayPlug API response
622+ $ paymentId = isset ($ _SESSION ["PAYPLUG_DOLICLOUD_PAYMENT_ID " ]) ? $ _SESSION ["PAYPLUG_DOLICLOUD_PAYMENT_ID " ] : '' ;
623+ $ amounttotest = isset ($ _SESSION ["FinalPaymentAmt " ]) ? $ _SESSION ["FinalPaymentAmt " ] : 0 ;
624+ $ currencyCodeTypetotest = isset ($ _SESSION ["currencyCodeType " ]) ? $ _SESSION ["currencyCodeType " ] : '' ;
625+
626+ if (empty ($ paymentId ) || empty ($ amounttotest ) || empty ($ currencyCodeTypetotest )) {
627+ $ error ++;
628+ $ this ->errors [] = $ langs ->trans ("ErrorSessionNotFound " );
629+ }
630+
631+ if (!$ error ) {
632+ $ payplugrurlapi = "api.payplug.com " ;
633+ if (getDolGlobalInt ("PAYPLUG_DOLICLOUD_LIVE " )) {
634+ $ secretapikey = getDolGlobalString ("PAYPLUG_DOLICLOUD_PROD_SECRET_API_KEY " );
635+ } else {
636+ $ secretapikey = getDolGlobalString ("PAYPLUG_DOLICLOUD_TEST_SECRET_API_KEY " );
637+ }
638+
639+ // Verify if payment is done
640+ $ headers = array ();
641+ $ headers [] = "accept: application/json " ;
642+ $ headers [] = "Authorization: Bearer " .$ secretapikey ;
643+ $ headers [] = "Content-Type: application/json " ;
644+ $ urlforcheckout = "https:// " .urlencode ($ payplugrurlapi )."/v1/payments/ " .$ paymentId ;
645+ $ ret1 = getURLContent ($ urlforcheckout , 'GET ' , '' , 1 , $ headers );
646+ if (empty ($ ret1 ) || !is_array ($ ret1 ) || empty ($ ret1 ['content ' ])) {
647+ $ error ++;
648+ $ this ->errors [] = $ langs ->trans ("ErrorConnectionToPayplugFailed " );
649+ $ json1 = null ;
650+ } else {
651+ $ json1 = json_decode ($ ret1 ['content ' ]);
652+ }
618653
619654
655+ if ($ ret1 ["http_code " ] == 200 && empty ($ json1 ->failure ) && !$ error ) {
656+ if (empty ($ json1 ->id ) || $ json1 ->id != $ _SESSION ["PAYPLUG_DOLICLOUD_PAYMENT_ID " ]) {
657+ $ error ++;
658+ $ this ->errors [] = $ langs ->trans ("ErrorPaymentNotFound " );
659+ }
660+ $ amountfrompayplug = !empty ($ json1 ->amount ) ? $ json1 ->amount / 100 : 0 ; // Payplug amount is in cents
661+ $ currencyfrompayplug = !empty ($ json1 ->currency ) ? $ json1 ->currency : '' ;
620662
621- $ ispaymentok = true ;
663+ if ($ amounttotest != $ amountfrompayplug ) {
664+ $ error ++;
665+ $ this ->errors [] = $ langs ->trans ("ErrorValueFinalPaymentDiffers " );
666+ }
667+ if ($ currencyCodeTypetotest != $ currencyfrompayplug ) {
668+ $ error ++;
669+ $ this ->errors [] = $ langs ->trans ("ErrorValueFinalPaymentDiffersCurrency " );
670+ }
671+ } else {
672+ $ error ++;
673+ $ this ->errors [] = $ langs ->trans ("ErrorPaymentNotFound " );
674+ }
675+ }
622676 }
623677 }
624678
625679 if (!$ error ) {
626- $ this ->results ["ispaymentok " ] = $ ispaymentok ;
680+ $ this ->results ["ispaymentok " ] = true ;
627681 return 1 ;
628682 } else {
629- $ this ->errors [] = $ langs ->trans ("PaymentRefused " );
630683 return -1 ;
631684 }
632685 }
0 commit comments