2

Good afternoon all,

I am attempting to create a function that will automatically create a membership through my external loyalty program (through Whisqr) for the current user on my Wix.com website. I am receiving an error message stating the public key is not found.

Here is my backend code:

 import {fetch} from 'wix-fetch';  
import {wixData} from 'wix-data';


export function postLoyalty() {
 let options ={
 "headers": {        
 "X-Public": "pk_live_ba43e74df464cbf521dd07ee20443ff754c3afc11adc16df2594facb2147cd76"
        }
  }
 const url = 'https://whisqr.com/api/v1.2/user/customer/';
 const key = '<pk_live_ba43e74df464cbf521dd07ee20443ff754c3afc11adc16df2594facb2147cd76>';
     console.log("Url: ");

 return fetch(url, {method: 'post'})
    .then(response => {
 return response.json();
    })
    .then((data) => {
      console.log(data);
 return data;
    });
}

Here is my page code:

import {postLoyalty} from 'backend/Loyalty.jsw';
import {wixData} from 'wix-data';
import wixLocation from "wix-location";
import {myFunction} from 'public/core.js';
import wixUsers from 'wix-users';


$w.onReady(function () {
 let publickey = 'pk_live_ba43e74df464cbf521dd07ee20443ff754c3afc11adc16df2594facb2147cd76';
    myFunction(publickey)
    .then( (response) => {
        console.log(response); //your base64 encoded string
    })});

export function page1_viewportEnter(event) {
 //Add your code for this event here: 
 let email = wixUsers.currentUser.getEmail();
postLoyalty(email)
        .then(LoyaltyInfo => {
            console.log(LoyaltyInfo)
            $w("#text1").text = LoyaltyInfo.Results.Value;
        })
}


Any and all feedback is greatly appreciated!

1 Answers1

0

You are making a call to the URL using the POST method but you are not utilizing any of the keys, headers which you have defined.

A proper POST call which utilizes the header and body in its request will look like the below:

    export function myFunction(data) {

     const url = "https://whisqr.com/api/v1.2/user/customer/";

      const headers = {
          "Authorization": "Bearer " + key, //if api key is required like this
          "Content-Type": "application/json" //the content type
        };

      return fetch(url, {
            "method": "POST",
            "headers": headers,
            "body": JSON.stringify(data) //if there is a body
        });
    }

You said that you need to create a member on the external platform so you must be needing to send a body with the customer's data. Read the API Documentation.

Shan
  • 948
  • 1
  • 9
  • 17
  • Thank you very much for this Shan. I am still getting an error about the content has not being found as well. I am not sure how to code this, I was under the impression that the content hash was generated per user. Are you able to explain this further? – Lightning Admin Oct 19 '19 at 14:34
  • My API calls for a content hash generated using HMAC SHA256 but I am unsure of how to generate this. Are you able to assist or offer any articles/tutorials for this? – Lightning Admin Oct 24 '19 at 15:18
  • https://stackoverflow.com/questions/35228114/using-javascript-to-properly-sign-a-string-using-hmacsha256 – Shan Oct 24 '19 at 16:17
  • I looked over the article and am unable to figure out what the "secret key" and "secret message" are. – Lightning Admin Oct 24 '19 at 18:20