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