Completed
Branch develop (5b8006)
by
unknown
43:45
created

Stripe::GetStripeCustomerAccount()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2018 	PtibogXIV        <[email protected]>
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 <http://www.gnu.org/licenses/>.
16
 */
17
18
// Put here all includes required by your class file
19
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
20
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
21
require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
22
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
23
require_once DOL_DOCUMENT_ROOT.'/stripe/config.php';
24
/**                                                                                                
25
 *	\class      Rewards
26
 *	\brief      Class for Rewards
27
 */
28
class Stripe extends CommonObject
29
{
30
	public $rowid;
31
  public $fk_soc;
32
  public $fk_key;
33
  public $id;
34
  public $mode;  
35
  public $entity;
36
  public $statut;
37
  public $type;
38
  public $code;
39
  public $message;
40
	
41
	/**
42
	 * 	Constructor
43
	 *
44
	 * 	@param	DoliDB		$db			Database handler
45
	 */
46
	public function __construct($db)
47
	{
48
		$this->db = $db;
49
50
	}
51
	
52
53
public function GetStripeAccount($id) // TODO: move /transforme optimize function/data
54
	{
55
		global $conf;
56
57
		$sql = "SELECT key_account";
58
		$sql.= " FROM ".MAIN_DB_PREFIX."stripe_entity";
59
		$sql.= " WHERE entity = ".$id.""; 
60
61
		dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
62
		$result = $this->db->query($sql);
63
    if ($result)
64
		{
65
			if ($this->db->num_rows($result))
66
			{
67
				$obj = $this->db->fetch_object($result);
68
    $key=$obj->key_account;
69
    }
70
    else {$key=0;}
71
    }
72
    else {$key=0;}
73
    
74
return $key;
75
	} 
76
  
77
public function GetStripeCustomerAccount($id)
78
	{
79
		global $conf;
80
81
		$sql = "SELECT s.key_account as key_account, s.entity, e.fk_object";
82
		$sql.= " FROM ".MAIN_DB_PREFIX."stripe_entity as s";
83
    $sql.= " JOIN ".MAIN_DB_PREFIX."entity_extrafields as e ON s.entity=e.fk_object";
84
		$sql.= " WHERE e.fk_soc=".$id." "; 
85
86
		dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
87
		$result = $this->db->query($sql);
88
    if ($result)
89
		{
90
			if ($this->db->num_rows($result))
91
			{
92
				$obj = $this->db->fetch_object($result);
93
    $key=$obj->key_account;
94
    }
95
    else {$key=NULL;}
96
    }
97
    else {$key=NULL;}
98
    
99
return $key;
100
	}  
101
   
102
public function CustomerStripe($id,$key)
103
	{
104
global $conf;
105
if (empty($conf->global->STRIPECONNECT_LIVE))
106
{
107
$mode=0;
108
}
109
else 
110
{
111
if (empty($conf->global->STRIPE_LIVE))
112
{
113
$mode=0;
114
}
115
else
116
{	
117
$mode=$conf->global->STRIPE_LIVE;
118
}
119
}  
120
		$sql = "SELECT rowid,fk_soc,fk_key,mode,entity";
121
		$sql.= " FROM ".MAIN_DB_PREFIX."societe_stripe";
122
		$sql.= " WHERE fk_soc = ".$id." "; 
123
		$sql.= " AND  mode=".$mode." AND entity IN (" . getEntity('stripe') . ")";
124
125
		dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
126
		$resql = $this->db->query($sql);
127
		if ($resql) {
128
    $soc = new Societe($this->db);
129
    $soc->fetch($id);
130
    $num=$this->db->num_rows($resql);
131
			if ($num) {
132
			$obj = $this->db->fetch_object($resql);
133
      $tiers = $obj->fk_key; 
134
      if ($conf->entity==1){
135
      $customer = \Stripe\Customer::retrieve("$tiers");
136
      }else{
137
      $customer = \Stripe\Customer::retrieve("$tiers",array("stripe_account" => $key));
138
      }}
139
      else {
140
      if ($conf->entity==1){
141
      $customer = \Stripe\Customer::create(array(
142
      "email" => $soc->email,
143
      "description" => $soc->name
144
      ));
145
      }else{
146
      $customer = \Stripe\Customer::create(array(
147
      "email" => $soc->email,
148
      "description" => $soc->name
149
      ), array("stripe_account" => $key));
150
      }
151
      $customer_id = "".$customer->id."";
152
      $sql  = "INSERT INTO ".MAIN_DB_PREFIX."societe_stripe (fk_soc,fk_key,mode,entity)";
153
      $sql .= " VALUES ($id,'$customer_id',".$mode.",".$conf->entity.")"; 
154
      dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG);
155
      $resql = $this->db->query($sql);
156
      }}
157
return $customer;
0 ignored issues
show
Bug introduced by
The variable $customer does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
158
	}
159
  
