Completed
Branch develop (a2b570)
by
unknown
43:44
created

payments.lib.php ➔ getValidOnlinePaymentMethods()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 8
nop 1
dl 0
loc 20
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright (C) 2013	Marcos García	<[email protected]>
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 <http://www.gnu.org/licenses/>.
17
 * or see http://www.gnu.org/
18
 */
19
20
/**
21
 * Returns an array with the tabs for the "Payment" section
22
 * It loads tabs from modules looking for the entity payment
23
 *
24
 * @param Paiement $object Current payment object
25
 * @return array Tabs for the payment section
26
 */
27
function payment_prepare_head(Paiement $object) {
28
29
	global $langs, $conf;
30
31
	$h = 0;
32
	$head = array();
33
34
	$head[$h][0] = DOL_URL_ROOT.'/compta/paiement/card.php?id='.$object->id;
35
	$head[$h][1] = $langs->trans("Card");
36
	$head[$h][2] = 'payment';
37
	$h++;
38
39
    // Show more tabs from modules
40
    // Entries must be declared in modules descriptor with line
41
    // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
42
    // $this->tabs = array('entity:-tabname);   												to remove a tab
43
    complete_head_from_modules($conf,$langs,$object,$head,$h,'payment');
44
45
	$head[$h][0] = DOL_URL_ROOT.'/compta/paiement/info.php?id='.$object->id;
46
	$head[$h][1] = $langs->trans("Info");
47
	$head[$h][2] = 'info';
48
	$h++;
49
50
	complete_head_from_modules($conf,$langs,$object,$head,$h,'payment', 'remove');
51
52
	return $head;
53
}
54
55
/**
56
 * Returns an array with the tabs for the "Supplier payment" section
57
 * It loads tabs from modules looking for the entity payment_supplier
58
 *
59
 * @param Paiement $object Current payment object
60
 * @return array Tabs for the payment section
61
 */
62
function payment_supplier_prepare_head(Paiement $object) {
63
64
	global $langs, $conf;
65
66
	$h = 0;
67
	$head = array();
68
69
	$head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/card.php?id='.$object->id;
70
	$head[$h][1] = $langs->trans("Card");
71
	$head[$h][2] = 'payment';
72
	$h++;
73
74
    // Show more tabs from modules
75
    // Entries must be declared in modules descriptor with line
76
    // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__');   to add new tab
77
    // $this->tabs = array('entity:-tabname);   												to remove a tab
78
    complete_head_from_modules($conf,$langs,$object,$head,$h,'payment_supplier');
79
80
	$head[$h][0] = DOL_URL_ROOT.'/fourn/paiement/info.php?id='.$object->id;
81
	$head[$h][1] = $langs->trans('Info');
82
	$head[$h][2] = 'info';
83
	$h++;
84
85
	complete_head_from_modules($conf,$langs,$object,$head,$h,'payment_supplier', 'remove');
86
87
	return $head;
88
}
89
90
/**
91
 * Return array of valid payment mode
92
 *
93
 * @param	string	$paymentmethod		Filter on this payment method
94
 * @return	array						Array of valid payment method
95
 */
96
function getValidOnlinePaymentMethods($paymentmethod='')
97
{
98
	global $conf;
99
100
	$validpaymentmethod=array();
101
102
	if ((empty($paymentmethod) || $paymentmethod == 'paypal') && ! empty($conf->paypal->enabled))
103
	{
104
		$validpaymentmethod['paypal']='valid';
105
	}
106
	if ((empty($paymentmethod) || $paymentmethod == 'paybox') && ! empty($conf->paybox->enabled))
107
	{
108
		$validpaymentmethod['paybox']='valid';
109
	}
110
	if ((empty($paymentmethod) || $paymentmethod == 'stripe') && ! empty($conf->stripe->enabled))
111
	{
112
		$validpaymentmethod['stripe']='valid';
113
	}
114
	return $validpaymentmethod;
115
}
116
117
/**
118
 * Return string with full Url
119
 *
120
 * @param   string	$type		Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...)
121
 * @param	string	$ref		Ref of object
122
 * @return	string				Url string
123
 */
