Drupal: "Stripe za Drupal v3"

This commit is contained in:
Robert 2020-11-16 15:36:11 +01:00
parent a5a53a324d
commit 466ae22379
5 changed files with 252 additions and 135 deletions

View File

@ -47,6 +47,28 @@ function nakupovanje_1ka_menu()
],
];
//Stripe uspešno plačilo
$items['narocilo/stripe'] = [
'title' => 'Uspešno plačilo preko Stripe sistema', //page title
'description' => 'Plačilo je bilo opravljeno preko Stripe sistema',
'page callback' => 'nakupovanje_1ka_stripe_return', //callback function which is invoked when menu item is called.
'access callback' => true, //any user can access this page
'page arguments' => [
1,
],
];
//Stripe cancel
$items['narocilo/stripe-cancel'] = [
'title' => 'Preklicano plačilo preko Stripe ssistema', //page title
'description' => 'Uporabnik je preklical plačilo',
'page callback' => 'nakupovanje_1ka_stripe_cancel', //callback function which is invoked when menu item is called.
'access callback' => true, //any user can access this page
'page arguments' => [
1,
],
];
$items['narocilo-pregled/%/pdf'] = [
'title' => 'Uspešno opravljen nakup', //page title
@ -127,6 +149,24 @@ function nakupovanje_1ka_menu()
],
];
$items['stripe-purchase/success'] = [
'title' => 'Successful payment via Stripe', //page title
'page callback' => 'nakupovanje_1ka_stripe_return', //callback function which is invoked when menu item is called.
'access callback' => true, //any user can access this page
'page arguments' => [
1,
],
];
$items['stripe-purchase/cancel'] = [
'title' => 'Payment canceled via Stripe', //page title
'page callback' => 'nakupovanje_1ka_stripe_cancel', //callback function which is invoked when menu item is called.
'access callback' => true, //any user can access this page
'page arguments' => [
1,
],
];
$items['purchase/%/package'] = [
'title' => 'Purchase of the selected package', //page title
'page callback' => 'nakupovanje_1ka_kosarica', //callback function which is invoked when menu item is called.
@ -270,6 +310,17 @@ function nakupovanje_1ka_paypal_cancel()
return theme('nakupovanje_1ka_paypal_cancel');
}
function nakupovanje_1ka_stripe_return()
{
return theme('nakupovanje_1ka_stripe_return');
}
function nakupovanje_1ka_stripe_cancel()
{
return theme('nakupovanje_1ka_stripe_cancel');
}
function cenik_paketov_1ka_zasebni_uporabniki()
{

View File

@ -765,12 +765,6 @@
var narocilo = jQuery('#izbria-nacina-placila-1ka [name="narocilo_id"]').val();
});
//Pri menjavi placila prikazemo ustrezno polje
jQuery('[name="payment_method"]').on('change', function () {
//Plačilo preko Stripa
preverimoZaVrstoPlacila();
});
// Uspešno oddan predračun ali plačilo preko spleta
if (jQuery('.narocilo-1ka-pdf').length > 0) {
if (document.cookie.indexOf('pdf=') < 1) {
@ -821,6 +815,39 @@
});
}
//Stripe plačilo
if (
(window.location.pathname.indexOf('/narocilo/stripe-cancel') > -1 && location.search.split('narocilo_id=').length > 1) ||
(window.location.pathname.indexOf('/stripe-purchase/cancel') > -1 && location.search.split('narocilo_id=').length > 1)
) {
var parametri = window.location.search.substr(1);
jQuery.post('/d/nakupovanje-api/cancel_narocilo_stripe', parametri).done(function (val) {
val = JSON.parse(val);
if (val.error) {
return jQuery('.procesiram-narocilo').text(val.error);
}
});
}
if (
(window.location.href.indexOf('/narocilo/stripe?narocilo_id') > -1 && location.search.split('narocilo_id=').length > 1) ||
(window.location.href.indexOf('/stripe-purchase/success?narocilo_id') > -1 && location.search.split('narocilo_id=').length > 1)
){
var parametri = window.location.search.substr(1);
jQuery.post('/d/nakupovanje-api/stripe_checkout_success', parametri).done(function (val) {
val = JSON.parse(val);
if (val.error) {
return jQuery('.procesiram-narocilo').text(val.error);
}
});
}
// jQuery('.accordion-head').on('click', function(){
// var st = jQuery(this).attr("data-open");
//
@ -871,7 +898,6 @@
//Nastavimo prevzeto plačevanje s karticam
jQuery('[name="payment_method"][value="3"]').prop('checked', true);
preverimoZaVrstoPlacila();
posodobiCenoPaketa();
}
@ -1215,11 +1241,7 @@ function oddajNarociloNaStreznik() {
return setTimeout(function(){
jQuery('.pregled-narocila').show();
if(jQuery('[name="payment_method"]:checked').val() == 3){
jQuery('#oddaj-stripe').show();
} else {
jQuery('#oddaj-narocilo').show();
}
jQuery('.submit-btn.naprej').show();
jQuery('#nazaj-na-nakup-1ka').show();
jQuery('.procesiram-narocilo').hide();
@ -1250,15 +1272,21 @@ function oddajNarociloNaStreznik() {
urlRedirect = val.paypal_link;
} else if (paymentMethod > 1) {
document.cookie = "pdf=" + val.racun + ";expires=" + cas + ";path=/";
} else if (paymentMethod == 3) {
// Nastavimo Stripe instanco
var stripe = Stripe(jQuery('.placilo-kartica #stripe-token').val());
// Glede na jezik preusmerimo na slovensko oz. angleško verzijo
var urlRedirect = "/d/narocilo-pregled/racun/pdf";
if (paymentLang == 'en') {
urlRedirect = "/d/en/purchase/invoice/pdf";
}
return stripe.redirectToCheckout({ sessionId: val.session_id });
}
// else if (paymentMethod > 1) {
// document.cookie = "pdf=" + val.racun + ";expires=" + cas + ";path=/";
//
// // Glede na jezik preusmerimo na slovensko oz. angleško verzijo
// var urlRedirect = "/d/narocilo-pregled/racun/pdf";
// if (paymentLang == 'en') {
// urlRedirect = "/d/en/purchase/invoice/pdf";
// }
// }
window.location = urlRedirect;
}
@ -1267,22 +1295,22 @@ function oddajNarociloNaStreznik() {
});
}
function preverimoZaVrstoPlacila() {
if (jQuery('[name="payment_method"]:checked').val() == 3) {
jQuery('.placilo-kartica').show();
jQuery('#oddaj-stripe').show();
jQuery('#oddaj-narocilo').hide();
} else {
jQuery('.placilo-kartica').hide();
jQuery('#oddaj-stripe').hide();
jQuery('#oddaj-narocilo').show();
}
}
// function preverimoZaVrstoPlacila() {
//
// if (jQuery('[name="payment_method"]:checked').val() == 3) {
//
// jQuery('.placilo-kartica').show();
// jQuery('#oddaj-stripe').show();
// jQuery('#oddaj-narocilo').hide();
//
// } else {
//
// jQuery('.placilo-kartica').hide();
// jQuery('#oddaj-stripe').hide();
// jQuery('#oddaj-narocilo').show();
//
// }
// }
function posodobiCenoPaketa() {
var paket = jQuery('[name="package_id"]:checked').val();
@ -1335,7 +1363,6 @@ function posodobiCenoPaketa() {
if (response['final'] == 0) {
jQuery('.podatki-za-placilo-racuna').hide();
jQuery('[name="payment_method"][value="1"]').prop('checked', true);
preverimoZaVrstoPlacila();
}
}
});
@ -1347,105 +1374,85 @@ function lang() {
}
//Stripe payment
function cardValidation() {
var valid = true;
jQuery('.error-field').removeClass('error-field');
var name = jQuery('.placilo-kartica #stripe-name');
var email = jQuery('#narocilo-paketa-podatki [name="email"]');
var cardNumber = jQuery('.placilo-kartica #stripe-card-number');
var month = jQuery('.placilo-kartica #stripe-month');
var year = jQuery('.placilo-kartica #stripe-year');
var cvc = jQuery('.placilo-kartica #stripe-cvc');
jQuery("#stripe-error-message").html("").hide();
if (name.val().trim() == "") {
name.addClass('error-field');
valid = false;
}
if (email.val().trim() == "") {
valid = false;
}
if (cardNumber.val().trim() == "") {
cardNumber.addClass('error-field');
valid = false;
}
if (month.val().trim() == "") {
month.addClass('error-field');
valid = false;
}
if (year.val().trim() == "") {
year.addClass('error-field');
valid = false;
}
if (cvc.val().trim() == "") {
cvc.addClass('error-field');
valid = false;
}
if (valid == false) {
var opozorilo = "Vsa polja so obvezna!";
if (lang() == 'en') {
opozorilo = "All Fields are required!";
}
jQuery("#stripe-error-message").html(opozorilo).show();
}
return valid;
}
// function cardValidation() {
// var valid = true;
//
// jQuery('.error-field').removeClass('error-field');
//
// var name = jQuery('.placilo-kartica #stripe-name');
// var email = jQuery('#narocilo-paketa-podatki [name="email"]');
// var cardNumber = jQuery('.placilo-kartica #stripe-card-number');
// var month = jQuery('.placilo-kartica #stripe-month');
// var year = jQuery('.placilo-kartica #stripe-year');
// var cvc = jQuery('.placilo-kartica #stripe-cvc');
//
// jQuery("#stripe-error-message").html("").hide();
//
// if (name.val().trim() == "") {
// name.addClass('error-field');
// valid = false;
// }
// if (email.val().trim() == "") {
// valid = false;
// }
//
// if (cardNumber.val().trim() == "") {
// cardNumber.addClass('error-field');
// valid = false;
// }
//
// if (month.val().trim() == "") {
// month.addClass('error-field');
// valid = false;
// }
// if (year.val().trim() == "") {
// year.addClass('error-field');
// valid = false;
// }
// if (cvc.val().trim() == "") {
// cvc.addClass('error-field');
// valid = false;
// }
//
// if (valid == false) {
// var opozorilo = "Vsa polja so obvezna!";
// if (lang() == 'en') {
// opozorilo = "All Fields are required!";
// }
// jQuery("#stripe-error-message").html(opozorilo).show();
// }
//
// return valid;
// }
function stripePay(e) {
e.preventDefault();
// V kolikor je omenjeno polje izpolnjeno potem gre za robota in preusmerimo na začetno stran
if (jQuery('[name="stripe-robotsko-narocilo"]').val().length > 0) {
return window.location = '/';
}
var valid = cardValidation();
if (valid == true) {
jQuery(".submit-btn").hide();
jQuery("#nazaj-na-nakup-1ka").hide();
// jQuery( "#loader" ).css("display", "inline-block");
Stripe.setPublishableKey(jQuery('.placilo-kartica #stripe-token').val());
Stripe.createToken({
number: jQuery('.placilo-kartica #stripe-card-number').val(),
cvc: jQuery('.placilo-kartica #stripe-cvc').val(),
exp_month: jQuery('.placilo-kartica #stripe-month').val(),
exp_year: jQuery('.placilo-kartica #stripe-year').val()
}, stripeResponseHandler);
//submit from callback
return false;
}
}
// function stripePay(narociloId) {
// var narociloId = narociloId || 0;
//
// // Create an instance of the Stripe object with your publishable API key
// var stripe = Stripe(jQuery('.placilo-kartica #stripe-token').val());
//
// return stripe.redirectToCheckout({ sessionId: narociloId });
// }
//callback to handle the response from stripe
function stripeResponseHandler(status, response) {
if (response.error) {
//enable the submit button
jQuery('#oddaj-stripe').show();
jQuery("#loader").css("display", "none");
//display the errors on the form
jQuery("#stripe-error-message").html(response.error.message).show();
} else {
//get token id
var token = response['id'];
//insert the token into the form
jQuery('#narocilo-paketa-podatki').append("<input type='hidden' name='stripe_id' value='" + token + "' />");
//submit form to the server
oddajNarociloNaStreznik();
}
}
// function stripeResponseHandler(status, response) {
// if (response.error) {
// //enable the submit button
// jQuery('#oddaj-stripe').show();
//
// jQuery("#loader").css("display", "none");
//
// //display the errors on the form
// jQuery("#stripe-error-message").html(response.error.message).show();
// } else {
// //get token id
// var token = response['id'];
//
// //insert the token into the form
// jQuery('#narocilo-paketa-podatki').append("<input type='hidden' name='stripe_id' value='" + token + "' />");
//
// //submit form to the server
// oddajNarociloNaStreznik();
// }
// }

