server_payment.php 7.9 KB

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