server_payment.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // This is to make Dolibarr working with Plesk
  25. set_include_path($_SERVER['DOCUMENT_ROOT'].'/htdocs');
  26. require '../master.inc.php';
  27. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  33. dol_syslog("Call Dolibarr webservices interfaces");
  34. $langs->load("main");
  35. // Enable and test if module web services is enabled
  36. if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
  37. $langs->load("admin");
  38. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  39. print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
  40. print $langs->trans("ToActivateModule");
  41. exit;
  42. }
  43. // Create the soap Object
  44. $server = new nusoap_server();
  45. $server->soap_defencoding = 'UTF-8';
  46. $server->decode_utf8 = false;
  47. $ns = 'http://www.dolibarr.org/ns/';
  48. $server->configureWSDL('WebServicesDolibarrPayment', $ns);
  49. $server->wsdl->schemaTargetNamespace = $ns;
  50. // Define WSDL Authentication object
  51. $server->wsdl->addComplexType(
  52. 'authentication',
  53. 'complexType',
  54. 'struct',
  55. 'all',
  56. '',
  57. array(
  58. 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
  59. 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
  60. 'login' => array('name'=>'login', 'type'=>'xsd:string'),
  61. 'password' => array('name'=>'password', 'type'=>'xsd:string'),
  62. 'entity' => array('name'=>'entity', 'type'=>'xsd:string')
  63. )
  64. );
  65. // Define WSDL Return object
  66. $server->wsdl->addComplexType(
  67. 'result',
  68. 'complexType',
  69. 'struct',
  70. 'all',
  71. '',
  72. array(
  73. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  74. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
  75. )
  76. );
  77. // Define WSDL for Payment object
  78. $server->wsdl->addComplexType(
  79. 'payment',
  80. 'complexType',
  81. 'struct',
  82. 'all',
  83. '',
  84. array(
  85. 'amount' => array('name'=>'amount', 'type'=>'xsd:double'),
  86. 'num_payment' => array('name'=>'num_payment', 'type'=>'xsd:string'),
  87. 'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
  88. 'bank_account' => array('name'=>'bank_account', 'type'=>'xsd:int'),
  89. 'payment_mode_id' => array('name'=>'payment_mode_id', 'type'=>'xsd:int'),
  90. 'invoice_id' => array('name'=>'invoice_id', 'type'=>'xsd:int'),
  91. 'int_label' => array('name'=>'int_label', 'type'=>'xsd:string'),
  92. 'emitter' => array('name'=>'emitter', 'type'=>'xsd:string'),
  93. 'bank_source' => array('name'=>'bank_source', 'type'=>'xsd:string'),
  94. )
  95. );
  96. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  97. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  98. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  99. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  100. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  101. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  102. // Register WSDL
  103. $server->register(
  104. 'createPayment',
  105. // Entry values
  106. array('authentication'=>'tns:authentication', 'payment'=>'tns:payment'),
  107. // Exit values
  108. array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
  109. $ns,
  110. $ns.'#createPayment',
  111. $styledoc,
  112. $styleuse,
  113. 'WS to create a new payment'
  114. );
  115. /**
  116. * Create a payment
  117. *
  118. * @param array $authentication Array of authentication information
  119. * @param Object $payment Payment
  120. * @return array Array result
  121. */
  122. function createPayment($authentication, $payment)
  123. {
  124. global $db, $conf;
  125. $now = dol_now();
  126. dol_syslog("Function: createPayment login=".$authentication['login']." id=".$payment->id.
  127. ", ref=".$payment->ref.", ref_ext=".$payment->ref_ext);
  128. if ($authentication['entity']) {
  129. $conf->entity = $authentication['entity'];
  130. }
  131. // Init and check authentication
  132. $objectresp = array();
  133. $errorcode = '';
  134. $errorlabel = '';
  135. $error = 0;
  136. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  137. // Check parameters
  138. if (empty($payment['amount']) && empty($payment['thirdparty_id'])) {
  139. $error++;
  140. $errorcode = 'KO';
  141. $errorlabel = "You must specify the amount and the third party's ID.";
  142. }
  143. if (!$error) {
  144. $soc = new Societe($db);
  145. $soc->fetch($payment['thirdparty_id']);
  146. $new_payment = new Paiement($db);
  147. $new_payment->amount = floatval($payment['amount']);
  148. $new_payment->num_payment = $payment['num_payment'];
  149. $new_payment->fk_account = intval($payment['bank_account']);
  150. $new_payment->paiementid = !empty($payment['payment_mode_id']) ? intval($payment['payment_mode_id']) : $soc->mode_reglement_id;
  151. $new_payment->datepaye = $now;
  152. $new_payment->author = $payment['thirdparty_id'];
  153. $new_payment->amounts = array();
  154. if (intval($payment['invoice_id']) > 0) {
  155. $new_payment->amounts[$payment['invoice_id']] = $new_payment->amount;
  156. }
  157. $db->begin();
  158. $result = $new_payment->create($fuser, true);
  159. if ($payment['bank_account']) {
  160. $new_payment->addPaymentToBank($fuser, 'payment', $payment['int_label'], $payment['bank_account'], $payment['emitter'], $payment['bank_source']);
  161. }
  162. if ($result < 0) {
  163. $error++;
  164. }
  165. if (!$error) {
  166. $db->commit();
  167. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$new_payment->id);
  168. } else {
  169. $db->rollback();
  170. $error++;
  171. $errorcode = 'KO';
  172. $errorlabel = $new_payment->error;
  173. dol_syslog("Function: createInvoice error while creating".$errorlabel);
  174. }
  175. }
  176. if ($error) {
  177. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  178. }
  179. return $objectresp;
  180. }
  181. // Return the results.
  182. $server->service(file_get_contents("php://input"));