Shopify : How to test webhooks in php - shopify

I am new in shopify. I have created one app in php for shopify. I have registered webhooks using admin apis. But i don't know how to test webhooks. I have spent lots of time to figure out but not getting any proper response. How to get response and write stuff over there?
Is it like Apis? How to notify that webhooks are called or not.
Please help me.

Unlike APIs, Webhook is event driven(triggered on any event e.g. Order Creation) and send data in JSON/XML format to particular URL.
You can create a Webhook in your Shopify store by following steps.
Go to Settings -> Notification -> Webhooks -> Create Webhook
Select Event on which your webhook will be triggered data Format and URL(https) to which you want to send your data.
Now your data is available in JSON format to server location you have shared in URL field. You can use following code.
<?php
define('SHOPIFY_APP_SECRET', 'my_shared_secret');
function verify_webhook($data, $hmac_header){
$calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true));
return hash_equals($hmac_header, $calculated_hmac);
}
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
error_log('Webhook verified: '.var_export($verified, true)); //check error.log to see the result
?>

Related

How can a webhook identify USER_IDs?

I'm developing a webhook, and I want it to be able to #mention specific users. The simple message format specifies using the format of <users/123456789012345> to #mention a specific users. Since this is not a bot, it is not processing an incoming message and cannot pull the from the sender field, as suggested in the note on the developer site linked above.
How can I get the USER_ID of a user on my domain?
I am in the same boat as you. Trying to use a Webhook to mention a user.
If you inspect the HTML on https://chat.google.com, using web/dev tools, you can actually see user ID under tag data-member-id="user/bot/123456789012345"
Using the above I tried to form a webhook that mentioned the user (in my case another bot).
Unfortunately it didn't work. Passing in the string <users/123456789012345> please test came through to chat as a string literal.
It's strange because the html links and markup DO get interpreted. As mentioned here. Edit: so does <users/all>.
I'm leaving this here since your question asks for how to elicit the ID. While it may not be pretty, or much automatable, it helped me get the user ID at least.
Edit 2: It works, just not to mention my other bot for some reason. But using this method I was able to mention a human user :)
I use this for find the USER_ID but if you have another way let me know
Right click on the user and select inspect
Search for data-member-id this is your USER_ID
A webhook will not be able to pull the USER_ID of a user. As a workaround for this, you can create a service account and a bot that has access to the room and use the REST API spaces.members.list() and spaces.members.get() method.
Note: The bot will need to be added to the room.
Okay, so in order to get the UserID without having to do all of the things that you're trying to do here you need to use the Admin SDK API in google app script. Basically what you'll want to do is use your google app script as an intermediary for what you're trying to do. So you'll post something to google app script via their web exec functions on a deployed app and then have that app communicate to the google chat API via something like this:
var googlewebhookurl = 'https://chat.googleapis.com/v1/spaces/ASDFLKAJHEPQIHEWFQOEWNQWEFOINQ';
var options = {
'method': 'post',
'contentType': 'application/json',
'payload' : JSON.stringify({ text: "<users/000000000001> I see you!" })
}
UrlFetchApp.fetch(googlewebhookurl, options);
To start, you'll need to add the Admin SDK API to the services in your google app script services area. So click the plus next to "Services" and then select the Admin SDK API and click add to add it to the services, it winds up showing up in the list as "AdminDirectory" once it has been added to the services.
This is an image showing what it looks like once you've added it to the services.
Here is a link to the documentation for the Admin SDK API getting user information: https://developers.google.com/apps-script/advanced/admin-sdk-directory#get_user
You should be able to copy and paste that example function to get the information you're looking for regarding the user. I'm pasting the example code below in case something happens to this link:
/**
* Get a user by their email address and logs all of their data as a JSON string.
*/
function getUser() {
var userEmail = 'liz#example.com';
var user = AdminDirectory.Users.get(userEmail);
Logger.log('User data:\n %s', JSON.stringify(user, null, 2));
}
In order to get the user id, take the user variable that comes back and access user.id and voila! You have the ID you're looking for. From there just plaster it into a text message in google chat and you should be in business. I haven't gotten it to work with cards at all yet. I'm not seeing any documentation saying that it's supported in cards at all. For more information regarding chat messages and cards take a look at these:
https://developers.google.com/chat/api/guides/message-formats/basic
https://developers.google.com/chat/api/guides/message-formats/cards

How to send request to Telegram bot API?

