Debug stripe payment method

This commit is contained in:
Laurent Destailleur 2018-03-16 00:43:47 +01:00
parent 84e1affd5b
commit 32ce7322c4

View File

@ -292,20 +292,23 @@ class Stripe extends CommonObject
/** /**
* Create charge with public/payment/newpayment.php, stripe/card.php, cronjobs or REST API * Create charge with public/payment/newpayment.php, stripe/card.php, cronjobs or REST API
* *
* @param int $amount Amount to pay * @param int $amount Amount to pay
* @param string $currency EUR, GPB... * @param string $currency EUR, GPB...
* @param string $origin Object type to pay (order, invoice, contract...) * @param string $origin Object type to pay (order, invoice, contract...)
* @param int $item Object id to pay * @param int $item Object id to pay
* @param string $source src_xxxxx or card_xxxxx * @param string $source src_xxxxx or card_xxxxx
* @param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe() * @param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe()
* @param string $account Stripe account ref 'acc_xxxxxxxxxxxxx' via getStripeAccount() * @param string $account Stripe account ref 'acc_xxxxxxxxxxxxx' via getStripeAccount()
* @param int $status Status (0=test, 1=live) * @param int $status Status (0=test, 1=live)
* @param int $usethirdpartyemailforreceiptemail Use thirdparty email as receipt email
* @return Stripe * @return Stripe
*/ */
public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status=0) public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status=0, $usethirdpartyemailforreceiptemail=0)
{ {
global $conf; global $conf;
$error = 0;
if (empty($status)) $service = 'StripeTest'; if (empty($status)) $service = 'StripeTest';
else $service = 'StripeLive'; else $service = 'StripeLive';
@ -335,16 +338,18 @@ class Stripe extends CommonObject
$societe = new Societe($this->db); $societe = new Societe($this->db);
if ($key > 0) $societe->fetch($key); if ($key > 0) $societe->fetch($key);
$description = "";
$ref = "";
if ($origin == order) { if ($origin == order) {
$order = new Commande($this->db); $order = new Commande($this->db);
$order->fetch($item); $order->fetch($item);
$ref = $order->ref; $ref = $order->ref;
$description = "ORD=" . $ref . ".CUS=" . $societe->code_client; $description = "ORD=" . $ref . ".CUS=" . $societe->id;
} elseif ($origin == invoice) { } elseif ($origin == invoice) {
$invoice = new Facture($this->db); $invoice = new Facture($this->db);
$invoice->fetch($item); $invoice->fetch($item);
$ref = $invoice->ref; $ref = $invoice->ref;
$description = "INV=" . $ref . ".CUS=" . $societe->code_client; $description = "INV=" . $ref . ".CUS=" . $societe->id;
} }
$metadata = array( $metadata = array(
@ -369,18 +374,22 @@ class Stripe extends CommonObject
"source" => "$source" "source" => "$source"
)); ));
} else { } else {
$charge = \Stripe\Charge::create(array( $paymentarray = array(
"amount" => "$stripeamount", "amount" => "$stripeamount",
"currency" => "$currency", "currency" => "$currency",
// "statement_descriptor" => " ", // "statement_descriptor" => " ",
"description" => "$description", "description" => "$description",
"metadata" => $metadata, "metadata" => $metadata,
"receipt_email" => $societe->email,
"source" => "$source", "source" => "$source",
"customer" => "$customer" "customer" => "$customer"
), array( );
"idempotency_key" => "$ref"
)); if ($societe->email && $usethirdpartyemailforreceiptemail)
{
$paymentarray["receipt_email"] = $societe->email;
}
$charge = \Stripe\Charge::create($paymentarray, array("idempotency_key" => "$ref"));
} }
} else { } else {
@ -408,17 +417,18 @@ class Stripe extends CommonObject
$return->statut = 'success'; $return->statut = 'success';
$return->id = $charge->id; $return->id = $charge->id;
if ($charge->source->type == 'card') { if ($charge->source->type == 'card') {
$return->message = $charge->source->card->brand . " ****" . $charge->source->card->last4; $return->message = $charge->source->card->brand . " ...." . $charge->source->card->last4;
} elseif ($charge->source->type == 'three_d_secure') { } elseif ($charge->source->type == 'three_d_secure') {
$stripe = new Stripe($this->db); $stripe = new Stripe($this->db);
$src = \Stripe\Source::retrieve("" . $charge->source->three_d_secure->card . "", array( $src = \Stripe\Source::retrieve("" . $charge->source->three_d_secure->card . "", array(
"stripe_account" => $stripe->getStripeAccount($service) "stripe_account" => $stripe->getStripeAccount($service)
)); ));
$return->message = $src->card->brand . " ****" . $src->card->last4; $return->message = $src->card->brand . " ...." . $src->card->last4;
} else { } else {
$return->message = $charge->id; $return->message = $charge->id;
} }
} catch (\Stripe\Error\Card $e) { } catch (\Stripe\Error\Card $e) {
include DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
// Since it's a decline, \Stripe\Error\Card will be caught // Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody(); $body = $e->getJsonBody();
$err = $body['error']; $err = $body['error'];
@ -429,36 +439,37 @@ class Stripe extends CommonObject
$return->code = $err['code']; $return->code = $err['code'];
$return->message = $err['message']; $return->message = $err['message'];
$body = "Error: <br>" . $return->id . " " . $return->message . " "; $body = "Error: <br>" . $return->id . " " . $return->message . " ";
$subject = '[NOTIFICATION] Erreur de paiement'; $subject = '[Alert] Payment error using Stripe';
$headers = 'From: "noreply" <' . $conf->global->MAIN_INFO_SOCIETE_MAIL . '>'; $cmailfile = new CMailFile($subject, $conf->global->ONLINE_PAYMENT_SENDEMAIL, $conf->global->MAIN_INFO_SOCIETE_MAIL, $body);
mail('' . $conf->global->MAIN_INFO_SOCIETE_MAIL . '', $subject, $body, $headers); $cmailfile->sendfile();
$error ++;
$error++;
dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
} catch (\Stripe\Error\RateLimit $e) { } catch (\Stripe\Error\RateLimit $e) {
// Too many requests made to the API too quickly // Too many requests made to the API too quickly
$error ++; $error++;
dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
} catch (\Stripe\Error\InvalidRequest $e) { } catch (\Stripe\Error\InvalidRequest $e) {
// Invalid parameters were supplied to Stripe's API // Invalid parameters were supplied to Stripe's API
$error ++; $error++;
dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
} catch (\Stripe\Error\Authentication $e) { } catch (\Stripe\Error\Authentication $e) {
// Authentication with Stripe's API failed // Authentication with Stripe's API failed
// (maybe you changed API keys recently) // (maybe you changed API keys recently)
$error ++; $error++;
dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
} catch (\Stripe\Error\ApiConnection $e) { } catch (\Stripe\Error\ApiConnection $e) {
// Network communication with Stripe failed // Network communication with Stripe failed
$error ++; $error++;
dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
} catch (\Stripe\Error\Base $e) { } catch (\Stripe\Error\Base $e) {
// Display a very generic error to the user, and maybe send // Display a very generic error to the user, and maybe send
// yourself an email // yourself an email
$error ++; $error++;
dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
} catch (Exception $e) { } catch (Exception $e) {
// Something else happened, completely unrelated to Stripe // Something else happened, completely unrelated to Stripe
$error ++; $error++;
dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe');
} }
return $return; return $return;