Files
GestioneSpiaggia.it/library/App/Stripe/StripeApi.php
T
2021-09-17 12:41:45 +02:00

108 lines
2.6 KiB
PHP

<?php
use \Stripe\Stripe;
use \Stripe\Charge;
use \Stripe\Token;
use \Stripe\Error\Card;
class App_Stripe_StripeApi
{
//Parametri test dell'account TEST STRIPE info@adriaticasistemi.it
const TEST_SECRECT_KEY = '';
const TEST_PUBLISHABLE_KEY = '';
//Parametri per l'account LIVE
const LIVE_SECRET_KEY = '';
const LIVE_PUBLISHABLE_KEY = '';
const DEFAULT_DESCRIPTION_TEXT = "";
protected $_apiStripeTestAccount;
protected $_configs;
protected $_ipManager;
protected $_secretKey;
protected $_publishableKey;
protected $_currency;
protected $_Stripe;
public function __construct()
{
$this->_currency = 'eur';
$this->_configs = new App_Service_ConfigManager();
$this->_ipManager = new App_Service_IpManager();
$this->_apiStripeTestAccount = false;
if($this->_ipManager->allowedDevMode()){
$this->_apiStripeTestAccount = ($this->_configs->get('interrutore_stripe_test') == 'on') ? 1:0;
}
if ($this->_apiStripeTestAccount){
$this->_secretKey = self::TEST_SECRECT_KEY;
$this->_publishableKey = self::TEST_PUBLISHABLE_KEY;
} else{
$this->_secretKey = self::LIVE_SECRET_KEY;
$this->_publishableKey = self::LIVE_PUBLISHABLE_KEY;
}
$this->_Stripe = new Stripe();
$this->_Stripe->setApiKey($this->_secretKey);
}
/**
* @param $number
* @param $exp_month
* @param $exp_year
* @param $cvc
* @param null $extraData
*
* @return \Stripe\ApiOperations\ApiResource
*/
public function createToken($number, $exp_month, $exp_year, $cvc,$extraData = null)
{
$card = array(
'number' => $number,
'exp_month' => $exp_month,
'exp_year' => $exp_year,
'cvc' => $cvc
);
//Nel caso in cui passo dei dati extra li aggiungo alla chiamata per la creazionde del token
if($extraData && (is_array($extraData) || is_object($extraData))){
foreach($extraData as $key => $val){
$card[$key] = $val;
}
}
$token = Token::create(array('card' => $card));
return $token;
}
/**
* @param $amount
* @param $token
* @param string $description
*
* @return \Stripe\ApiOperations\ApiResource
* @throws Exception
*/
public function createCharge($amount, $token,$description = self::DEFAULT_DESCRIPTION_TEXT)
{
if(!$token)
throw new Exception("Il Token non è stato generato correttamente");
$charge = Charge::create(array(
"amount" => $amount,
"currency" => $this->_currency,
"card" => $token,
"description"=>$description
));
return $charge;
}
}