dolibarr  19.0.0-dev
server_payment.php
Go to the documentation of this file.
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 /*
19  * The payment webservice was initially created by Nicolas Nunge <me@nikkow.eu>
20  */
21 
27 if (!defined('NOCSRFCHECK')) {
28  define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
29 }
30 if (!defined('NOTOKENRENEWAL')) {
31  define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
32 }
33 if (!defined('NOREQUIREMENU')) {
34  define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
35 }
36 if (!defined('NOREQUIREHTML')) {
37  define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
38 }
39 if (!defined('NOREQUIREAJAX')) {
40  define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
41 }
42 if (!defined("NOLOGIN")) {
43  define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
44 }
45 if (!defined("NOSESSION")) {
46  define("NOSESSION", '1');
47 }
48 
49 require '../main.inc.php';
50 require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
51 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
52 require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
53 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
54 
55 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
56 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
57 
58 
59 dol_syslog("Call Dolibarr webservices interfaces");
60 
61 $langs->load("main");
62 
63 // Enable and test if module web services is enabled
64 if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
65  $langs->load("admin");
66 
67  dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
68  print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
69  print $langs->trans("ToActivateModule");
70  exit;
71 }
72 
73 // Create the soap Object
74 $server = new nusoap_server();
75 $server->soap_defencoding = 'UTF-8';
76 $server->decode_utf8 = false;
77 $ns = 'http://www.dolibarr.org/ns/';
78 $server->configureWSDL('WebServicesDolibarrPayment', $ns);
79 $server->wsdl->schemaTargetNamespace = $ns;
80 
81 
82 // Define WSDL Authentication object
83 $server->wsdl->addComplexType(
84  'authentication',
85  'complexType',
86  'struct',
87  'all',
88  '',
89  array(
90  'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
91  'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
92  'login' => array('name'=>'login', 'type'=>'xsd:string'),
93  'password' => array('name'=>'password', 'type'=>'xsd:string'),
94  'entity' => array('name'=>'entity', 'type'=>'xsd:string')
95  )
96 );
97 // Define WSDL Return object
98 $server->wsdl->addComplexType(
99  'result',
100  'complexType',
101  'struct',
102  'all',
103  '',
104  array(
105  'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
106  'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
107  )
108 );
109 
110 // Define WSDL for Payment object
111 $server->wsdl->addComplexType(
112  'payment',
113  'complexType',
114  'struct',
115  'all',
116  '',
117  array(
118  'amount' => array('name'=>'amount', 'type'=>'xsd:double'),
119  'num_payment' => array('name'=>'num_payment', 'type'=>'xsd:string'),
120  'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
121  'bank_account' => array('name'=>'bank_account', 'type'=>'xsd:int'),
122  'payment_mode_id' => array('name'=>'payment_mode_id', 'type'=>'xsd:int'),
123  'invoice_id' => array('name'=>'invoice_id', 'type'=>'xsd:int'),
124  'int_label' => array('name'=>'int_label', 'type'=>'xsd:string'),
125  'emitter' => array('name'=>'emitter', 'type'=>'xsd:string'),
126  'bank_source' => array('name'=>'bank_source', 'type'=>'xsd:string'),
127  )
128 );
129 
130 // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
131 // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
132 // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
133 $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
134 $styleuse = 'encoded'; // encoded/literal/literal wrapped
135 // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
136 
137 // Register WSDL
138 $server->register(
139  'createPayment',
140  // Entry values
141  array('authentication'=>'tns:authentication', 'payment'=>'tns:payment'),
142  // Exit values
143  array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
144  $ns,
145  $ns.'#createPayment',
146  $styledoc,
147  $styleuse,
148  'WS to create a new payment'
149 );
150 
151 
159 function createPayment($authentication, $payment)
160 {
161  global $db, $conf;
162 
163  $now = dol_now();
164 
165  dol_syslog("Function: createPayment login=".$authentication['login']." id=".$payment->id.
166  ", ref=".$payment->ref.", ref_ext=".$payment->ref_ext);
167 
168  if ($authentication['entity']) {
169  $conf->entity = $authentication['entity'];
170  }
171 
172  // Init and check authentication
173  $objectresp = array();
174  $errorcode = '';
175  $errorlabel = '';
176  $error = 0;
177  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
178 
179  // Check parameters
180  if (empty($payment['amount']) && empty($payment['thirdparty_id'])) {
181  $error++;
182  $errorcode = 'KO';
183  $errorlabel = "You must specify the amount and the third party's ID.";
184  }
185 
186  if (!$error) {
187  $soc = new Societe($db);
188  $soc->fetch($payment['thirdparty_id']);
189 
190  $new_payment = new Paiement($db);
191  $new_payment->amount = floatval($payment['amount']);
192  $new_payment->num_payment = $payment['num_payment'];
193  $new_payment->fk_account = intval($payment['bank_account']);
194  $new_payment->paiementid = !empty($payment['payment_mode_id']) ? intval($payment['payment_mode_id']) : $soc->mode_reglement_id;
195  $new_payment->datepaye = $now;
196  $new_payment->author = $payment['thirdparty_id'];
197  $new_payment->amounts = array();
198 
199  if (intval($payment['invoice_id']) > 0) {
200  $new_payment->amounts[$payment['invoice_id']] = $new_payment->amount;
201  }
202 
203  $db->begin();
204  $result = $new_payment->create($fuser, true);
205 
206  if ($payment['bank_account']) {
207  $new_payment->addPaymentToBank($fuser, 'payment', $payment['int_label'], $payment['bank_account'], $payment['emitter'], $payment['bank_source']);
208  }
209 
210  if ($result < 0) {
211  $error++;
212  }
213 
214  if (!$error) {
215  $db->commit();
216  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$new_payment->id);
217  } else {
218  $db->rollback();
219  $error++;
220  $errorcode = 'KO';
221  $errorlabel = $new_payment->error;
222  dol_syslog("Function: createInvoice error while creating".$errorlabel);
223  }
224  }
225 
226  if ($error) {
227  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
228  }
229 
230  return $objectresp;
231 }
232 
233 // Return the results.
234 $server->service(file_get_contents("php://input"));
Class to manage payments of customer invoices.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
createPayment($authentication, $payment)
Create a payment.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition: ws.lib.php:35