dolibarr 24.0.0-beta
server_payment.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/*
21 * The payment webservice was initially created by Nicolas Nunge <me@nikkow.eu>
22 */
23
29if (!defined('NOCSRFCHECK')) {
30 define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
31}
32if (!defined('NOTOKENRENEWAL')) {
33 define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
34}
35if (!defined('NOREQUIREMENU')) {
36 define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
37}
38if (!defined('NOREQUIREHTML')) {
39 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
40}
41if (!defined('NOREQUIREAJAX')) {
42 define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
43}
44if (!defined("NOLOGIN")) {
45 define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
46}
47if (!defined("NOSESSION")) {
48 define("NOSESSION", '1');
49}
50
51require '../main.inc.php';
52require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
53require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
54require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
55require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
56
57require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
58require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
59
65dol_syslog("Call Dolibarr webservices interfaces");
66
67$langs->load("main");
68
69// Enable and test if module web services is enabled
70if (!getDolGlobalString('MAIN_MODULE_WEBSERVICES')) {
71 $langs->load("admin");
72
73 dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
74 print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
75 print $langs->trans("ToActivateModule");
76 exit;
77}
78
79// Create the soap Object
80$server = new nusoap_server();
81$server->soap_defencoding = 'UTF-8';
82$server->decode_utf8 = false;
83$ns = 'http://www.dolibarr.org/ns/';
84$server->configureWSDL('WebServicesDolibarrPayment', $ns);
85// @phan-suppress-next-line PhanUndeclaredProperty
86$server->wsdl->schemaTargetNamespace = $ns;
87
88
89// Define WSDL Authentication object
90$server->wsdl->addComplexType(
91 'authentication',
92 'complexType',
93 'struct',
94 'all',
95 '',
96 array(
97 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
98 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
99 'login' => array('name' => 'login', 'type' => 'xsd:string'),
100 'password' => array('name' => 'password', 'type' => 'xsd:string'),
101 'entity' => array('name' => 'entity', 'type' => 'xsd:string')
102 )
103);
104// Define WSDL Return object
105$server->wsdl->addComplexType(
106 'result',
107 'complexType',
108 'struct',
109 'all',
110 '',
111 array(
112 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
113 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
114 )
115);
116
117// Define WSDL for Payment object
118$server->wsdl->addComplexType(
119 'payment',
120 'complexType',
121 'struct',
122 'all',
123 '',
124 array(
125 'amount' => array('name' => 'amount', 'type' => 'xsd:double'),
126 'num_payment' => array('name' => 'num_payment', 'type' => 'xsd:string'),
127 'thirdparty_id' => array('name' => 'thirdparty_id', 'type' => 'xsd:int'),
128 'bank_account' => array('name' => 'bank_account', 'type' => 'xsd:int'),
129 'payment_mode_id' => array('name' => 'payment_mode_id', 'type' => 'xsd:int'),
130 'invoice_id' => array('name' => 'invoice_id', 'type' => 'xsd:int'),
131 'int_label' => array('name' => 'int_label', 'type' => 'xsd:string'),
132 'emitter' => array('name' => 'emitter', 'type' => 'xsd:string'),
133 'bank_source' => array('name' => 'bank_source', 'type' => 'xsd:string'),
134 )
135);
136
137// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
138// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
139// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
140$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
141$styleuse = 'encoded'; // encoded/literal/literal wrapped
142// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
143
144// Register WSDL
145$server->register(
146 'createPayment',
147 // Entry values
148 array('authentication' => 'tns:authentication', 'payment' => 'tns:payment'),
149 // Exit values
150 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string', 'ref_ext' => 'xsd:string'),
151 $ns,
152 $ns.'#createPayment',
153 $styledoc,
154 $styleuse,
155 'WS to create a new payment'
156);
157
158
166function createPayment($authentication, $payment)
167{
168 global $db, $conf;
169
170 $now = dol_now();
171
172 // TODO: Verify 'ref' and 'ref_ext', not defined in the WS Payment object.
173 dol_syslog("Function: createPayment login=".$authentication['login']." id=".$payment['id'].
174 ", ref=".($payment['ref'] ?? '').", ref_ext=".($payment['ref_ext'] ?? ''));
175
176 if ($authentication['entity']) {
177 $conf->entity = $authentication['entity'];
178 }
179
180 // Init and check authentication
181 $objectresp = array();
182 $errorcode = '';
183 $errorlabel = '';
184 $error = 0;
185 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
186
187 // Check parameters
188 if (empty($payment['amount']) && empty($payment['thirdparty_id'])) {
189 $error++;
190 $errorcode = 'KO';
191 $errorlabel = "You must specify the amount and the third party's ID.";
192 }
193
194 if (!$error) {
195 $soc = new Societe($db);
196 $soc->fetch($payment['thirdparty_id']);
197
198 $new_payment = new Paiement($db);
199 $new_payment->num_payment = $payment['num_payment'];
200 $new_payment->fk_account = intval($payment['bank_account']);
201 $new_payment->paiementid = !empty($payment['payment_mode_id']) ? intval($payment['payment_mode_id']) : $soc->mode_reglement_id;
202 $new_payment->datepaye = $now;
203 $new_payment->author = $payment['thirdparty_id'];
204 $new_payment->amounts = array($payment['invoice_id'] => (float) $payment['amount']);
205
206 $db->begin();
207 $result = $new_payment->create($fuser, 1);
208
209 if ($payment['bank_account']) {
210 $new_payment->addPaymentToBank($fuser, 'payment', $payment['int_label'], $payment['bank_account'], $payment['emitter'], $payment['bank_source']);
211 }
212
213 if ($result < 0) {
214 $error++;
215 }
216
217 if (!$error) {
218 $db->commit();
219 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $new_payment->id);
220 } else {
221 $db->rollback();
222 $error++;
223 $errorcode = 'KO';
224 $errorlabel = $new_payment->error;
225 dol_syslog("Function: createInvoice error while creating".$errorlabel);
226 }
227 }
228
229 if ($error) {
230 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
231 }
232
233 return $objectresp;
234}
235
236// Return the results.
237$server->service(file_get_contents("php://input"));
Class to manage payments of customer invoices.
Class to manage third parties objects (customers, suppliers, prospects...)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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:37