After creating a telegram bot and gain bot token, I want to send a request to the bot API.
This link says we must send the HTTP request like this:
https://api.telegram.org/bot<token>/METHOD_NAME and brings example for easiest method "getme" which has not any input parameters.
Imagine I want to send some messages. I should use the sendMessage method which has two Required input parameters: chat_ID and text.
Now my Questions begins:
How can I write this sendMessage method in above request format with its parameters? I tried sendMessage(param1,param2) and received method not found message.
What is chat_id? if I want to send a message to the contact, how can I know his chat_id?
I searched a lot on the internet, there are plenty of projects on GitHub especially for this purpose, and honestly none of them makes any sense.
for god's sake someone please help me. I am loosing way.
Regards.
You just send a POST request to:
https://api.telegram.org/bot{token}/{method}
For example:
https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage
In the body of the request, you URL encode the parameters:
chat_id=12345&text=hello%20friend
For example, in Python using the requests module:
import requests
response = requests.post(
url='https://api.telegram.org/bot{0}/{1}'.format(token, method),
data={'chat_id': 12345, 'text': 'hello friend'}
).json()
When a user chats with your bot, you get a Message object that has a chat id (and a user id, which you can substitute for a chat id). There's no way to initiate a chat with a user unless you already know their user id, so you have to wait for a user to talk to you. You can simplify that by using deep linking and having the user click on a link that sends a pre-made message when they hit the Start button.
Edit: for those struggling to find chat_id, here's a way:
1.- Create a bot: on Telegram's search look for #BotFather. Click start, write /newbot, give it a name and a username. You should get a token to access the HTTP API. Save this token.
2.- Find your bot on Telegram with its username. Write something to it e.g. 'test'. This will come in handy later.
3.- Print chat_id. Before running this function, make sure that you have at least written one message to your bot on Telegram (step 2)
Javascript code:
var token = "123456:kioASDdjicOljd_ijsdf"; // Fill this in with your token
var telegramUrl = "https://api.telegram.org/bot" + token;
function getChat_id(){
var res = UrlFetchApp.fetch(telegramUrl+"/getUpdates").getContentText();
var res = JSON.parse(res);
Logger.log(res.result[0].message.chat.id.toString());
}
Try this
https://api.telegram.org/bot{token}/sendMessage?chat_id=<chat_id>&text=<Enter your text here>
Example
https://api.telegram.org/bot449123456:AAHSAnSGDm8PW2Z-1ZiwdVDmgv7sM3NMTxg/sendMessage?chat_id=311911234&text=Hi+Everyone

Bigcommerce - Get Product Custom Field

I wanted to use the Bigcommerce API from here , and fetch all custom product fileds, so I used this get path:
GET /stores/{store_hash}/v2/products/{product_id}/custom_fields/{id}.
Bacause I needed {store_hash} I visited this site , and creted "Legacy API Account" and created in admin panel legacy api account and generated: Api path and Api token.
I thought that the store_hash is the same as api token, but it wasn't.
What am I do now, how does look like the full path to using this clause :
for "OAuth" :
GET /stores/{store_hash}/v2/products/{product_id}/custom_fields/{id}.
or for "Basic Auth" :
GET /api/v2/products/{product_id}/custom_fields/{id}
In this way, I would like to do:
$.getJSON( "/stores/{store_hash}/v2/products/{product_id}/custom_fields/{id}", function( data ) { // custom code with data });
Thank you in advance for any help
Paul
If you're just using the legacy API you can request directly from the store's API path.
$.getJSON( "https://store-XXXXX.mybigcommerce.com/api/v2/products/{product_id}/custom_fields/{id}")
If the store has their own private SSL installed that API address will be their domain instead of the .mybigcommerce address

Coinbase address callbacks not working?

I'm using the Coinbase API. My app generates new receive addresses with callbacks. However, when BTC arrives at any of those addresses, the callbacks don't seem to be firing.
I've verified that the callbacks are indeed being created for the new addresses, and that my app would respond correctly to the callbacks. For instance, this triggers the desired functionality on my server:
curl --data "address=someaddress&amount=1.2" https://mydomain.com/callback?secret_token=mysecret
Alas, there are no calls being made to my server whatsoever (any call would show up in the logs, but none do).
Anyone using Coinbase address callbacks successfully? Any hints for debugging this?
Have you tried using a json request with php to set the callback url? If you login and look at the addresses section, do you see the correct url in the callback section? Make sure there is no typo. I was able to do this a few months ago but have left the project that did this.
create a php file callback.php
put this code in your callback.php
<?php
$jsonData = file_get_contents('php://input');
$postDatacoinbase = json_decode($jsonData, true);
print_r ($postDatacoinbase);
?>
login into Coinbase and click on " Merchant settings " and
input your http://mydomain.com/callback.php url into callback url
and click test now to see the results .

How to access facebook session id in an fbml application?

I am working on an fbml application which can be added to a fan page. The application then sends ajax request to my php script for data etc.
I have to store some data on my php side against a key or something which remains the same throughout facebook session. How can I implement this? Is there any way to send facebook session id in ajax request? I am using fbml Ajax() element to send ajax request.
Here's a glimpse of code
function do_ajax(path)
{
var sessionKey = '###'; //Something representing fbml app's session state
var ajax = new Ajax();
ajax.responseType = Ajax.FBML;
ajax.ondone = function(data) {
document.getElementById('content').setInnerFBML(data);
}
ajax.post(path+'/'+sessionKey);
}
The visitor may or may not be logged in so I can't use userid. And php session id keeps changing in each request.
Thanks a lot
FBML is being deprecated. See: https://developers.facebook.com/docs/reference/fbml/ No more support for it in 10 days, and then in 6 months it will no longer work. I would suggest using the new Javascript SDK.