124
function showOnlinePaymentUrl($type,$ref)
125
{
126
	global $conf, $langs;
127
128
	$langs->load("payment");
129
	$langs->load("paybox");
130
	$servicename='Online';
131
132
	$out = img_picto('','object_globe.png').' '.$langs->trans("ToOfferALinkForOnlinePayment",$servicename).'<br>';
133
	$url = getOnlinePaymentUrl(0,$type,$ref);
134
	$out.= '<input type="text" id="onlinepaymenturl" class="quatrevingtpercent" value="'.$url.'">';
135
	$out.= ajax_autoselect("onlinepaymenturl", 0);
136
	return $out;
137
}
138
139
/**
140
 * Return string with full Url
141
 *
142
 * @param   int		$mode		0=True url, 1=Url formated with colors
143
 * @param   string	$type		Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...)
144
 * @param	string	$ref		Ref of object
145
 * @param	int		$amount		Amount (required for $type='free' only)
146
 * @param	string	$freetag	Free tag
147
 * @return	string				Url string
148
 */
149
function getOnlinePaymentUrl($mode, $type, $ref='', $amount='9.99', $freetag='your_free_tag')
150
{
151
	global $conf;
152
153
	$ref=str_replace(' ','',$ref);
154
	$out='';
155
156
	if ($type == 'free')
157
	{
158
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?amount='.($mode?'<font color="#666666">':'').$amount.($mode?'</font>':'').'&tag='.($mode?'<font color="#666666">':'').$freetag.($mode?'</font>':'');
159
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
160
		{
161
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
162
			else $out.='&securekey='.dol_hash($conf->global->PAYMENT_SECURITY_TOKEN, 2);
163
		}
164
	}
165
	if ($type == 'order')
166
	{
167
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?source=order&ref='.($mode?'<font color="#666666">':'');
168
		if ($mode == 1) $out.='order_ref';
169
		if ($mode == 0) $out.=urlencode($ref);
170
		$out.=($mode?'</font>':'');
171
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
172
		{
173
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
174
			else
175
			{
176
				$out.='&securekey='.($mode?'<font color="#666666">':'');
177
				if ($mode == 1) $out.="hash('".$conf->global->PAYMENT_SECURITY_TOKEN."' + '".$type."' + order_ref)";
178
				if ($mode == 0) $out.= dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $type . $ref, 2);
179
				$out.=($mode?'</font>':'');
180
			}
181
		}
182
	}
183
	if ($type == 'invoice')
184
	{
185
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?source=invoice&ref='.($mode?'<font color="#666666">':'');
186
		if ($mode == 1) $out.='invoice_ref';
187
		if ($mode == 0) $out.=urlencode($ref);
188
		$out.=($mode?'</font>':'');
189
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
190
		{
191
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
192
			else
193
			{
194
				$out.='&securekey='.($mode?'<font color="#666666">':'');
195
				if ($mode == 1) $out.="hash('".$conf->global->PAYMENT_SECURITY_TOKEN."' + '".$type."' + invoice_ref)";
196
				if ($mode == 0) $out.= dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $type . $ref, 2);
197
				$out.=($mode?'</font>':'');
198
			}
199
		}
200
	}
201
	if ($type == 'contractline')
202
	{
203
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?source=contractline&ref='.($mode?'<font color="#666666">':'');
204
		if ($mode == 1) $out.='contractline_ref';
205
		if ($mode == 0) $out.=urlencode($ref);
206
		$out.=($mode?'</font>':'');
207
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
208
		{
209
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
210
			else
211
			{
212
				$out.='&securekey='.($mode?'<font color="#666666">':'');
213
				if ($mode == 1) $out.="hash('".$conf->global->PAYMENT_SECURITY_TOKEN."' + '".$type."' + contractline_ref)";
214
				if ($mode == 0) $out.= dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $type . $ref, 2);
215
				$out.=($mode?'</font>':'');
216
			}
217
		}
218
	}
219
	if ($type == 'member' || $type == 'membersubscription')
220
	{
221
		$out=DOL_MAIN_URL_ROOT.'/public/payment/newpayment.php?source=membersubscription&ref='.($mode?'<font color="#666666">':'');
222
		if ($mode == 1) $out.='member_ref';
223
		if ($mode == 0) $out.=urlencode($ref);
224
		$out.=($mode?'</font>':'');
225
		if (! empty($conf->global->PAYMENT_SECURITY_TOKEN))
226
		{
227
			if (empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYMENT_SECURITY_TOKEN;
228
			else
229
			{
230
				$out.='&securekey='.($mode?'<font color="#666666">':'');
231
				if ($mode == 1) $out.="hash('".$conf->global->PAYMENT_SECURITY_TOKEN."' + '".$type."' + member_ref)";
232
				if ($mode == 0) $out.= dol_hash($conf->global->PAYMENT_SECURITY_TOKEN . $type . $ref, 2);
233
				$out.=($mode?'</font>':'');
234
			}
235
		}
236
	}
237
238
	// For multicompany
239
	if (! empty($out) && ! empty($conf->multicompany->enabled)) $out.="&entity=".$conf->entity; // Check the entity because we may have the same reference in several entities
240
241
	return $out;
242
}
243
244
245
246
/**
247
 * Show footer of company in HTML pages
248
 *
249
 * @param   Societe		$fromcompany	Third party
250
 * @param   Translate	$langs			Output language
251
 * @param	int			$addformmessage	Add the payment form message
252
 * @param	string		$suffix			Suffix to use on constants
253
 * @param	Object		$object			Object related to payment
254
 * @return	void
255
 */
