SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

woo_webhook.py (1215B)


      1 import hmac
      2 import hashlib
      3 import json
      4 import codecs
      5 import time
      6 import requests
      7 import logging
      8 
      9 
     10 def hook(satsale_secret, invoice, order_id):
     11     key = codecs.decode(satsale_secret, "hex")
     12 
     13     # Calculate a secret that is required to send back to the
     14     # woocommerce gateway, proving we did not modify id nor amount.
     15     secret_seed = str(int(100 * float(invoice["base_value"]))).encode("utf-8")
     16     logging.info("Secret seed: {}".format(secret_seed))
     17 
     18     secret = hmac.new(key, secret_seed, hashlib.sha256).hexdigest()
     19 
     20     # The main signature  which proves we have paid, and very recently!
     21     paid_time = int(time.time())
     22     params = {"wc-api": "wc_satsale_gateway", "time": str(paid_time), "id": order_id}
     23     message = (str(paid_time) + "." + json.dumps(params, separators=(",", ":"))).encode(
     24         "utf-8"
     25     )
     26 
     27     # Calculate the hash
     28     hash = hmac.new(key, message, hashlib.sha256).hexdigest()
     29     headers = {
     30         "Content-Type": "application/json",
     31         "X-Signature": hash,
     32         "X-Secret": secret,
     33     }
     34 
     35     # Send the webhook response, confirming the payment with woocommerce.
     36     response = requests.get(invoice["webhook"], params=params, headers=headers)
     37 
     38     return response