View File

@ -150,7 +150,7 @@ function pro1ka_preprocess_html(&$variables) {
// JS external
drupal_add_js('https://www.google.com/recaptcha/api.js', array('group' => JS_THEME));
drupal_add_js('https://js.stripe.com/v2/', array('group' => JS_THEME));
drupal_add_js('https://js.stripe.com/v3/', array('group' => JS_THEME));
}

View File

@ -0,0 +1,25 @@
<?php global $language; ?>
<div id="narocilo-1ka-cancel-paypal">
<div style="padding: 2rem 0;">
<p>
<?php if ($language->language == 'sl'): ?>
Vaše naročilo je preklicano. Če želite ponovno oddati naročilo (spremeniti način plačila ali izbrati drug paket), kliknite na <a href="/d/sl/cenik/zasebni-paketi">povezavo</a>.
<?php else: ?>
Your order has been canceled. Click the <a href="/d/en/price-list/private-packages">link</a> to resubmit your order (change your payment method or choose another package).
<?php endif; ?>
</p>
</div>
<div class="w-clearfix bottom-buttons">
<div class="col" style="float: left;">
<?php if ($language->language == 'sl'): ?>
<a href="/d/sl" class="w-link-button">Vrni se na naslovno stran</a>
<?php else: ?>
<a href="/d/en" class="w-link-button">Return to home page</a>
<?php endif; ?>
</div>
</div>
</div>

View File

@ -0,0 +1,34 @@
<?php global $language; ?>
<div id="narocilo-1ka-return-paypal">
<div style="padding: 2rem 0;">
<p>
<?php if ($language->language == 'sl'): ?>
Zahvaljujemo se vam za vaš nakup preko Stripe plačilnea sistema. Vaš novi paket je aktiven in ga lahko pričnete uporabljati.
<?php else: ?>
Thank you for your purchase using Strpe! Your new package is now active and you can use it.
<?php endif; ?>
</p>
</div>
<div class="w-clearfix bottom-buttons">
<div class="col" style="float: right;">
<?php if ($language->language == 'sl'): ?>
<a href="/admin/survey" class="submit-btn w-button">Ustvari anketo</a>
<?php else: ?>
<a href="/admin/survey/index.php?lang=en" class="submit-btn w-button">Create a survey</a>
<?php endif; ?>
</div>
<div class="col" style="float: left;">
<?php if ($language->language == 'sl'): ?>
<a href="/" class="w-link-button">Vrni se na naslovno stran</a>
<?php else: ?>
<a href="/d/en" class="w-link-button">Return to home page</a>
<?php endif; ?>
</div>
</div>
</div>