Stripe merge

This commit is contained in:
Robert 2020-11-17 06:32:42 +01:00
commit b18ac6653f
4 changed files with 203 additions and 100 deletions

View File

@ -303,8 +303,35 @@ class ApiNarocilaController{
$this->response['success'] = true; $this->response['success'] = true;
} }
else {
$this->response['error'] = 'Napaka! Manjkajo zahtevani parametri!';
}
break;
// Dokoncaj narocilo ce je placano preko stripe (ko je stranka potrdila placilo preko sca)
case 'stripe_checkout_success':
if(isset($this->data['narocilo_id'])){
$stripe = new UserNarocilaStripe($this->data['narocilo_id']);
$this->response = $stripe->stripeCheckoutSuccess();
}
else{ else{
$this->response['error'] = 'Napaka! Manjkajo zahtevani parametri!'; $this->response['error'] = 'Napaka! Manjka ID narocila!';
$this->response['success'] = false;
}
break;
// Preklici narocilo za stripe (ko je stranka preklicala placilo preko sca)
case 'stripe_checkout_cancel':
if(isset($this->data['narocilo_id'])){
$stripe = new UserNarocilaStripe($this->data['narocilo_id']);
$this->response = $stripe->stripeCheckoutCancel();
}
else{
$this->response['error'] = 'Napaka! Manjka ID narocila!';
$this->response['success'] = false; $this->response['success'] = false;
} }

View File

@ -792,42 +792,22 @@ class UserNarocila{
$response = array(); $response = array();
$token = isset($narocilo_data['stripe_id']) ? $narocilo_data['stripe_id'] : ''; // Inicializiramo paypal
if($token == ''){
$response['error'] = 'ERROR! Missing token.';
$response['success'] = false;
return $response;
}
// Inicializiramo stripe
$stripe = new UserNarocilaStripe($narocilo_id); $stripe = new UserNarocilaStripe($narocilo_id);
$stripe_response = $stripe->stripePayment($token); // Ustvarimo stripe session za placilo in vrnemo id sessiona, da uporabnik potrdi placilo
$stripe_response = $stripe->stripeCreateSession();
// Ce je bilo placilo preko stripa uspesno zgeneriramo racun in uporabniku aktiviramo paket // Ce je bilo placilo preko stripa uspesno zgeneriramo racun in uporabniku aktiviramo paket
if($stripe_response['success'] == true){ if($stripe_response['success'] == true){
$response['session_id'] = $stripe_response['session_id'];
$response['stripe_note'] = $stripe_response['stripe_note']; $response['success'] = true;
$payment_response = $this->payNarocilo($narocilo_id);
if($payment_response['success'] == true){
$response['racun'] = $payment_response['racun'];
$response['success'] = true;
}
else{
$response['error'] = $payment_response['error'];
$response['success'] = false;
}
} }
else{ else{
$response['error'] = $stripe_response['error']; $response['error'] = $stripe_response['error'];
$response['success'] = false; $response['success'] = false;
} }
$response['narocilo_id'] = $narocilo_id;
return $response; return $response;
} }

View File

