I'm having a hard time testing the Stripe Checkout from single-checkout-subscription on my local xampp server. So far, I have a Stripe account, created my test keys, product and prices, installed Stripe Cli and created a test webhook and added them all to the .env file in my server
DOMAIN="http://localhost/stripe/server/public/"
BASIC_PRICE_ID="price_xxx"
PRO_PRICE_ID="price_xxx"
STATIC_DIR="STATIC_DIR=../../client"
STRIPE_PUBLISHABLE_KEY="pk_test_xxx"
STRIPE_SECRET_KEY="sk_test_xxx"
STRIPE_WEBHOOK_SECRET="1mwhxxx"
But when I tested it at my local host: http://localhost/stripe/server/public/ I get the front end, but when I click on the button nothing happens. It doesn't even go to the prebuilt checkout page.
I check the console and the problems seems to becoming from my config.php
Two console errors appear on my script.js:
fetch("/config.php").then(function(json)
This is the routing:
My config.php > requires shared.php > requires '..vendor/autoload.php' & parses '../conifg/ini' > conifg.ini contains my test keys:
stripe_secret_key ="sk_test_444"
stripe_publishable_key = "pk_test_444"
stripe_webhook_secret = "1mw444"
domain = "http://localhost/stripe/server/public/"
basic_price_id = "price_444"
pro_price_id = "price_555"
Script.js inisde my server:
// Create a Checkout Session with the selected plan ID
var createCheckoutSession = function(priceId) {
return fetch("/create-checkout-session.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
priceId: priceId
})
}).then(function(result) {
return result.json();
});
};
// Handle any errors returned from Checkout
var handleResult = function(result) {
if (result.error) {
var displayError = document.getElementById("error-message");
displayError.textContent = result.error.message;
}
};
/* Get your Stripe publishable key to initialize Stripe.js */
fetch("/config.php")
.then(function(result) {
return result.json();
})
.then(function(json) {
var publishableKey = json.publishableKey;
var basicPriceId = json.basicPrice;
var proPriceId = json.proPrice;
var stripe = Stripe(publishableKey);
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("basic-plan-btn")
.addEventListener("click", function(evt) {
createCheckoutSession(basicPriceId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("pro-plan-btn")
.addEventListener("click", function(evt) {
createCheckoutSession(proPriceId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
});
I'm well versed in HTML, Bootstrap, CSS, and some PHP and JavaScript, but I can't seem to follow the difficult directions on how to get the Stripe Subscription Checkout working. Can someone please point me in the right direction or tell me how to fix my code. I'm on a tight deadline with the holidays.
First of all, there is a 404 error because the file was never there. In localhost or all servers, if you put a / before the file name it will automatically become after the host so /config.php will become http://localhost/config.php. To prevent this error, you should use ./
And the unexpected token < means the server is returning the 404 document.
In short, put a dot before the file name as I am assuming that this project is not in the root directory. (Means that the project is at http://localhost/projectName)
// Create a Checkout Session with the selected plan ID
var createCheckoutSession = function(priceId) {
return fetch("./create-checkout-session.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
priceId: priceId
})
}).then(function(result) {
return result.json();
});
};
and
/* Get your Stripe publishable key to initialize Stripe.js */
fetch("./config.php")
.then(function(result) {
return result.json();
})
.then(function(json) {
var publishableKey = json.publishableKey;
var basicPriceId = json.basicPrice;
var proPriceId = json.proPrice;
var stripe = Stripe(publishableKey);
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("basic-plan-btn")
.addEventListener("click", function(evt) {
createCheckoutSession(basicPriceId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("pro-plan-btn")
.addEventListener("click", function(evt) {
createCheckoutSession(proPriceId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
});
Related
I am creating a application using Laravel 5.4 that require PayPal to do payments ,when i study the PayPal integration documentation i found that the REST API need 2 controller in server side name "/demo/checkout/api/paypal/payment/create/" and "/demo/checkout/api/paypal/payment/execute/", but the documentation of PayPal is blur, does anyone know what is it and the example of it?
Here is my code in frontend:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $("meta[name='csrf-token']").attr("content")
}
});
paypal.Button.render({
env: 'sandbox', // sandbox | production
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function() {
// Set up a url on your server to create the payment
var CREATE_URL = '{{route('paypal.createpayment')}}';
// Make a call to your server to set up the payment
return paypal.request({
method: 'post',
url: CREATE_URL,
headers: {
'x-csrf-token': $("meta[name='csrf-token']").attr("content")
}
}).then(function(res) {
return res.paymentID;
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
// Set up a url on your server to execute the payment
var EXECUTE_URL = '/demo/checkout/api/paypal/payment/execute/';
// Set up the data you need to pass to your server
var data = {
paymentID: data.paymentID,
payerID: data.payerID
};
// Make a call to your server to execute the payment
return paypal.request.post(EXECUTE_URL, data)
.then(function (res) {
window.alert('Payment Complete!');
});
}
}, '#paypal-button-container');
The documentation is on this page with example in multiple programming language : https://developer.paypal.com/docs/api/quickstart/payments/#define-payment
So on the first call to your server, you need to define the payment and create the payment.
On the second call to your server (called inside the onAuthorize function) you need to execute the payment.
I'm trying out the Stripe API & functions, but I am twisting my head around this one issue - how to submit a form after the (test) payment is successful?
Here's my current code:
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_mykey',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'sv',
token: function(token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
stripe_spots = document.getElementById("spots").value;
stripe_total = (stripe_spots) * 70;
// Open Checkout with further options:
handler.open({
name: 'Revy!',
description: stripe_spots + " platser",
zipCode: true,
currency: 'sek',
amount: stripe_total * 100
});
e.preventDefault();
});
// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>
Where or when am I supposed to submit the form?
Thanks!
You submit the form using the token callback function update your handle like so
var handler = StripeCheckout.configure({
key: 'pk_test_mykey',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'sv',
token: function (token) {
$.ajax({
type: 'POST',
url: '/checkout.php',
data: {stripeToken: token.id, stripeEmail: token.email},
success: function (data) {
//handle success response here
},
error: function(data){
//handle error response here
}
});
}
});
After retrieving Stripe TOKEN you should send your data to your server including TOKEN to make Stripe Charge or create Subscription
Here is an example tutorial to create Customer for Subscription
Editing my earlier answer, because I've got it working so I can share the code snippet. (Was struggling with the same problem when I discovered this.)
Here is my solution. It works. This assumes your next page is named "PaymentComplete.php". Edit accordingly.
token: function(token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
var form = document.createElement("form");
var tokenPassed = document.createElement("input");
form.method = "POST";
form.action = "PaymentComplete.php";
tokenPassed.value = token.id;
tokenPassed.name = "token";
form.appendChild(tokenPassed);
document.body.appendChild(form);
form.submit();
}
There may be a better way of doing it, but this works for me.
Source: creating and submitting a form with javascript
I am working an an Ionic app where I implement native Facebook login (followed this tutorial -> https://ionicthemes.com/tutorials/about/native-facebook-login-with-ionic-framework). As you can see the Facebook data now gets stored in local storage. I need to save this data in my MySql database.
I got this to work without any issues. Now I want to store the Facebook user data to my MySql database.
Basically I am not sure where to place my http request to pass the data along to my database or how to even do it code wise.
I should mention that I have a backend already setup (which is coded with bootstrap, html, css, js php and mysql).
So the url for my users would be this: http://www.xxxxx.com/user.php
Part of my controller code:
app.controller('LoginCtrl', function($scope, $state, $q, UserService, $ionicLoading) {
// This is the success callback from the login method
var fbLoginSuccess = function(response) {
if (!response.authResponse){
fbLoginError("Cannot find the authResponse");
return;
}
var authResponse = response.authResponse;
getFacebookProfileInfo(authResponse)
.then(function(profileInfo) {
// For the purpose of this example I will store user data on local storage
UserService.setUser({
authResponse: authResponse,
userID: profileInfo.id,
name: profileInfo.name,
email: profileInfo.email,
picture : "http://graph.facebook.com/" + authResponse.userID + "/picture?type=large"
});
$ionicLoading.hide();
$state.go('app.dashboard');
}, function(fail){
// Fail get profile info
console.log('profile info fail', fail);
});
};
// This is the fail callback from the login method
var fbLoginError = function(error){
console.log('fbLoginError', error);
$ionicLoading.hide();
};
// This method is to get the user profile info from the facebook api
var getFacebookProfileInfo = function (authResponse) {
var info = $q.defer();
facebookConnectPlugin.api('/me?fields=email,name&access_token=' + authResponse.accessToken, null,
function (response) {
console.log('logging facebook response',response);
info.resolve(response);
},
function (response) {
console.log(response);
info.reject(response);
}
);
return info.promise;
};
//This method is executed when the user press the "Login with facebook" button
$scope.facebookSignIn = function() {
facebookConnectPlugin.getLoginStatus(function(success){
if(success.status === 'connected'){
// The user is logged in and has authenticated your app, and response.authResponse supplies
// the user's ID, a valid access token, a signed request, and the time the access token
// and signed request each expire
console.log('getLoginStatus', success.status);
// Check if we have our user saved
var user = UserService.getUser('facebook');
if(!user.userID){
getFacebookProfileInfo(success.authResponse)
.then(function(profileInfo) {
// For the purpose of this example I will store user data on local storage
UserService.setUser({
authResponse: success.authResponse,
userID: profileInfo.id,
name: profileInfo.name,
email: profileInfo.email,
picture : "http://graph.facebook.com/" + success.authResponse.userID + "/picture?type=large"
});
$state.go('app.dashboard');
}, function(fail){
// Fail get profile info
console.log('profile info fail', fail);
});
}else{
$state.go('app.dashboard');
}
} else {
// If (success.status === 'not_authorized') the user is logged in to Facebook,
// but has not authenticated your app
// Else the person is not logged into Facebook,
// so we're not sure if they are logged into this app or not.
console.log('getLoginStatus', success.status);
$ionicLoading.show({
template: 'Logging in...'
});
// Ask the permissions you need. You can learn more about
// FB permissions here: https://developers.facebook.com/docs/facebook-login/permissions/v2.4
facebookConnectPlugin.login(['email', 'public_profile'], fbLoginSuccess, fbLoginError);
}
});
};
})
My service.js code (local storage)
angular.module('Challenger.services', [])
.service('UserService', function() {
// For the purpose of this example I will store user data on ionic local storage but you should save it on a database
var setUser = function(user_data) {
window.localStorage.starter_facebook_user = JSON.stringify(user_data);
};
var getUser = function(){
return JSON.parse(window.localStorage.starter_facebook_user || '{}');
};
return {
getUser: getUser,
setUser: setUser
};
});
My recommendation is to simply use a JSON ajax PUT or POST from JavaScript. For example, assuming a backend host of example.com
Add a CSP to the Ionic HTML such as:
<meta http-equiv="Content-Security-Policy" content="default-src http://example.com; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
Add the domain to the whitelist in the Cordova config.xml:
<access origin="http://example.com" />
Then you can call PHP from JavaScript with ajax in your angular controller (I used jQuery here but you can use any JavaScript ajax library):
var data = {
authResponse: authResponse,
userID: profileInfo.id,
name: profileInfo.name,
email: profileInfo.email,
picture : "http://graph.facebook.com/" + authResponse.userID + "/picture?type=large"
};
$.post( "http://example.com/login.php", data, function(returnData, status) {
console.log('PHP returned HTTP status code', status);
});
Finally, on the PHP side — e.g. login.php — access the post data with $_POST['userId'], $_POST['email'], etc.
I guess that you have all your codes ready, but just not sure where is the best place to locate your codes. There is nice linker where has clear instruction about how to layout your php project structure: http://davidshariff.com/blog/php-project-structure/, hope this can give a kind of help.
i have my php point exposing these endpoints for example
[GET] /api/news --> [return] [ {id:1,title:'news1'},{id:1,title:'news1'} ];
[POST] /api/news?title=<string> --> [return] {id:NEW_ID,title:$title};
and my frontend webapp use angular 1 to get news.
//to get all news
$http.get('http://server.com/api/news').then(function(res){
$scope.news = res.data;
});
//to post a new news
$http.post('http://server.com/api/news',{title:'news3'}).then(function(res){
$scope.news.push(res.data);
});
this would work fine, except that i will have to pool every interval to get any new news, so i thought something like socket.io can help.
I'm tryying to achive.
user connect to io
io send all news once user connects by consuming my php api
when user post new, he send it to io server, and io forward the post request to my rest api and return the result to all connected users.
so my change to front end can be something like
var socket = io('http://localhost:3700');
socket.on('news', function (data) {
$timeout(()=>{$scope.news = data;});
});
socket.on('news_posted', function (data) {
$timeout(()=>{$scope.news.push(data);});
});
$scope.post = function(title){
socket.emit('post_news', { title: title });
}
now what i have no clue is how to make such simple server ?
var express = require("express");
var app = express();
var port = 3700;
var io = require('socket.io').listen(app.listen(port));
//setting CORS
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8100");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key,auth-client,auth-uid,auth-token');
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});
io.sockets.on('connection', function (socket) {
console.log('a user connected');
///BLOCK 1
///GET ALL POSTS FROM API AND EMIT news event BACK TO USER
///
socket.on('new_post', function(msg){
///BLOCK 2
///POST title TO API AND EMIT BACK news_posted WITH THE RESPONSE
});
});
This is on Internet Explorer only.
I have a factory that provides notifications for my app.
The input for those notifications is a file (qc.sta) that is created automatically by a third party software and is dropped in a specific folder.
Whenever that file exists, a new notification is added to the app.
My mechanism would be to get the data from that qc.sta file and then delete it after collecting the data.
To delete a file I need to execute a PHP that deletes the file, but this seems to have to be done inside a $http.get, so I end up with an $http.get inside another $http.get.
What happens is that the notifications (triggered on a $interval inside the controller) keep popping up continuously, despite the file being deleted.
My factory is as below:
myApp.factory("qcSTA", function ($http, $q) {
return {
apiPath: "data/qc.sta?rnd=" + new Date().getTime(), //randomizing request to prevent caching
getNotifications: function () {
//Creating a deferred object
var deferred = $q.defer();
//Calling Web API to fetch notifications
$http.get(this.apiPath, { headers: { 'Cache-Control': 'no-cache'} })
.success(function (response) {
//Passing data to deferred's resolve function on successful completion
//Does whatever needs to be done to the response before passing it on
function deleteFile() {
$http.get("deleteFile.php");
return false
};
deleteFile();
deferred.resolve(JSON.parse('{"item" : "' + response.itemNumber + '", "description" : "' + response.desc + '"}'));
})
.error(function () {
//Sending a friendly error message in case of failure
deferred.reject("An error occured while fetching items");
});
//Returning the promise object
return deferred.promise;
}
}
});
And my controller:
myApp.controller("notificationController", function ($scope, $location, $interval, $q, qcSTA) {
function getNotifications() {
qcSTA.getNotifications()
.then(function (data) {
$scope.notifications.push(data);
},
function (errorMessage) {
$scope.error = errorMessage
});
}
}
How can I ensure the notification only pops up once rather than going in circles?
Could this be cache related?
If I refresh the browser, it won't get more notifications, it's like it finally detects the qc.sta file isn't there anymore.