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