Reputation: 77
We are trying to create an affiliate URL, so that we can track users when they install our app on their Shopify store. We provide a URL, https://ourappname.com/services/convert?ref=xxxx, and when user clicks this url, we set browser cookies and then they are redirected to https://apps.shopify.com/appname.
@GetMapping("/convert")
public void setCookieAndRedirect(HttpServletRequest request,HttpServletResponse response,
@RequestParam(value ="ref", required = false) String ref) {
System.out.println(" REceived ref code " + ref +" ::: redirecting to appstore.");
Cookie cookie = new Cookie("RefCookie", ref);
try {
//add cookie to response
response.addCookie(cookie);
cookie.setPath("/");
cookie.setMaxAge(60 * 60 * 24 * 365 * 10);
URL urlToRedirect = new URL("https://apps.shopify.com/appname");
cookie.setDomain(urlToRedirect.getHost());
response.addCookie(cookie);
response.sendRedirect("https://apps.shopify.com/appname");
}catch(Exception ex) {
ex.printStackTrace();
}
}
Our plan was - once they install the app and we get the control in our server, we are checking the browser cookies and if the ref is saved with in our database, we provide a special plan for the user.
@GetMapping("/checkForDiscounts")
public ResponseEntity<String> checkForDiscounts(HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for(Cookie ck : cookies) {
System.out.println(" Cookie Name = "+ck.getName()+" :::: Value = "+ck.getValue());
if("RefCookie".equals(ck.getName())) {
//provide special discount plan for user
}
}
}else {
System.out.println(" Cookie Not Found");
}
return new ResponseEntity<String>("success", HttpStatus.OK);
}
But unfortunately, cookies will not work for redirect urls, as per this answer. Is there any other solution for this issue?
Upvotes: 2
Views: 957
Reputation: 151
On Calling https://ourappname.com/services/convert?ref=xxxx
provide a response with redirect status(3xx) with Location header as https://apps.shopify.com/appname which will set that cookies for ourappname.com
and it also redirects
Cookies will be restricted to accesible for ourappname.com
and it's subdomains for security and privacy reasons.
But if there is an XHR call happening from https://apps.shopify.com/appname to ourappname.com
then that call will send the cookies along with the request.
Important : Since this is CORS, proper headers,cookie flags and parameters need to be set in FE/BE inorder to get it working.
Follow this answer as a guide for that.
Upvotes: 2