| Conditions | 22 |
| Paths | 5454 |
| Total Lines | 159 |
| Code Lines | 114 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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); |
||
| 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)){ |
||
| 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", |
||
| 235 | "metadata" => $metadata, |
||
| 236 | "receipt_email" => $societe->email, |
||
| 237 | "source" => "$source", |
||
| 238 | "customer" => "$customer") |
||
| 239 | ,array("idempotency_key" => "$ref") |
||
| 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; |
||
| 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++; |
||
| 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 | |||
| 321 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: