1KA_F2F/frontend/payments/classes/class.UserNarocilaStripe.php

163 lines
5.4 KiB
PHP
Raw Normal View History

2020-08-14 13:36:36 +02:00
<?php
/**
*
* Class ki skrbi za placila s kreditno kartico (stripe) - TODO
*
*/
use \Stripe\Stripe;
use \Stripe\Customer;
use \Stripe\ApiOperations\Create;
use \Stripe\Charge;
class UserNarocilaStripe{
private $narocilo;
private $apiKey;
private $stripeService;
public function __construct($narocilo_id ){
global $app_settings;
global $stripe_secret;
global $stripe_key;
$this->apiKey = $stripe_secret;
$this->stripeService = new \Stripe\Stripe();
$this->stripeService->setVerifySslCerts(false);
$this->stripeService->setApiKey($this->apiKey);
if($narocilo_id > 0){
// Dobimo podatke narocila
$sqlNarocilo = sisplet_query("SELECT un.*, u.name, u.surname, u.email, up.name AS package_name, up.description AS package_description, up.price AS package_price
FROM user_access_narocilo un, users u, user_access_paket up
WHERE un.id='".$narocilo_id."' AND un.usr_id=u.id AND un.package_id=up.id");
if(mysqli_num_rows($sqlNarocilo) > 0){
$this->narocilo = mysqli_fetch_array($sqlNarocilo);
}
else{
die("Napaka pri komunikaciji s stripe! Narocilo ne obstaja.");
}
}
else {
die("Napaka pri komunikaciji s stripe! Manjka ID naročila.");
}
}
// Placamo narocilo s kreditno kartico preko stripa
public function stripePayment($token){
$UA = new UserNarocila();
$cena = $UA->getPrice($this->narocilo['package_name'], $this->narocilo['trajanje'], $this->narocilo['discount']);
if($this->narocilo['trajanje'] == 1)
$months_string = 'mesec';
elseif($this->narocilo['trajanje'] == 2)
$months_string = 'meseca';
elseif($this->narocilo['trajanje'] == 3 || $this->narocilo['trajanje'] == 4)
$months_string = 'mesece';
else
$months_string = 'mesecev';
// Zavezanec iz tujine ima racun/predracun brez ddv
if($UA->isWithoutDDV($this->narocilo['id'])){
$ddv = 0;
$cena_za_placilo = $cena['final_without_tax'];
}
else{
$ddv = 1;
$cena_za_placilo = $cena['final'];
}
// Podatki za kartico potrebni za placilo
$cardDetails = array(
'email' => $this->narocilo['email'],
'token' => $token,
'amount' => $cena_za_placilo * 100,
'currency_code' => 'eur',
'item_name' => '1KA naročnina (paket '.strtoupper($this->narocilo['package_name']). ' - '.$this->narocilo['trajanje'].' '.$months_string.')',
'item_number' => $this->narocilo['id'],
);
// Izvedemo placilo - stripe response
$stripeResponse = $this->chargeAmountFromCard($cardDetails);
// Vstavimo plačilo v bazo
$sqlNarocilo = sisplet_query("INSERT INTO user_access_stripe_charge
(narocilo_id, description, price, amount_paid, status, balance_transaction, time)
VALUES
('".$this->narocilo['id']."', '".$cardDetails['item_name']."', '".$cena_za_placilo."', '".($stripeResponse['amount'] / 100)."', '".$stripeResponse['status']."', '".$stripeResponse['balance_transaction']."', NOW())
");
if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
return $response;
}
$response = array();
// Placilo uspesno
if ($stripeResponse['amount_refunded'] == 0
&& empty($stripeResponse['failure_code'])
&& $stripeResponse['paid'] == 1
&& $stripeResponse['captured'] == 1
&& $stripeResponse['status'] == 'succeeded'
) {
$response['success'] = true;
$response['stripe_note'] = "Stripe payment is completed successfully. The TXN ID is " . $stripeResponse["balance_transaction"];
}
// Placilo ni uspelo
else{
$response['error'] = 'ERROR! Stripe payment failed. Failure code '.$stripeResponse['failure_code'];
}
return $response;
}
private function chargeAmountFromCard($cardDetails){
// Iz emaila in tokena ustvarimo stranko
$customerDetailsAry = array(
'email' => $cardDetails['email'],
'source' => $cardDetails['token']
);
$customerResult = $this->addCustomer($customerDetailsAry);
$charge = new Charge();
// Napolnimo podatke za placilo
$cardDetailsAry = array(
'customer' => $customerResult->id,
'amount' => $cardDetails['amount'],
'currency' => $cardDetails['currency_code'],
'description' => $cardDetails['item_name'],
'metadata' => array(
'order_id' => $cardDetails['item_number']
)
);
// Izvedemo "charge"
$result = $charge->create($cardDetailsAry);
return $result->jsonSerialize();
}
private function addCustomer($customerDetailsAry){
$customer = new Customer();
$customerDetails = $customer->create($customerDetailsAry);
return $customerDetails;
}
}