server_payment.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /* Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * The payment webservice was initially created by Nicolas Nunge <me@nikkow.eu>
  19. */
  20. /**
  21. * \file htdocs/webservices/server_payment.php
  22. * \brief File that is entry point to call Dolibarr WebServices
  23. */
  24. if (!defined('NOCSRFCHECK')) {
  25. define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
  26. }
  27. if (!defined('NOTOKENRENEWAL')) {
  28. define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
  29. }
  30. if (!defined('NOREQUIREMENU')) {
  31. define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
  32. }
  33. if (!defined('NOREQUIREHTML')) {
  34. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  35. }
  36. if (!defined('NOREQUIREAJAX')) {
  37. define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
  38. }
  39. if (!defined("NOLOGIN")) {
  40. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  41. }
  42. if (!defined("NOSESSION")) {
  43. define("NOSESSION", '1');
  44. }
  45. require '../main.inc.php';
  46. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  47. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  48. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  49. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  50. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  51. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  52. dol_syslog("Call Dolibarr webservices interfaces");
  53. $langs->load("main");
  54. // Enable and test if module web services is enabled
  55. if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
  56. $langs->load("admin");
  57. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  58. print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
  59. print $langs->trans("ToActivateModule");
  60. exit;
  61. }
  62. // Create the soap Object
  63. $server = new nusoap_server();
  64. $server->soap_defencoding = 'UTF-8';
  65. $server->decode_utf8 = false;
  66. $ns = 'http://www.dolibarr.org/ns/';
  67. $server->configureWSDL('WebServicesDolibarrPayment', $ns);
  68. $server->wsdl->schemaTargetNamespace = $ns;
  69. // Define WSDL Authentication object
  70. $server->wsdl->addComplexType(
  71. 'authentication',
  72. 'complexType',
  73. 'struct',
  74. 'all',
  75. '',
  76. array(
  77. 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
  78. 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
  79. 'login' => array('name'=>'login', 'type'=>'xsd:string'),
  80. 'password' => array('name'=>'password', 'type'=>'xsd:string'),
  81. 'entity' => array('name'=>'entity', 'type'=>'xsd:string')
  82. )
  83. );
  84. // Define WSDL Return object
  85. $server->wsdl->addComplexType(
  86. 'result',
  87. 'complexType',
  88. 'struct',
  89. 'all',
  90. '',
  91. array(
  92. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  93. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
  94. )
  95. );
  96. // Define WSDL for Payment object
  97. $server->wsdl->addComplexType(
  98. 'payment',
  99. 'complexType',
  100. 'struct',
  101. 'all',
  102. '',
  103. array(
  104. 'amount' => array('name'=>'amount', 'type'=>'xsd:double'),
  105. 'num_payment' => array('name'=>'num_payment', 'type'=>'xsd:string'),
  106. 'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
  107. 'bank_account' => array('name'=>'bank_account', 'type'=>'xsd:int'),
  108. 'payment_mode_id' => array('name'=>'payment_mode_id', 'type'=>'xsd:int'),
  109. 'invoice_id' => array('name'=>'invoice_id', 'type'=>'xsd:int'),
  110. 'int_label' => array('name'=>'int_label', 'type'=>'xsd:string'),
  111. 'emitter' => array('name'=>'emitter', 'type'=>'xsd:string'),
  112. 'bank_source' => array('name'=>'bank_source', 'type'=>'xsd:string'),
  113. )
  114. );
  115. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  116. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  117. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  118. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  119. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  120. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  121. // Register WSDL
  122. $server->register(
  123. 'createPayment',
  124. // Entry values
  125. array('authentication'=>'tns:authentication', 'payment'=>'tns:payment'),
  126. // Exit values
  127. array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
  128. $ns,
  129. $ns.'#createPayment',
  130. $styledoc,
  131. $styleuse,
  132. 'WS to create a new payment'
  133. );
  134. /**
  135. * Create a payment
  136. *
  137. * @param array $authentication Array of authentication information
  138. * @param Object $payment Payment
  139. * @return array Array result
  140. */
  141. function createPayment($authentication, $payment)
  142. {
  143. global $db, $conf;
  144. $now = dol_now();
  145. dol_syslog("Function: createPayment login=".$authentication['login']." id=".$payment->id.
  146. ", ref=".$payment->ref.", ref_ext=".$payment->ref_ext);
  147. if ($authentication['entity']) {
  148. $conf->entity = $authentication['entity'];
  149. }
  150. // Init and check authentication
  151. $objectresp = array();
  152. $errorcode = '';
  153. $errorlabel = '';
  154. $error = 0;
  155. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  156. // Check parameters
  157. if (empty($payment['amount']) && empty($payment['thirdparty_id'])) {
  158. $error++;
  159. $errorcode = 'KO';
  160. $errorlabel = "You must specify the amount and the third party's ID.";
  161. }
  162. if (!$error) {
  163. $soc = new Societe($db);
  164. $soc->fetch($payment['thirdparty_id']);
  165. $new_payment = new Paiement($db);
  166. $new_payment->amount = floatval($payment['amount']);
  167. $new_payment->num_payment = $payment['num_payment'];
  168. $new_payment->fk_account = intval($payment['bank_account']);
  169. $new_payment->paiementid = !empty($payment['payment_mode_id']) ? intval($payment['payment_mode_id']) : $soc->mode_reglement_id;
  170. $new_payment->datepaye = $now;
  171. $new_payment->author = $payment['thirdparty_id'];
  172. $new_payment->amounts = array();
  173. if (intval($payment['invoice_id']) > 0) {
  174. $new_payment->amounts[$payment['invoice_id']] = $new_payment->amount;
  175. }
  176. $db->begin();
  177. $result = $new_payment->create($fuser, true);
  178. if ($payment['bank_account']) {
  179. $new_payment->addPaymentToBank($fuser, 'payment', $payment['int_label'], $payment['bank_account'], $payment['emitter'], $payment['bank_source']);
  180. }
  181. if ($result < 0) {
  182. $error++;
  183. }
  184. if (!$error) {
  185. $db->commit();
  186. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$new_payment->id);
  187. } else {
  188. $db->rollback();
  189. $error++;
  190. $errorcode = 'KO';
  191. $errorlabel = $new_payment->error;
  192. dol_syslog("Function: createInvoice error while creating".$errorlabel);
  193. }
  194. }
  195. if ($error) {
  196. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  197. }
  198. return $objectresp;
  199. }
  200. // Return the results.
  201. $server->service(file_get_contents("php://input"));