-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfulfillmentController
More file actions
68 lines (51 loc) · 2.39 KB
/
fulfillmentController
File metadata and controls
68 lines (51 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Newtonsoft.Json;
using RestSharp;
using ShopifyApp05.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ShopifyApp05.Controllers
{
public class fulfillmentController : Controller
{
private string apiKey = AppSettings.ShopifyApiKey;
private string secretKey = AppSettings.ShopifySecretKey;
private string appUrl = AppSettings.AppUrl;
// GET: fulfillment
public ActionResult install(string shop, string signature, string timestamp)
{
string r = string.Format("https://{0}/admin/oauth/authorize?client_id={1}&scope=read_fulfillments,write_fulfillments&redirect_uri=https://{2}/fulfillment/auth", shop, apiKey, appUrl);
return Redirect(r);
}
public ActionResult auth(string shop, string code)
{
string u = string.Format("https://{0}/admin/oauth/access_token", shop);
var client = new RestClient(u);
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/x-www-form-urlencoded", "client_id=" + apiKey + "&client_secret=" + secretKey + "&code=" + code, ParameterType.RequestBody);
var response = client.Execute(request);
var r = JsonConvert.DeserializeObject<dynamic>(response.Content);
var access = r.access_token;
//Part 5
//create a un-install web hook
//you want to know when customers delete your app from their shop
string unInstallUrlCallback = "https://549653d4.ngrok.io/fulfillment/uninstall";
string shopAdmin = string.Format("https://{0}/admin/", shop);
var webHook = new WebHookBucket();
webHook.Whook = new WebHook { Format = "json", Topic = "app/uninstalled", Address = unInstallUrlCallback };
CreateUninstallHook(shopAdmin, "webhooks.json", Method.POST, (string)accessToken, webHook);
return View();
}
[HttpPost]
public ActionResult uninstall()
{
var req = Request.InputStream;
var json = new StreamReader(req).ReadToEnd();
return new HttpStatusCodeResult(200);
}
}
}