160
public function CreatePaymentStripe($amount,$currency,$origin,$item,$source,$customer,$account)
161
{
162
global $conf;
163
if (empty($conf->global->STRIPECONNECT_LIVE))
164
{
165
$mode=0;
166
}
167
else 
168
{
169
if (empty($conf->global->STRIPE_LIVE))
170
{
171
$mode=0;
172
}
173
else
174
{	
175
$mode=$conf->global->STRIPE_LIVE;
176
}
177
} 
178
		$sql = "SELECT fk_soc,fk_key,mode,entity";
179
		$sql.= " FROM ".MAIN_DB_PREFIX."societe_stripe";
180
		$sql.= " WHERE fk_key = '$customer' "; 
181
		$sql.= " AND mode=".$mode." "; 
182
183
		dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
184
		$result = $this->db->query($sql);
185
    if ($result)
186
		{
187
			if ($this->db->num_rows($result))
188
			{
189
				$obj = $this->db->fetch_object($result);
190
    $entite=$obj->entity;
191
    $fksoc=$obj->fk_soc;
192
    }
193
    }
194
$stripeamount=round($amount*100);   
195
$societe = new Societe($this->db);
196
$societe->fetch($fksoc);
0 ignored issues
show
Bug introduced by
The variable $fksoc does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
197
198
if ($origin==order){
199
$order=new Commande($this->db);
200
$order->fetch($item);
201
$ref=$order->ref;
202
$description="ORD=".$ref.".CUS=".$societe->code_client;
203
}
204
elseif ($origin==invoice) {
205
$invoice=new Facture($this->db);
206
$invoice->fetch($item);
207
$ref=$invoice->ref;
208
$description="INV=".$ref.".CUS=".$societe->code_client;
209
} 
210
211
$metadata = array(
212
    "source" => "".$origin."",
213
    "idsource" => "".$item."",
214
    "idcustomer" => "".$societe->id.""
215
  );
216
$return = new Stripe($this->db);   
217
try {
218
if ($stripeamount>=100) {
219
if ($entite=='1' or empty($conf->stripeconnect->enabled)){
0 ignored issues
show
Bug introduced by
The variable $entite does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
220
if (preg_match('/acct_/i',$source)){
221
   $charge = \Stripe\Charge::create(array(
222
  "amount" => "$stripeamount",
223
  "currency" => "$currency",
224
//  "statement_descriptor" => " ",
225
  "metadata" => $metadata,
226
  "source" => "$source"
227
)
228
);
229
} else {
230
$charge = \Stripe\Charge::create(array(
231
  "amount" => "$stripeamount",
232
  "currency" => "$currency",
233
//  "statement_descriptor" => " ",
234
  "description" => "$description",
0 ignored issues
show
Bug introduced by
The variable $description does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
235
  "metadata" => $metadata,
236
  "receipt_email" => $societe->email,
237
  "source" => "$source",
238
  "customer" => "$customer") 
239
 ,array("idempotency_key" => "$ref")
0 ignored issues
show
Bug introduced by
The variable $ref does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
240
); 
241
}}else{
242
$fee=round(($amount*($conf->global->STRIPE_APPLICATION_FEE_PERCENT/100)+$conf->global->STRIPE_APPLICATION_FEE)*100);
243
if ($fee<($conf->global->STRIPE_APPLICATION_FEE_MINIMAL*100)){
244
$fee=round($conf->global->STRIPE_APPLICATION_FEE_MINIMAL*100);
245
}
246
$charge = \Stripe\Charge::create(array(
247
  "amount" => "$stripeamount",
248
  "currency" => "$currency",
249
//  "statement_descriptor" => " ",
250
  "description" => "$description",
251
  "metadata" => $metadata,
252
  "source" => "$source",
253
  "customer" => "$customer",
254
  "application_fee" => "$fee"
255
), array("idempotency_key" => "$ref","stripe_account" => "$account"));
256
}
257
if (isset($charge->id)){
258
259
}
260
}
261
262
$return->statut = 'success';
263
$return->id = $charge->id;
0 ignored issues
show
Bug introduced by
The variable $charge does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
264
if ($charge->source->type=='card'){
265
$return->message = $charge->source->card->brand." ****".$charge->source->card->last4;
266
}elseif ($charge->source->type=='three_d_secure'){
267
$stripe=new Stripe($this->db);
268
$src = \Stripe\Source::retrieve("".$charge->source->three_d_secure->card."",array("stripe_account" => $stripe->GetStripeAccount($conf->entity)));
269
$return->message = $src->card->brand." ****".$src->card->last4;
270
}else {
271
$return->message = $charge->id;
272
}
273
274
} catch(\Stripe\Error\Card $e) {
275
        // Since it's a decline, \Stripe\Error\Card will be caught
276
        $body = $e->getJsonBody();
277
        $err  = $body['error'];
278
        
279
$return->statut = 'error';        
280
$return->id = $err['charge'];
281
$return->type = $err['type'];
282
$return->code = $err['code'];
283
$return->message = $err['message'];
284
$body = "Une erreur de paiement est survenue. Voici le code d'erreur: <br />".$return->id." ".$return->message." "; 
285
$subject = '[NOTIFICATION] Erreur de paiement';
286
$headers = 'From: "ptibogxiv.net" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
287
mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); 
288
        $error++;
0 ignored issues
show
Bug introduced by
The variable $error does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
289
        dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
290
    } catch (\Stripe\Error\RateLimit $e) {
291
        // Too many requests made to the API too quickly
292
        $error++;
293
        dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
294
    } catch (\Stripe\Error\InvalidRequest $e) {
295
        // Invalid parameters were supplied to Stripe's API
296
        $error++;
297
        dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
298
    } catch (\Stripe\Error\Authentication $e) {
299
        // Authentication with Stripe's API failed
300
        // (maybe you changed API keys recently)
301
        $error++;
302
        dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
303
    } catch (\Stripe\Error\ApiConnection $e) {
304
        // Network communication with Stripe failed
305
        $error++;
306
        dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
307
    } catch (\Stripe\Error\Base $e) {
308
        // Display a very generic error to the user, and maybe send
309
        // yourself an email
310
        $error++;
311
        dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
312
    } catch (Exception $e) {
313
        // Something else happened, completely unrelated to Stripe
314
        $error++;
315
        dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
316
    }      
317
        return $return;
318
} 
319
   
320
}
321