Reputation: 1336
I'm developing a Shopify's rails app. In development mode I'm trying to create a new recurring application charge with Shopify's Billing API by making this POST request from my index.html.erb:
(function($) {
var url = 'https://test.myshopify.com/admin/recurring_application_charges.json?';
$.ajax({
type: 'POST',
url: url,
crossDomain: true,
async: false,
contentType: "application/json",
dataType: 'jsonp',
data: {"recurring_application_charge": {"name": "Super Duper Plan", "test" : "true", "price": 10.0, "return_url": "http://super-duper.shopifyapps.com", "trial_days": 5}},
jsonp: 'whatever',
success: function(json) {
alert("success " + json);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
I get the following error in my browser's console:
Uncaught SyntaxError: Unexpected token : https://test.myshopify.com/admin/recurring_application_charges.json?whatever=jQuery1102022113484237343073_1393499741382&{%22recurring_application_charge%22:%20{%22name%22:%20%22Super%20Duper%20Plan%22,%20%22test%22%20:%20%22true%22,%20%22price%22:%2010.0,%20%22return_url%22:%20%22http://super-duper.shopifyapps.com%22,%20%22trial_days%22:%205%20}&_=1393499741383
and when I click on the error link it takes me to a page showing
{"recurring_application_charges":[]}
What am I doing wrong?
Upvotes: 0
Views: 1696
Reputation: 2966
To get access to the Shopify API you should specify:
X-Shopify-Access-Token
within the header of your requestapikey:password
within the URL your trying to access. For example https://apikey:password@hostname/admin/resource.json
However both the X-Shopify-Access-Token
and apikey
password
combination should be kept secret. So you shouldn't include those secrets into a public readable code (js
or html
). Those actions typically should be initiated from within a controller.
For example you could create a ChargeController
which calls the ShopifyAPI::RecurringCharge
method:
class ChargeController < ApplicationController
around_filter :shopify_session
# create new RecurringApplicationCharge
def create
ShopifyAPI::Base.activate_session(...)
ShopifyAPI::RecurringApplicationCharge.create(
name: "Super Duper Plan",
price: 10.0,
test: !Rails.env.production?,
return_url: charge_activate_url,
trial_days: 5
)
end
# activate RecurringApplicationCharge
def update
ShopifyAPI::Base.activate_session(...)
ShopifyAPI::RecurringApplicationCharge.find(params[:charge_id])
end
end
Upvotes: 3
Reputation: 2617
Have you tried removing the ' after data and adding the missing }.
Which gives you:
(function($) {
var url = 'https://test.myshopify.com/admin/recurring_application_charges.json?';
$.ajax({
type: 'POST',
url: url,
crossDomain: true,
async: false,
contentType: "application/json",
dataType: 'jsonp',
data: {"recurring_application_charge": {"name": "Super Duper Plan", "test" : "true", "price": 10.0, "return_url": "http://super-duper.shopifyapps.com", "trial_days": 5 }
},
jsonp: 'whatever',
success: function(json) {
alert("success " + json);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
Upvotes: 1