ajax.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  18. * \file htdocs/stripe/ajax/ajax.php
  19. * \brief Ajax action for Stipe ie: Terminal
  20. */
  21. if (!defined('NOTOKENRENEWAL')) {
  22. define('NOTOKENRENEWAL', '1');
  23. }
  24. if (!defined('NOREQUIREMENU')) {
  25. define('NOREQUIREMENU', '1');
  26. }
  27. if (!defined('NOREQUIREHTML')) {
  28. define('NOREQUIREHTML', '1');
  29. }
  30. if (!defined('NOREQUIREAJAX')) {
  31. define('NOREQUIREAJAX', '1');
  32. }
  33. if (!defined('NOBROWSERNOTIF')) {
  34. define('NOBROWSERNOTIF', '1');
  35. }
  36. // Load Dolibarr environment
  37. require '../../main.inc.php'; // Load $user and permissions
  38. require_once DOL_DOCUMENT_ROOT.'/includes/stripe/stripe-php/init.php';
  39. require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  41. $action = GETPOST('action', 'aZ09');
  42. $location = GETPOST('location', 'alphanohtml');
  43. $stripeacc = GETPOST('stripeacc', 'alphanohtml');
  44. $servicestatus = GETPOST('servicestatus', 'int');
  45. $amount = GETPOST('amount', 'int');
  46. if (empty($user->rights->takepos->run)) {
  47. accessforbidden();
  48. }
  49. /*
  50. * View
  51. */
  52. top_httphead('application/json');
  53. if ($action == 'getConnexionToken') {
  54. try {
  55. // Be sure to authenticate the endpoint for creating connection tokens.
  56. // Force to use the correct API key
  57. global $stripearrayofkeysbyenv;
  58. \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$servicestatus]['secret_key']);
  59. // The ConnectionToken's secret lets you connect to any Stripe Terminal reader
  60. // and take payments with your Stripe account.
  61. $array = array();
  62. if (isset($location) && !empty($location)) $array['location'] = $location;
  63. if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
  64. $connectionToken = \Stripe\Terminal\ConnectionToken::create($array);
  65. } else {
  66. $connectionToken = \Stripe\Terminal\ConnectionToken::create($array, array("stripe_account" => $stripeacc));
  67. }
  68. echo json_encode(array('secret' => $connectionToken->secret));
  69. } catch (Error $e) {
  70. http_response_code(500);
  71. echo json_encode(['error' => $e->getMessage()]);
  72. }
  73. } elseif ($action == 'createPaymentIntent') {
  74. try {
  75. $json_str = file_get_contents('php://input');
  76. $json_obj = json_decode($json_str);
  77. // For Terminal payments, the 'payment_method_types' parameter must include
  78. // 'card_present' and the 'capture_method' must be set to 'manual'
  79. $object = new Facture($db);
  80. $object->fetch($json_obj->invoiceid);
  81. $object->fetch_thirdparty();
  82. $fulltag='INV='.$object->id.'.CUS='.$object->thirdparty->id;
  83. $tag=null;
  84. $fulltag=dol_string_unaccent($fulltag);
  85. $stripe = new Stripe($db);
  86. $customer = $stripe->customerStripe($object->thirdparty, $stripeacc, $servicestatus, 1);
  87. $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);
  88. echo json_encode(array('client_secret' => $intent->client_secret));
  89. } catch (Error $e) {
  90. http_response_code(500);
  91. echo json_encode(['error' => $e->getMessage()]);
  92. }
  93. } elseif ($action == 'capturePaymentIntent') {
  94. try {
  95. // retrieve JSON from POST body
  96. $json_str = file_get_contents('php://input');
  97. $json_obj = json_decode($json_str);
  98. if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
  99. $intent = \Stripe\PaymentIntent::retrieve($json_obj->id);
  100. } else {
  101. $intent = \Stripe\PaymentIntent::retrieve($json_obj->id, array("stripe_account" => $stripeacc));
  102. }
  103. $intent = $intent->capture();
  104. echo json_encode($intent);
  105. } catch (Error $e) {
  106. http_response_code(500);
  107. echo json_encode(['error' => $e->getMessage()]);
  108. }
  109. }