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