dolibarr 20.0.0
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
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', '1');
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', '1');
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', '1');
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', '1');
39}
40if (!defined('NOBROWSERNOTIF')) {
41 define('NOBROWSERNOTIF', '1');
42}
43
44// Load Dolibarr environment
45require '../../main.inc.php'; // Load $user and permissions
46require_once DOL_DOCUMENT_ROOT.'/includes/stripe/stripe-php/init.php';
47require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
48require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
49
50$action = GETPOST('action', 'aZ09');
51$location = GETPOST('location', 'alphanohtml');
52$stripeacc = GETPOST('stripeacc', 'alphanohtml');
53$servicestatus = GETPOSTINT('servicestatus');
54$amount = GETPOSTINT('amount');
55
56if (!$user->hasRight('takepos', 'run')) {
57 accessforbidden('Not allowed to use TakePOS');
58}
59
60$usestripeterminals = getDolGlobalString('STRIPE_LOCATION');
61if (! $usestripeterminals) {
62 accessforbidden('Feature to use Stripe terminals not enabled');
63}
64
65
66/*
67 * View
68 */
69
70top_httphead('application/json');
71
72if ($action == 'getConnexionToken') {
73 try {
74 // Be sure to authenticate the endpoint for creating connection tokens.
75 // Force to use the correct API key
76 global $stripearrayofkeysbyenv;
77 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$servicestatus]['secret_key']);
78 // The ConnectionToken's secret lets you connect to any Stripe Terminal reader
79 // and take payments with your Stripe account.
80 $array = array();
81 if (isset($location) && !empty($location)) {
82 $array['location'] = $location;
83 }
84 if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
85 $connectionToken = \Stripe\Terminal\ConnectionToken::create($array);
86 } else {
87 $connectionToken = \Stripe\Terminal\ConnectionToken::create($array, array("stripe_account" => $stripeacc));
88 }
89 echo json_encode(array('secret' => $connectionToken->secret));
90 } catch (Error $e) {
91 http_response_code(500);
92 echo json_encode(['error' => $e->getMessage()]);
93 }
94} elseif ($action == 'createPaymentIntent') {
95 try {
96 $json_str = file_get_contents('php://input');
97 $json_obj = json_decode($json_str);
98
99 // For Terminal payments, the 'payment_method_types' parameter must include
100 // 'card_present' and the 'capture_method' must be set to 'manual'
101 $object = new Facture($db);
102 $object->fetch($json_obj->invoiceid);
103 $object->fetch_thirdparty();
104
105 $fulltag='INV='.$object->id.'.CUS='.$object->thirdparty->id;
106 $tag=null;
107 $fulltag=dol_string_unaccent($fulltag);
108
109 $stripe = new Stripe($db);
110 $customer = $stripe->customerStripe($object->thirdparty, $stripeacc, $servicestatus, 1);
111
112 $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);
113
114 echo json_encode(array('client_secret' => $intent->client_secret));
115 } catch (Error $e) {
116 http_response_code(500);
117 echo json_encode(['error' => $e->getMessage()]);
118 }
119} elseif ($action == 'capturePaymentIntent') {
120 try {
121 // retrieve JSON from POST body
122 $json_str = file_get_contents('php://input');
123 $json_obj = json_decode($json_str);
124 if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
125 $intent = \Stripe\PaymentIntent::retrieve($json_obj->id);
126 } else {
127 $intent = \Stripe\PaymentIntent::retrieve($json_obj->id, array("stripe_account" => $stripeacc));
128 }
129 $intent = $intent->capture();
130
131 echo json_encode($intent);
132 } catch (Error $e) {
133 http_response_code(500);
134 echo json_encode(['error' => $e->getMessage()]);
135 }
136}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage invoices.
Stripe class @TODO No reason to extends CommonObject.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.