dolibarr  x.y.z
ajax.php
Go to the documentation of this file.
1 <?php
2  /* Copyright (C) 2021 Thibault FOUCART <support@ptibogxiv.net>
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 <https://www.gnu.org/licenses/>.
16  */
17 
23 if (!defined('NOTOKENRENEWAL')) {
24  define('NOTOKENRENEWAL', '1');
25 }
26 if (!defined('NOREQUIREMENU')) {
27  define('NOREQUIREMENU', '1');
28 }
29 if (!defined('NOREQUIREHTML')) {
30  define('NOREQUIREHTML', '1');
31 }
32 if (!defined('NOREQUIREAJAX')) {
33  define('NOREQUIREAJAX', '1');
34 }
35 if (!defined('NOBROWSERNOTIF')) {
36  define('NOBROWSERNOTIF', '1');
37 }
38 
39 // Load Dolibarr environment
40 require '../../main.inc.php'; // Load $user and permissions
41 require_once DOL_DOCUMENT_ROOT.'/includes/stripe/stripe-php/init.php';
42 require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
43 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
44 
45 $action = GETPOST('action', 'aZ09');
46 $location = GETPOST('location', 'alphanohtml');
47 $stripeacc = GETPOST('stripeacc', 'alphanohtml');
48 $servicestatus = GETPOST('servicestatus', 'int');
49 $amount = GETPOST('amount', 'int');
50 
51 if (empty($user->rights->takepos->run)) {
53 }
54 
55 
56 /*
57  * View
58  */
59 
60 top_httphead('application/json');
61 
62 if ($action == 'getConnexionToken') {
63  try {
64  // Be sure to authenticate the endpoint for creating connection tokens.
65  // Force to use the correct API key
66  global $stripearrayofkeysbyenv;
67  \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$servicestatus]['secret_key']);
68  // The ConnectionToken's secret lets you connect to any Stripe Terminal reader
69  // and take payments with your Stripe account.
70  $array = array();
71  if (isset($location) && !empty($location)) $array['location'] = $location;
72  if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
73  $connectionToken = \Stripe\Terminal\ConnectionToken::create($array);
74  } else {
75  $connectionToken = \Stripe\Terminal\ConnectionToken::create($array, array("stripe_account" => $stripeacc));
76  }
77  echo json_encode(array('secret' => $connectionToken->secret));
78  } catch (Error $e) {
79  http_response_code(500);
80  echo json_encode(['error' => $e->getMessage()]);
81  }
82 } elseif ($action == 'createPaymentIntent') {
83  try {
84  $json_str = file_get_contents('php://input');
85  $json_obj = json_decode($json_str);
86 
87  // For Terminal payments, the 'payment_method_types' parameter must include
88  // 'card_present' and the 'capture_method' must be set to 'manual'
89  $object = new Facture($db);
90  $object->fetch($json_obj->invoiceid);
91  $object->fetch_thirdparty();
92 
93  $fulltag='INV='.$object->id.'.CUS='.$object->thirdparty->id;
94  $tag=null;
95  $fulltag=dol_string_unaccent($fulltag);
96 
97  $stripe = new Stripe($db);
98  $customer = $stripe->customerStripe($object->thirdparty, $stripeacc, $servicestatus, 1);
99 
100  $intent = $stripe->getPaymentIntent($json_obj->amount, $object->multicurrency_code, null, 'Stripe payment: '.$fulltag.(is_object($object)?' ref='.$object->ref:''), $object, $customer, $stripeacc, $servicestatus, 1, 'terminal', false, null, 0, 1);
101 
102  echo json_encode(array('client_secret' => $intent->client_secret));
103  } catch (Error $e) {
104  http_response_code(500);
105  echo json_encode(['error' => $e->getMessage()]);
106  }
107 } elseif ($action == 'capturePaymentIntent') {
108  try {
109  // retrieve JSON from POST body
110  $json_str = file_get_contents('php://input');
111  $json_obj = json_decode($json_str);
112  if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
113  $intent = \Stripe\PaymentIntent::retrieve($json_obj->id);
114  } else {
115  $intent = \Stripe\PaymentIntent::retrieve($json_obj->id, array("stripe_account" => $stripeacc));
116  }
117  $intent = $intent->capture();
118 
119  echo json_encode($intent);
120  } catch (Error $e) {
121  http_response_code(500);
122  echo json_encode(['error' => $e->getMessage()]);
123  }
124 }
Class to manage invoices.
Stripe class.
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1436
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.