payments.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
  4. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  5. * Copyright (C) 2020 Abbes Bahfir <bafbes@gmail.com>
  6. * Copyright (C) 2021 Waël Almoman <info@almoman.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. * or see https://www.gnu.org/
  21. */
  22. /**
  23. * Returns an array with the tabs for the "Payment" section
  24. * It loads tabs from modules looking for the entity payment
  25. *
  26. * @param Paiement $object Current payment object
  27. * @return array Tabs for the payment section
  28. */
  29. function payment_prepare_head(Paiement $object)
  30. {
  31. global $langs, $conf;
  32. $h = 0;
  33. $head = array();
  34. $head[$h][0] = DOL_URL_ROOT.'/compta/paiement/card.php?id='.$object->id;
  35. $head[$h][1] = $langs->trans("Payment");
  36. $head[$h][2] = 'payment';
  37. $h++;
  38. // Show more tabs from modules
  39. // Entries must be declared in modules descriptor with line
  40. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  41. // $this->tabs = array('entity:-tabname); to remove a tab
  42. complete_head_from_modules($conf, $langs, $object, $head, $h, 'payment');
  43. $head[$h][0] = DOL_URL_ROOT.'/compta/paiement/info.php?id='.$object->id;
  44. $head[$h][1] = $langs->trans("Info");
  45. $head[$h][2] = 'info';
  46. $h++;
  47. complete_head_from_modules($conf, $langs, $object, $head, $h, 'payment', 'remove');
  48. return $head;
  49. }
  50. /**
  51. * Returns an array with the tabs for the "Bannkline" section
  52. * It loads tabs from modules looking for the entity payment
  53. *
  54. * @param int $id ID of bank line
  55. * @return array Tabs for the Bankline section
  56. */
  57. function bankline_prepare_head($id)
  58. {
  59. global $langs, $conf;
  60. $h = 0;
  61. $head = array();
  62. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/line.php?rowid='.$id;
  63. $head[$h][1] = $langs->trans('BankTransaction');
  64. $head[$h][2] = 'bankline';
  65. $h++;
  66. // Show more tabs from modules
  67. // Entries must be declared in modules descriptor with line
  68. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  69. // $this->tabs = array('entity:-tabname); to remove a tab
  70. complete_head_from_modules($conf, $langs, null, $head, $h, 'bankline');
  71. $head[$h][0] = DOL_URL_ROOT.'/compta/bank/info.php?rowid='.$id;
  72. $head[$h][1] = $langs->trans("Info");
  73. $head[$h][2] = 'info';
  74. $h++;
  75. complete_head_from_modules($conf, $langs, null, $head, $h, 'bankline', 'remove');
  76. return $head;
  77. }
  78. /**
  79. * Returns an array with the tabs for the "Supplier payment" section
  80. * It loads tabs from modules looking for the entity payment_supplier
  81. *
  82. * @param Paiement $object Current payment object
  83. * @return array Tabs for the payment section
  84. */
  85. function payment_supplier_prepare_head(Paiement $object)
  86. {
  87. global $db, $langs, $conf;
  88. $h = 0;
  89. $head = array();
  90. $head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/card.php?id='.$object->id;
  91. $head[$h][1] = $langs->trans("Payment");
  92. $head[$h][2] = 'payment';
  93. $h++;
  94. // Show more tabs from modules
  95. // Entries must be declared in modules descriptor with line
  96. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  97. // $this->tabs = array('entity:-tabname); to remove a tab
  98. complete_head_from_modules($conf, $langs, $object, $head, $h, 'payment_supplier');
  99. $head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/info.php?id='.$object->id;
  100. $head[$h][1] = $langs->trans('Info');
  101. $head[$h][2] = 'info';
  102. $h++;
  103. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  104. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  105. $upload_dir = $conf->fournisseur->payment->dir_output.'/'.$object->ref;
  106. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  107. $nbLinks = Link::count($db, $object->element, $object->id);
  108. $head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/document.php?id='.$object->id;
  109. $head[$h][1] = $langs->trans('Documents');
  110. if (($nbFiles + $nbLinks) > 0) {
  111. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  112. }
  113. $head[$h][2] = 'documents';
  114. $h++;
  115. complete_head_from_modules($conf, $langs, $object, $head, $h, 'payment_supplier', 'remove');
  116. return $head;
  117. }
  118. /**
  119. * Return array of valid payment mode
  120. *
  121. * @param string $paymentmethod Filter on this payment method (''=none, 'paypal', ...)
  122. * @return array Array of valid payment method
  123. */
  124. function getValidOnlinePaymentMethods($paymentmethod = '')
  125. {
  126. global $langs, $hookmanager, $action;
  127. $validpaymentmethod = array();
  128. if ((empty($paymentmethod) || $paymentmethod == 'paypal') && isModEnabled('paypal')) {
  129. $langs->load("paypal");
  130. $validpaymentmethod['paypal'] = 'valid';
  131. }
  132. if ((empty($paymentmethod) || $paymentmethod == 'paybox') && isModEnabled('paybox')) {
  133. $langs->load("paybox");
  134. $validpaymentmethod['paybox'] = 'valid';
  135. }
  136. if ((empty($paymentmethod) || $paymentmethod == 'stripe') && isModEnabled('stripe')) {
  137. $langs->load("stripe");
  138. $validpaymentmethod['stripe'] = 'valid';
  139. }
  140. // This hook is used to complete the $validpaymentmethod array so an external payment modules
  141. // can add its own key (ie 'payzen' for Payzen, ...)
  142. $parameters = [
  143. 'paymentmethod' => $paymentmethod,
  144. 'validpaymentmethod' => &$validpaymentmethod
  145. ];
  146. $tmpobject = new stdClass();
  147. $reshook = $hookmanager->executeHooks('getValidPayment', $parameters, $tmpobject, $action);
  148. if ($reshook < 0) {
  149. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  150. } elseif (!empty($hookmanager->resArray['validpaymentmethod'])) {
  151. if ($reshook == 0) {
  152. $validpaymentmethod = array_merge($validpaymentmethod, $hookmanager->resArray['validpaymentmethod']);
  153. } else {
  154. $validpaymentmethod = $hookmanager->resArray['validpaymentmethod'];
  155. }
  156. }
  157. return $validpaymentmethod;
  158. }
  159. /**
  160. * Return string with full online payment Url
  161. *
  162. * @param string $type Type of URL ('free', 'order', 'invoice', 'contractline', 'member' ...)
  163. * @param string $ref Ref of object
  164. * @param int|float $amount Amount of money to request for
  165. * @return string Url string
  166. */
  167. function showOnlinePaymentUrl($type, $ref, $amount = 0)
  168. {
  169. global $langs;
  170. // Load translation files required by the page
  171. $langs->loadLangs(array('payment', 'stripe'));
  172. $servicename = ''; // Link is a generic link for all payments services (paypal, stripe, ...)
  173. $out = img_picto('', 'globe').' <span class="opacitymedium">'.$langs->trans("ToOfferALinkForOnlinePayment", $servicename).'</span><br>';
  174. $url = getOnlinePaymentUrl(0, $type, $ref, $amount);
  175. $out .= '<div class="urllink"><input type="text" id="onlinepaymenturl" class="quatrevingtpercentminusx" value="'.$url.'">';
  176. $out .= '<a class="" href="'.$url.'" target="_blank" rel="noopener noreferrer">'.img_picto('', 'globe', 'class="paddingleft"').'</a>';
  177. $out .= '</div>';
  178. $out .= ajax_autoselect("onlinepaymenturl", 0);
  179. return $out;
  180. }
  181. /**
  182. * Return string with HTML link for online payment
  183. *
  184. * @param string $type Type of URL ('free', 'order', 'invoice', 'contractline', 'member' ...)
  185. * @param string $ref Ref of object
  186. * @param string $label Text or HTML tag to display, if empty it display the URL
  187. * @param int|float $amount Amount of money to request for
  188. * @return string Url string
  189. */
  190. function getHtmlOnlinePaymentLink($type, $ref, $label = '', $amount = 0)
  191. {
  192. $url = getOnlinePaymentUrl(0, $type, $ref, $amount);
  193. $label = $label ? $label : $url;
  194. return '<a href="'.$url.'" target="_blank" rel="noopener noreferrer">'.$label.'</a>';
  195. }
  196. /**
  197. * Return string with full Url
  198. *
  199. * @param int $mode 0=True url, 1=Url formated with colors
  200. * @param string $type Type of URL ('free', 'order', 'invoice', 'contractline', 'member', 'boothlocation', ...)
  201. * @param string $ref Ref of object
  202. * @param int|float $amount Amount of money to request for
  203. * @param string $freetag Free tag (required and used for $type='free' only)
  204. * @param string $localorexternal 0=Url for browser, 1=Url for external access
  205. * @return string Url string
  206. */
  207. function getOnlinePaymentUrl($mode, $type, $ref = '', $amount = 0, $freetag = 'your_tag', $localorexternal = 1)
  208. {
  209. global $conf, $dolibarr_main_url_root;
  210. $ref = str_replace(' ', '', $ref);
  211. $out = '';
  212. // Define $urlwithroot
  213. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  214. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  215. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  216. $urltouse = DOL_MAIN_URL_ROOT; // Should be "https://www.mydomain.com/mydolibarr" for example
  217. //dol_syslog("getOnlinePaymentUrl DOL_MAIN_URL_ROOT=".DOL_MAIN_URL_ROOT);
  218. if ($localorexternal) {
  219. $urltouse = $urlwithroot;
  220. }
  221. if ($type == 'free') {
  222. $out = $urltouse.'/public/payment/newpayment.php?amount='.($mode ? '<span style="color: #666666">' : '').$amount.($mode ? '</span>' : '').'&tag='.($mode ? '<span style="color: #666666">' : '').$freetag.($mode ? '</span>' : '');
  223. if (getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
  224. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  225. $out .= '&securekey='.urlencode($conf->global->PAYMENT_SECURITY_TOKEN);
  226. } else {
  227. $out .= '&securekey='.urlencode(dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN'), 2));
  228. }
  229. }
  230. //if ($mode) $out.='&noidempotency=1';
  231. } elseif ($type == 'order') {
  232. $out = $urltouse.'/public/payment/newpayment.php?source='.$type.'&ref='.($mode ? '<span style="color: #666666">' : '');
  233. if ($mode == 1) {
  234. $out .= 'order_ref';
  235. }
  236. if ($mode == 0) {
  237. $out .= urlencode($ref);
  238. }
  239. $out .= ($mode ? '</span>' : '');
  240. if (getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
  241. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  242. $out .= '&securekey='.urlencode($conf->global->PAYMENT_SECURITY_TOKEN);
  243. } else {
  244. $out .= '&securekey='.($mode ? '<span style="color: #666666">' : '');
  245. if ($mode == 1) {
  246. $out .= "hash('" . getDolGlobalString('PAYMENT_SECURITY_TOKEN')."' + '".$type."' + order_ref)";
  247. }
  248. if ($mode == 0) {
  249. $out .= dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . $type.$ref, 2);
  250. }
  251. $out .= ($mode ? '</span>' : '');
  252. }
  253. }
  254. } elseif ($type == 'invoice') {
  255. $out = $urltouse.'/public/payment/newpayment.php?source='.$type.'&ref='.($mode ? '<span style="color: #666666">' : '');
  256. if ($mode == 1) {
  257. $out .= 'invoice_ref';
  258. }
  259. if ($mode == 0) {
  260. $out .= urlencode($ref);
  261. }
  262. $out .= ($mode ? '</span>' : '');
  263. if (getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
  264. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  265. $out .= '&securekey='.urlencode($conf->global->PAYMENT_SECURITY_TOKEN);
  266. } else {
  267. $out .= '&securekey='.($mode ? '<span style="color: #666666">' : '');
  268. if ($mode == 1) {
  269. $out .= "hash('" . getDolGlobalString('PAYMENT_SECURITY_TOKEN')."' + '".$type."' + invoice_ref)";
  270. }
  271. if ($mode == 0) {
  272. $out .= dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . $type.$ref, 2);
  273. }
  274. $out .= ($mode ? '</span>' : '');
  275. }
  276. }
  277. } elseif ($type == 'contractline') {
  278. $out = $urltouse.'/public/payment/newpayment.php?source='.$type.'&ref='.($mode ? '<span style="color: #666666">' : '');
  279. if ($mode == 1) {
  280. $out .= 'contractline_ref';
  281. }
  282. if ($mode == 0) {
  283. $out .= urlencode($ref);
  284. }
  285. $out .= ($mode ? '</span>' : '');
  286. if (getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
  287. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  288. $out .= '&securekey='.urlencode($conf->global->PAYMENT_SECURITY_TOKEN);
  289. } else {
  290. $out .= '&securekey='.($mode ? '<span style="color: #666666">' : '');
  291. if ($mode == 1) {
  292. $out .= "hash('" . getDolGlobalString('PAYMENT_SECURITY_TOKEN')."' + '".$type."' + contractline_ref)";
  293. }
  294. if ($mode == 0) {
  295. $out .= dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . $type.$ref, 2);
  296. }
  297. $out .= ($mode ? '</span>' : '');
  298. }
  299. }
  300. } elseif ($type == 'member' || $type == 'membersubscription') {
  301. $newtype = 'member';
  302. $out = $urltouse.'/public/payment/newpayment.php?source=member';
  303. $out .= '&amount='.$amount;
  304. $out .= '&ref='.($mode ? '<span style="color: #666666">' : '');
  305. if ($mode == 1) {
  306. $out .= 'member_ref';
  307. }
  308. if ($mode == 0) {
  309. $out .= urlencode($ref);
  310. }
  311. $out .= ($mode ? '</span>' : '');
  312. if (getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
  313. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  314. $out .= '&securekey='.urlencode($conf->global->PAYMENT_SECURITY_TOKEN);
  315. } else {
  316. $out .= '&securekey='.($mode ? '<span style="color: #666666">' : '');
  317. if ($mode == 1) {
  318. $out .= "hash('" . getDolGlobalString('PAYMENT_SECURITY_TOKEN')."' + '".$newtype."' + member_ref)";
  319. }
  320. if ($mode == 0) {
  321. $out .= dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . $newtype.$ref, 2);
  322. }
  323. $out .= ($mode ? '</span>' : '');
  324. }
  325. }
  326. } elseif ($type == 'donation') {
  327. $out = $urltouse.'/public/payment/newpayment.php?source='.$type.'&ref='.($mode ? '<span style="color: #666666">' : '');
  328. if ($mode == 1) {
  329. $out .= 'donation_ref';
  330. }
  331. if ($mode == 0) {
  332. $out .= urlencode($ref);
  333. }
  334. $out .= ($mode ? '</span>' : '');
  335. if (getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
  336. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  337. $out .= '&securekey='.urlencode($conf->global->PAYMENT_SECURITY_TOKEN);
  338. } else {
  339. $out .= '&securekey='.($mode ? '<span style="color: #666666">' : '');
  340. if ($mode == 1) {
  341. $out .= "hash('" . getDolGlobalString('PAYMENT_SECURITY_TOKEN')."' + '".$type."' + donation_ref)";
  342. }
  343. if ($mode == 0) {
  344. $out .= dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . $type.$ref, 2);
  345. }
  346. $out .= ($mode ? '</span>' : '');
  347. }
  348. }
  349. } elseif ($type == 'boothlocation') {
  350. $out = $urltouse.'/public/payment/newpayment.php?source='.$type.'&ref='.($mode ? '<span style="color: #666666">' : '');
  351. if ($mode == 1) {
  352. $out .= 'invoice_ref';
  353. }
  354. if ($mode == 0) {
  355. $out .= urlencode($ref);
  356. }
  357. $out .= ($mode ? '</span>' : '');
  358. if (getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
  359. if (!getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
  360. $out .= '&securekey='.urlencode($conf->global->PAYMENT_SECURITY_TOKEN);
  361. } else {
  362. $out .= '&securekey='.($mode ? '<span style="color: #666666">' : '');
  363. if ($mode == 1) {
  364. $out .= "hash('" . getDolGlobalString('PAYMENT_SECURITY_TOKEN')."' + '".$type."' + invoice_ref)";
  365. }
  366. if ($mode == 0) {
  367. $out .= dol_hash(getDolGlobalString('PAYMENT_SECURITY_TOKEN') . $type.$ref, 2);
  368. }
  369. $out .= ($mode ? '</span>' : '');
  370. }
  371. }
  372. }
  373. // For multicompany
  374. if (!empty($out) && isModEnabled('multicompany')) {
  375. $out .= "&entity=".$conf->entity; // Check the entity because we may have the same reference in several entities
  376. }
  377. return $out;
  378. }