Skip to content

Instantly share code, notes, and snippets.

@mafellows
Created June 29, 2020 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafellows/4e962d12ae6a186a6494a5716db379e0 to your computer and use it in GitHub Desktop.
Save mafellows/4e962d12ae6a186a6494a5716db379e0 to your computer and use it in GitHub Desktop.
# Get Products
shop_url = "https://%s:%s@my-shop-url.myshopify.com/admin/api/%s" % ('username', 'password', '2020-01')
shopify.ShopifyResource.set_site(shop_url)
products = shopify.Product.find()
for product in products:
try:
print('====')
print(product.id)
print(product.title)
print(product.image.src)
for variant in product.variants:
print(variant.id)
print(variant.attributes)
except:
print('error')
# Draft Orders
# Update a draft order
shop_url = "https://%s:%s@my-shop-url.myshopify.com/admin/api/%s" % ('username', 'password', '2020-01')
shopify.ShopifyResource.set_site(shop_url)
draft_order = shopify.DraftOrder().find(order_id)
draft_order.complete() # mark as completed.
# Webhook
# Verify shopify webhook from a labmda function
import json
import hmac
import hashlib
import base64
import os
SECRET = os.environ['SHOP_SECRET']
def verify_webhook(data, hmac_header):
digest = hmac.new(SECRET.encode('utf-8'), data.encode('utf-8'), hashlib.sha256).digest()
computed_hmac = base64.b64encode(digest)
return hmac.compare_digest(computed_hmac, hmac_header.encode('utf-8'))
def lambda_handler(event, context):
verified = verify_webhook(event['body'], event['headers']['X-Shopify-Hmac-Sha256'])
# Create an order.
if not verified:
print('webhook not verified')
return {
'statusCode': 401,
'body': json.dumps({'message': 'Not verified'})
}
return {
'statusCode': 200,
'body': json.dumps({'message': 'ok'})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment