diff --git a/frontend/payments/classes/class.ApiNarocilaController.php b/frontend/payments/classes/class.ApiNarocilaController.php index c84d7a5b5..018daf81b 100644 --- a/frontend/payments/classes/class.ApiNarocilaController.php +++ b/frontend/payments/classes/class.ApiNarocilaController.php @@ -303,8 +303,35 @@ class ApiNarocilaController{ $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{ - $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; } diff --git a/frontend/payments/classes/class.UserNarocila.php b/frontend/payments/classes/class.UserNarocila.php index 895fdb99d..b91b1db97 100644 --- a/frontend/payments/classes/class.UserNarocila.php +++ b/frontend/payments/classes/class.UserNarocila.php @@ -792,42 +792,22 @@ class UserNarocila{ $response = array(); - $token = isset($narocilo_data['stripe_id']) ? $narocilo_data['stripe_id'] : ''; - if($token == ''){ - $response['error'] = 'ERROR! Missing token.'; - $response['success'] = false; - - return $response; - } - - // Inicializiramo stripe + // Inicializiramo paypal $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 - if($stripe_response['success'] == true){ - - $response['stripe_note'] = $stripe_response['stripe_note']; - - $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; - } + if($stripe_response['success'] == true){ + $response['session_id'] = $stripe_response['session_id']; + $response['success'] = true; } else{ $response['error'] = $stripe_response['error']; $response['success'] = false; } - $response['narocilo_id'] = $narocilo_id; - return $response; } diff --git a/frontend/payments/classes/class.UserNarocilaStripe.php b/frontend/payments/classes/class.UserNarocilaStripe.php index 2469c5a8f..bfc78d2c9 100644 --- a/frontend/payments/classes/class.UserNarocilaStripe.php +++ b/frontend/payments/classes/class.UserNarocilaStripe.php @@ -12,6 +12,8 @@ use \Stripe\Customer; use \Stripe\ApiOperations\Create; use \Stripe\Charge; +use \Stripe\StripeClient; + class UserNarocilaStripe{ @@ -22,16 +24,12 @@ class UserNarocilaStripe{ private $stripeService; - public function __construct($narocilo_id ){ + 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); + $this->stripeService = new \Stripe\StripeClient($stripe_secret); if($narocilo_id > 0){ @@ -52,9 +50,14 @@ class UserNarocilaStripe{ } - // Placamo narocilo s kreditno kartico preko stripa - public function stripePayment($token){ + // Ustvarimo session za placilo v stripe - V DELU + public function stripeCreateSession(){ + global $site_url; + global $lang; + $response = array(); + + $UA = new UserNarocila(); $cena = $UA->getPrice($this->narocilo['package_name'], $this->narocilo['trajanje'], $this->narocilo['discount']); @@ -78,95 +81,184 @@ class UserNarocilaStripe{ $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 - try{ - $stripeResponse = $this->chargeAmountFromCard($cardDetails); + // URL po potrditvi oz preklicu + if($lang['id'] == '2'){ + $drupal_url_confirm = $site_url.'/d/en/stripe-purchase/success?narocilo_id='.$this->narocilo['id']; + $drupal_url_cancel = $site_url.'/d/en/stripe-purchase/cancel?narocilo_id='.$this->narocilo['id']; } - catch (Exception $e){ - $response['error'] = 'ERROR! '.$e->getMessage(); + else{ + $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; - } + } - // Vstavimo plačilo v bazo - $sqlNarocilo = sisplet_query("INSERT INTO user_access_stripe_charge - (narocilo_id, description, price, amount_paid, status, balance_transaction, time) + + // Vstavimo stripe charge v bazo + $sqlNarocilo = sisplet_query("INSERT INTO user_access_stripe_charge + (session_id, narocilo_id, price, time, status) 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){ $response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']); $response['success'] = false; - + 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']; - $response['success'] = false; - } + + $response['session_id'] = $stripe_response['session_id']; + + $response['success'] = true; 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 - $customerDetailsAry = array( - 'email' => $cardDetails['email'], - 'source' => $cardDetails['token'] - ); - $customerResult = $this->addCustomer($customerDetailsAry); - $charge = new Charge(); + $response = array(); - // Napolnimo podatke za placilo - $cardDetailsAry = array( - 'customer' => $customerResult->id, + // Preverimo plačilo v bazo + $sqlNarociloStripe = sisplet_query("SELECT session_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'], - 'currency' => $cardDetails['currency_code'], - 'description' => $cardDetails['item_name'], + return $response; + } - 'metadata' => array( - 'order_id' => $cardDetails['item_number'] - ) - ); + // Narocilo ne obstaja (ni v bazi stripe narocil) + if (mysqli_num_rows($sqlNarociloStripe) == 0){ + $response['error'] = 'ERROR! Stripe order session does not exist.'; + $response['success'] = false; - // Izvedemo "charge" - $result = $charge->create($cardDetailsAry); + return $response; + } - 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){ - - $customer = new Customer(); - $customerDetails = $customer->create($customerDetailsAry); - - return $customerDetails; + // Preklicemo placilo, ce je bilo placilo preklicano preko stripe s strani stranke + public function stripeCheckoutCancel(){ + + $response = array(); + + // 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; } } \ No newline at end of file diff --git a/sql/update2.sql b/sql/update2.sql index c822de603..6b24b433b 100644 --- a/sql/update2.sql +++ b/sql/update2.sql @@ -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"; +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 ## Restrict brisanje sistemskih vrstic za srv_anketa CREATE TABLE restrict_fk_srv_anketa (