From 030df716a222403ce3d4531a0d2403877b483dd3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 7 Dec 2020 11:01:08 +0100 Subject: [PATCH] Clean dead code: Function showStripePaymentUrl, getStripePaymentUrl, showPaypalPaymentUrl and getPaypalPaymentUrl has been removed. The generic one showOnlinePaymentUrl and getOnlinePaymentUrl are always used. --- ChangeLog | 2 +- htdocs/paypal/lib/paypal.lib.php | 127 +------------------------ htdocs/stripe/lib/stripe.lib.php | 83 +--------------- test/phpunit/AllTests.php | 2 + test/phpunit/PaypalTest.php | 8 +- test/phpunit/StripeTest.php | 156 +++++++++++++++++++++++++++++++ 6 files changed, 167 insertions(+), 211 deletions(-) create mode 100644 test/phpunit/StripeTest.php diff --git a/ChangeLog b/ChangeLog index 7b685eb3381..9b29fe30e51 100644 --- a/ChangeLog +++ b/ChangeLog @@ -243,7 +243,7 @@ Following changes may create regressions for some external modules, but were nec * The API addPayment for api_invoice has evolved to accept amount into a foreign currency. You must provide array(amount=>X,mutlicurrency_ammount=>Y) instead of simple amount. * The method select_thirdparty(), deprecated since 3.8, into html.form.class.php has been removed. * Depreciate all methods with name ->valide(). Use instead methods ->validate(). - +* Function showStripePaymentUrl, getStripePaymentUrl, showPaypalPaymentUrl and getPaypalPaymentUrl has been removed. The generic one showOnlinePaymentUrl and getOnlinePaymentUrl are always used. ***** ChangeLog for 12.0.3 compared to 12.0.2 ***** diff --git a/htdocs/paypal/lib/paypal.lib.php b/htdocs/paypal/lib/paypal.lib.php index 00db8797a1f..655d501cac3 100644 --- a/htdocs/paypal/lib/paypal.lib.php +++ b/htdocs/paypal/lib/paypal.lib.php @@ -22,6 +22,8 @@ * \brief Library for common paypal functions */ +require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php'; + /** * Define head array for tabs of paypal tools setup pages @@ -54,131 +56,6 @@ function paypaladmin_prepare_head() } - -/** - * Return string with full Url - * - * @param string $type Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...) - * @param string $ref Ref of object - * @return string Url string - */ -function showPaypalPaymentUrl($type, $ref) -{ - global $conf, $langs; - - $langs->load("paypal"); - $langs->load("paybox"); - $servicename = 'PayPal'; - $out = '