256
function htmlPrintOnlinePaymentFooter($fromcompany, $langs, $addformmessage=0, $suffix='', $object=null)
257
{
258
    global $conf;
259
260
    // Juridical status
261
    $line1="";
262
    if ($fromcompany->forme_juridique_code)
263
    {
264
        $line1.=($line1?" - ":"").getFormeJuridiqueLabel($fromcompany->forme_juridique_code);
265
    }
266
    // Capital
267
    if ($fromcompany->capital)
268
    {
269
        $line1.=($line1?" - ":"").$langs->transnoentities("CapitalOf",$fromcompany->capital)." ".$langs->transnoentities("Currency".$conf->currency);
270
    }
271
    // Prof Id 1
272
    if ($fromcompany->idprof1 && ($fromcompany->country_code != 'FR' || ! $fromcompany->idprof2))
273
    {
274
        $field=$langs->transcountrynoentities("ProfId1",$fromcompany->country_code);
275
        if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
276
        $line1.=($line1?" - ":"").$field.": ".$fromcompany->idprof1;
277
    }
278
    // Prof Id 2
279
    if ($fromcompany->idprof2)
280
    {
281
        $field=$langs->transcountrynoentities("ProfId2",$fromcompany->country_code);
282
        if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
283
        $line1.=($line1?" - ":"").$field.": ".$fromcompany->idprof2;
284
    }
285
286
    // Second line of company infos
287
    $line2="";
288
    // Prof Id 3
289
    if ($fromcompany->idprof3)
290
    {
291
        $field=$langs->transcountrynoentities("ProfId3",$fromcompany->country_code);
292
        if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
293
        $line2.=($line2?" - ":"").$field.": ".$fromcompany->idprof3;
294
    }
295
    // Prof Id 4
296
    if ($fromcompany->idprof4)
297
    {
298
        $field=$langs->transcountrynoentities("ProfId4",$fromcompany->country_code);
299
        if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
300
        $line2.=($line2?" - ":"").$field.": ".$fromcompany->idprof4;
301
    }
302
    // IntraCommunautary VAT
303
    if ($fromcompany->tva_intra != '')
304
    {
305
        $line2.=($line2?" - ":"").$langs->transnoentities("VATIntraShort").": ".$fromcompany->tva_intra;
306
    }
307
308
    print '<br>';
309
310
    print '<div class="center">'."\n";
311
    if ($addformmessage)
312
    {
313
    	print '<!-- object = '.$object->element.' -->';
314
    	print '<br>';
315
316
    	$parammessageform='ONLINE_PAYMENT_MESSAGE_FORM_'.$suffix;
317
    	if (! empty($conf->global->$parammessageform)) print $langs->transnoentities($conf->global->$parammessageform);
318
    	else if (! empty($conf->global->ONLINE_PAYMENT_MESSAGE_FORM)) print $langs->transnoentities($conf->global->ONLINE_PAYMENT_MESSAGE_FORM);
319
320
    	// Add other message if VAT exists
321
    	if ($object->total_vat != 0 || $object->total_tva != 0)
322
    	{
323
    		$parammessageform='ONLINE_PAYMENT_MESSAGE_FORMIFVAT_'.$suffix;
324
    		if (! empty($conf->global->$parammessageform)) print $langs->transnoentities($conf->global->$parammessageform);
325
    		else if (! empty($conf->global->ONLINE_PAYMENT_MESSAGE_FORMIFVAT)) print $langs->transnoentities($conf->global->ONLINE_PAYMENT_MESSAGE_FORMIFVAT);
326
    	}
327
    }
328
329
    print '<font style="font-size: 10px;"><br><hr>'."\n";
330
    print $fromcompany->name.'<br>';
331
    print $line1;
332
    if (strlen($line1+$line2) > 50) print '<br>';
333
    else print ' - ';
334
    print $line2;
335
    print '</font></div>'."\n";
336
}
337