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