'; - $out .= img_picto('', 'globe').' '.$langs->trans("ToOfferALinkForOnlinePayment", $servicename).'
'; - $url = getPaypalPaymentUrl(0, $type, $ref); - $out .= ''; - $out .= ajax_autoselect("paypalurl", 0); - return $out; -} - - -/** - * Return string with full Url - * - * @param int $mode 0=True url, 1=Url formated with colors - * @param string $type Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...) - * @param string $ref Ref of object - * @param int $amount Amount - * @param string $freetag Free tag - * @return string Url string - */ -function getPaypalPaymentUrl($mode, $type, $ref = '', $amount = '9.99', $freetag = 'your_tag') -{ - global $conf; - - $ref = str_replace(' ', '', $ref); - - if ($type == 'free') - { - $out = DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?amount='.($mode ? '' : '').$amount.($mode ? '' : '').'&tag='.($mode ? '' : '').$freetag.($mode ? '' : ''); - if (!empty($conf->global->PAYPAL_SECURITY_TOKEN)) - { - if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out .= '&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN; - else $out .= '&securekey='.dol_hash($conf->global->PAYPAL_SECURITY_TOKEN, 2); - } - } - if ($type == 'order') - { - $out = DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?source=order&ref='.($mode ? '' : ''); - if ($mode == 1) $out .= 'order_ref'; - if ($mode == 0) $out .= urlencode($ref); - $out .= ($mode ? '' : ''); - if (!empty($conf->global->PAYPAL_SECURITY_TOKEN)) - { - if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out .= '&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN; - else { - $out .= '&securekey='.($mode ? '' : ''); - if ($mode == 1) $out .= "hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + '".$type."' + order_ref)"; - if ($mode == 0) $out .= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN.$type.$ref, 2); - $out .= ($mode ? '' : ''); - } - } - } - if ($type == 'invoice') - { - $out = DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?source=invoice&ref='.($mode ? '' : ''); - if ($mode == 1) $out .= 'invoice_ref'; - if ($mode == 0) $out .= urlencode($ref); - $out .= ($mode ? '' : ''); - if (!empty($conf->global->PAYPAL_SECURITY_TOKEN)) - { - if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out .= '&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN; - else { - $out .= '&securekey='.($mode ? '' : ''); - if ($mode == 1) $out .= "hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + '".$type."' + invoice_ref)"; - if ($mode == 0) $out .= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN.$type.$ref, 2); - $out .= ($mode ? '' : ''); - } - } - } - if ($type == 'contractline') - { - $out = DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?source=contractline&ref='.($mode ? '' : ''); - if ($mode == 1) $out .= 'contractline_ref'; - if ($mode == 0) $out .= urlencode($ref); - $out .= ($mode ? '' : ''); - if (!empty($conf->global->PAYPAL_SECURITY_TOKEN)) - { - if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out .= '&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN; - else { - $out .= '&securekey='.($mode ? '' : ''); - if ($mode == 1) $out .= "hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + '".$type."' + contractline_ref)"; - if ($mode == 0) $out .= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN.$type.$ref, 2); - $out .= ($mode ? '' : ''); - } - } - } - if ($type == 'membersubscription') - { - $out = DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?source=membersubscription&ref='.($mode ? '' : ''); - if ($mode == 1) $out .= 'member_ref'; - if ($mode == 0) $out .= urlencode($ref); - $out .= ($mode ? '' : ''); - if (!empty($conf->global->PAYPAL_SECURITY_TOKEN)) - { - if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out .= '&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN; - else { - $out .= '&securekey='.($mode ? '' : ''); - if ($mode == 1) $out .= "hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + '".$type."' + member_ref)"; - if ($mode == 0) $out .= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN.$type.$ref, 2); - $out .= ($mode ? '' : ''); - } - } - } - - // For multicompany - $out .= "&entity=".$conf->entity; // Check the entity because He may be the same reference in several entities - - return $out; -} - - /** * Send redirect to paypal to browser * diff --git a/htdocs/stripe/lib/stripe.lib.php b/htdocs/stripe/lib/stripe.lib.php index bdd05fb47d9..f1d03ae39be 100644 --- a/htdocs/stripe/lib/stripe.lib.php +++ b/htdocs/stripe/lib/stripe.lib.php @@ -21,6 +21,8 @@ * \brief Library for common stripe functions */ +require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php'; + /** * Define head array for tabs of stripe tools setup pages * @@ -52,87 +54,6 @@ function stripeadmin_prepare_head() } - -/** - * Return string with full Url - * - * @param string $type Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...) - * @param string $ref Ref of object - * @return string Url string - */ -function showStripePaymentUrl($type, $ref) -{ - global $conf, $langs; - - $langs->load("paypal"); - $langs->load("paybox"); - $langs->load("stripe"); - - $servicename = 'Stripe'; - $out = '

'; - $out .= img_picto('', 'globe').' '.$langs->trans("ToOfferALinkForOnlinePayment", $servicename).'
'; - $url = getStripePaymentUrl(0, $type, $ref); - $out .= '
'; - $out .= ajax_autoselect("stripeurl", 0); - return $out; -} - -/** - * Return string with full Url - * - * @param int $mode 0=True url, 1=Url formated with colors - * @param string $type Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...) - * @param string $ref Ref of object - * @param int $amount Amount - * @param string $freetag Free tag - * @return string Url string - */ -function getStripePaymentUrl($mode, $type, $ref = '', $amount = '9.99', $freetag = 'your_tag') -{ - global $conf; - - $ref = str_replace(' ', '', $ref); - - if ($type == 'free') - { - $out = DOL_MAIN_URL_ROOT.'/public/stripe/newpayment.php?amount='.($mode ? '' : '').$amount.($mode ? '' : '').'&tag='.($mode ? '' : '').$freetag.($mode ? '' : ''); - } - if ($type == 'order') - { - $out = DOL_MAIN_URL_ROOT.'/public/stripe/newpayment.php?source=order&ref='.($mode ? '' : ''); - if ($mode == 1) $out .= 'order_ref'; - if ($mode == 0) $out .= urlencode($ref); - $out .= ($mode ? '' : ''); - } - if ($type == 'invoice') - { - $out = DOL_MAIN_URL_ROOT.'/public/stripe/newpayment.php?source=invoice&ref='.($mode ? '' : ''); - if ($mode == 1) $out .= 'invoice_ref'; - if ($mode == 0) $out .= urlencode($ref); - $out .= ($mode ? '' : ''); - } - if ($type == 'contractline') - { - $out = DOL_MAIN_URL_ROOT.'/public/stripe/newpayment.php?source=contractline&ref='.($mode ? '' : ''); - if ($mode == 1) $out .= 'contractline_ref'; - if ($mode == 0) $out .= urlencode($ref); - $out .= ($mode ? '' : ''); - } - if ($type == 'membersubscription') - { - $out = DOL_MAIN_URL_ROOT.'/public/stripe/newpayment.php?source=membersubscription&ref='.($mode ? '' : ''); - if ($mode == 1) $out .= 'member_ref'; - if ($mode == 0) $out .= urlencode($ref); - $out .= ($mode ? '' : ''); - } - - // For multicompany - $out .= "&entity=".$conf->entity; // Check the entity because He may be the same reference in several entities - - return $out; -} - - /** * Show footer of company in HTML pages * diff --git a/test/phpunit/AllTests.php b/test/phpunit/AllTests.php index 09c075c1083..0d195560d1c 100644 --- a/test/phpunit/AllTests.php +++ b/test/phpunit/AllTests.php @@ -258,6 +258,8 @@ class AllTests require_once dirname(__FILE__).'/PaypalTest.php'; $suite->addTestSuite('PaypalTest'); + require_once dirname(__FILE__).'/StripeTest.php'; + $suite->addTestSuite('StripeTest'); return $suite; } diff --git a/test/phpunit/PaypalTest.php b/test/phpunit/PaypalTest.php index 19774a11c09..7a2e31cbdad 100644 --- a/test/phpunit/PaypalTest.php +++ b/test/phpunit/PaypalTest.php @@ -131,7 +131,7 @@ class PaypalTest extends PHPUnit\Framework\TestCase } /** - * testProductCreate + * testPaypalOk * * @return void */ @@ -143,13 +143,13 @@ class PaypalTest extends PHPUnit\Framework\TestCase $langs=$this->savlangs; $db=$this->savdb; - $urltotest=getPaypalPaymentUrl(0, 'free'); + $urltotest=getOnlinePaymentUrl(0, 'free'); print "urltotest=".$urltotest."\n"; $result=getURLContent($urltotest, 'GET', '', 1, array(), array('http', 'https'), 2); - print __METHOD__." result=".$result."\n"; - $this->assertLessThanOrEqual($result, 0); + print __METHOD__." result=".$result['http_code']."\n"; + $this->assertEquals(200, $result['http_code']); return $result; } diff --git a/test/phpunit/StripeTest.php b/test/phpunit/StripeTest.php new file mode 100644 index 00000000000..72d4722b34e --- /dev/null +++ b/test/phpunit/StripeTest.php @@ -0,0 +1,156 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see https://www.gnu.org/ + */ + +/** + * \file test/phpunit/StripeTest.php + * \ingroup test + * \brief PHPUnit test + * \remarks To run this script as CLI: phpunit filename.php + */ + +global $conf,$user,$langs,$db; +//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver +//require_once 'PHPUnit/Autoload.php'; +require_once dirname(__FILE__).'/../../htdocs/master.inc.php'; +require_once dirname(__FILE__).'/../../htdocs/core/lib/geturl.lib.php'; +require_once dirname(__FILE__).'/../../htdocs/stripe/lib/stripe.lib.php'; + +if (empty($user->id)) +{ + print "Load permissions for admin user nb 1\n"; + $user->fetch(1); + $user->getrights(); +} +$conf->global->MAIN_DISABLE_ALL_MAILS=1; + + +/** + * Class for PHPUnit tests + * + * @backupGlobals disabled + * @backupStaticAttributes enabled + * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased. + */ +class StripeTest extends PHPUnit\Framework\TestCase +{ + protected $savconf; + protected $savuser; + protected $savlangs; + protected $savdb; + + /** + * Constructor + * We save global variables into local variables + * + * @return ProductTest + */ + public function __construct() + { + parent::__construct(); + + //$this->sharedFixture + global $conf,$user,$langs,$db; + $this->savconf=$conf; + $this->savuser=$user; + $this->savlangs=$langs; + $this->savdb=$db; + + print __METHOD__." db->type=".$db->type." user->id=".$user->id; + //print " - db ".$db->db; + print "\n"; + } + + /** + * setUpBeforeClass + * + * @return void + */ + public static function setUpBeforeClass() + { + global $conf,$user,$langs,$db; + + if (empty($conf->stripe->enabled)) { print __METHOD__." Module Stripe must be enabled.\n"; die(); } + + $db->begin(); // This is to have all actions inside a transaction even if test launched without suite. + + print __METHOD__."\n"; + } + + /** + * tearDownAfterClass + * + * @return void + */ + public static function tearDownAfterClass() + { + global $conf,$user,$langs,$db; + $db->rollback(); + + print __METHOD__."\n"; + } + + /** + * Init phpunit tests + * + * @return void + */ + protected function setUp() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + print __METHOD__."\n"; + } + + /** + * End phpunit tests + * + * @return void + */ + protected function tearDown() + { + print __METHOD__."\n"; + } + + /** + * testStripeOk + * + * @return void + */ + public function testStripeOk() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $urltotest=getOnlinePaymentUrl(0, 'free'); + print "urltotest=".$urltotest."\n"; + + $result=getURLContent($urltotest, 'GET', '', 1, array(), array('http', 'https'), 2); + + print __METHOD__." result=".$result['http_code']."\n"; + $this->assertEquals(200, $result['http_code']); + + return $result; + } +}