@ -12,6 +12,8 @@ use \Stripe\Customer;
use \Stripe\ApiOperations\Create; use \Stripe\ApiOperations\Create;
use \Stripe\Charge; use \Stripe\Charge;
use \Stripe\StripeClient;
class UserNarocilaStripe{ class UserNarocilaStripe{
@ -22,16 +24,12 @@ class UserNarocilaStripe{
private $stripeService; private $stripeService;
public function __construct($narocilo_id ){ public function __construct($narocilo_id){
global $app_settings; global $app_settings;
global $stripe_secret; global $stripe_secret;
global $stripe_key; global $stripe_key;
$this->apiKey = $stripe_secret; $this->stripeService = new \Stripe\StripeClient($stripe_secret);
$this->stripeService = new \Stripe\Stripe();
$this->stripeService->setVerifySslCerts(false);
$this->stripeService->setApiKey($this->apiKey);
if($narocilo_id > 0){ if($narocilo_id > 0){
@ -52,9 +50,14 @@ class UserNarocilaStripe{
} }
// Placamo narocilo s kreditno kartico preko stripa // Ustvarimo session za placilo v stripe - V DELU
public function stripePayment($token){ public function stripeCreateSession(){
global $site_url;
global $lang;
$response = array();
$UA = new UserNarocila(); $UA = new UserNarocila();
$cena = $UA->getPrice($this->narocilo['package_name'], $this->narocilo['trajanje'], $this->narocilo['discount']); $cena = $UA->getPrice($this->narocilo['package_name'], $this->narocilo['trajanje'], $this->narocilo['discount']);
@ -78,95 +81,184 @@ class UserNarocilaStripe{
$cena_za_placilo = $cena['final']; $cena_za_placilo = $cena['final'];
} }
// Podatki za kartico potrebni za placilo
$cardDetails = array(
'email' => $this->narocilo['email'],
'token' => $token,
'amount' => $cena_za_placilo * 100, // URL po potrditvi oz preklicu
'currency_code' => 'eur', if($lang['id'] == '2'){
'item_name' => '1KA naročnina (paket '.strtoupper($this->narocilo['package_name']). ' - '.$this->narocilo['trajanje'].' '.$months_string.')', $drupal_url_confirm = $site_url.'/d/en/stripe-purchase/success?narocilo_id='.$this->narocilo['id'];
'item_number' => $this->narocilo['id'], $drupal_url_cancel = $site_url.'/d/en/stripe-purchase/cancel?narocilo_id='.$this->narocilo['id'];
);
// Izvedemo placilo - stripe response
try{
$stripeResponse = $this->chargeAmountFromCard($cardDetails);
} }
catch (Exception $e){ else{
$response['error'] = 'ERROR! '.$e->getMessage(); $drupal_url_confirm = $site_url.'/d/narocilo/stripe?narocilo_id='.$this->narocilo['id'];
$drupal_url_cancel = $site_url.'/d/narocilo/stripe-cancel?narocilo_id='.$this->narocilo['id'];
}
// Ustvarimo checkout session
try {
$session = $this->stripeService->checkout->sessions->create([
'success_url' => $drupal_url_confirm,
'cancel_url' => $drupal_url_cancel,
'payment_method_types' => ['card'],
'mode' => 'payment',
'line_items' => [
[
'price_data' => array(
'currency' => 'EUR',
'product_data' => array(
'name' => '1KA naročnina (paket '.strtoupper($this->narocilo['package_name']). ' - '.$this->narocilo['trajanje'].' '.$months_string.')',
),
'unit_amount' => $cena_za_placilo * 100,
),
'quantity' => 1,
],
],
]);
// Dobimo id paypal narocila
$stripe_response['session_id'] = $session->id;
}
catch (HttpException $e) {
$response['error'] = $e->getMessage();
$response['success'] = false;
return $response; return $response;
} }
// Vstavimo plačilo v bazo
$sqlNarocilo = sisplet_query("INSERT INTO user_access_stripe_charge // Vstavimo stripe charge v bazo
(narocilo_id, description, price, amount_paid, status, balance_transaction, time) $sqlNarocilo = sisplet_query("INSERT INTO user_access_stripe_charge
(session_id, narocilo_id, price, time, status)
VALUES VALUES
('".$this->narocilo['id']."', '".$cardDetails['item_name']."', '".$cena_za_placilo."', '".($stripeResponse['amount'] / 100)."', '".$stripeResponse['status']."', '".$stripeResponse['balance_transaction']."', NOW()) ('".$stripe_response['session_id']."', '".$this->narocilo['id']."', '".$cena_za_placilo."', NOW(), 'CREATED')
"); ");
if (!$sqlNarocilo){ if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']); $response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false; $response['success'] = false;
return $response; return $response;
} }
$response = array();
// Placilo uspesno
if ($stripeResponse['amount_refunded'] == 0 $response['session_id'] = $stripe_response['session_id'];
&& empty($stripeResponse['failure_code'])
&& $stripeResponse['paid'] == 1 $response['success'] = true;
&& $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'];
$response['success'] = false;
}
return $response; return $response;
} }
private function chargeAmountFromCard($cardDetails){ // Zakljucimo placilo, ce je bilo placilo ok odobreno preko stripe s strani stranke - V DELU
public function stripeCheckoutSuccess(){
// Iz emaila in tokena ustvarimo stranko $response = array();
$customerDetailsAry = array(
'email' => $cardDetails['email'],
'source' => $cardDetails['token']
);
$customerResult = $this->addCustomer($customerDetailsAry);
$charge = new Charge();
// Napolnimo podatke za placilo // Preverimo plačilo v bazo
$cardDetailsAry = array( $sqlNarociloStripe = sisplet_query("SELECT session_id
'customer' => $customerResult->id, FROM user_access_stripe_charge
WHERE narocilo_id='".$this->narocilo['id']."'
");
if (!$sqlNarociloStripe){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
'amount' => $cardDetails['amount'], return $response;
'currency' => $cardDetails['currency_code'], }
'description' => $cardDetails['item_name'],
'metadata' => array( // Narocilo ne obstaja (ni v bazi stripe narocil)
'order_id' => $cardDetails['item_number'] if (mysqli_num_rows($sqlNarociloStripe) == 0){
) $response['error'] = 'ERROR! Stripe order session does not exist.';
); $response['success'] = false;
// Izvedemo "charge" return $response;
$result = $charge->create($cardDetailsAry); }
return $result->jsonSerialize(); $rowNarociloStripe = mysqli_fetch_array($sqlNarociloStripe);
// Preverimo, ce je bilo vse ok placano
try{
// Poklicemo paypal api kjer preverimo placilo narocila
$session = $this->stripeService->checkout->sessions->retrieve($rowNarociloStripe['session_id']);
}
catch(HttpException $e) {
$response['error'] = $e->getMessage();
$response['success'] = false;
return $response;
}
// Ce je session placan, posodobimo status narocila
if($session->payment_status == 'paid'){
$sqlNarocilo = sisplet_query("UPDATE user_access_stripe_charge
SET status='PAID'
WHERE transaction_id='".$paypal_response->result->id."'
");
if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
}
else{
$response['error'] = 'ERROR! SESSION IS NOT PAID!';
$response['success'] = false;
return $response;
}
// Nastavimo narocilo na placano, aktiviramo paket in vrnemo id narocila
$narocilo = new UserNarocila();
$payment_response = $narocilo->payNarocilo($this->narocilo['id']);
if($payment_response['success'] == true){
$response['racun'] = $payment_response['racun'];
$response['success'] = true;
}
else{
$response['error'] = $payment_response['error'];
$response['success'] = false;
}
$response['narocilo_id'] = $this->narocilo['id'];
$response['success'] = true;
return $response;
} }
private function addCustomer($customerDetailsAry){ // Preklicemo placilo, ce je bilo placilo preklicano preko stripe s strani stranke
public function stripeCheckoutCancel(){
$customer = new Customer();
$customerDetails = $customer->create($customerDetailsAry); $response = array();
return $customerDetails; // Posodobimo status narocila
$sqlNarocilo = sisplet_query("UPDATE user_access_stripe_charge
SET status='CANCELLED'
WHERE narocilo_id='".$this->narocilo['id']."'
");
if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
// Nastavimo status narocila na storniran
$sqlNarociloStatus = sisplet_query("UPDATE user_access_narocilo SET status='2' WHERE id='".$this->narocilo['id']."'");
if (!$sqlNarociloStatus){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
$response['success'] = true;
return $response;
} }
} }

View File

@ -9344,6 +9344,10 @@ ALTER TABLE user_access_narocilo CHANGE COLUMN podjetje_zavezanec podjetje_no_dd
UPDATE misc SET value='20.11.04' WHERE what="version"; UPDATE misc SET value='20.11.04' WHERE what="version";
ALTER TABLE user_access_stripe_charge ADD COLUMN session_id VARCHAR(100) NOT NULL DEFAULT '' AFTER id;
UPDATE misc SET value='20.11.11' WHERE what="version";
## RESTRICTION TABELE S FOREIGN KEYI VREDNOSTI, KI SE NIKOLI NE SMEJO POBRISATI ## RESTRICTION TABELE S FOREIGN KEYI VREDNOSTI, KI SE NIKOLI NE SMEJO POBRISATI
## Restrict brisanje sistemskih vrstic za srv_anketa ## Restrict brisanje sistemskih vrstic za srv_anketa
CREATE TABLE restrict_fk_srv_anketa ( CREATE TABLE restrict_fk_srv_anketa (