linepay_test.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import requests
  2. import json
  3. import base64
  4. import hashlib
  5. import hmac
  6. import uuid
  7. # sandbox
  8. url = "https://sandbox-api-pay.line.me"
  9. # url_path = "/v3/payments/request"
  10. transactionId=2021101400692705810
  11. orderId=1
  12. url_path = "/v3/payments/" + str(transactionId) + "/confirm"
  13. # # production
  14. # url = "https://api-pay.line.me"
  15. ChannelId = 1656522171
  16. ChannelSecret = 'd6d37304bc39496025519366b9c0ab44'
  17. nonce = uuid.uuid4()
  18. def line_Signature(ChannelSecret, payload, nonce, url_path):
  19. signature = hmac.new(
  20. ChannelSecret.encode('utf-8'),
  21. msg=(ChannelSecret + url_path + json.dumps(payload) + str(nonce)).encode('utf-8'),
  22. digestmod=hashlib.sha256
  23. ).digest()
  24. return base64.b64encode(signature)
  25. # payload={
  26. # "amount": 100,
  27. # "currency": "TWD",
  28. # "orderId":1,
  29. # "packages" : [
  30. # {
  31. # "id" : "1",
  32. # "amount": 100,
  33. # "products" : [
  34. # {
  35. # "id" : "PEN-B-001",
  36. # "name" : "Pen Brown",
  37. # "imageUrl" : "https://pay-store.line.com/images/pen_brown.jpg",
  38. # "quantity" : 2,
  39. # "price" : 50
  40. # }
  41. # ]
  42. # }
  43. # ],
  44. # "redirectUrls" : {
  45. # "confirmUrl" : "https://pay-store.line.com/order/payment/confirm",
  46. # "cancelUrl" : "https://pay-store.line.com/order/payment/cancel"
  47. # }
  48. # }
  49. payload={
  50. "amount": 100,
  51. "currency": "TWD"
  52. }
  53. headers = {
  54. 'Content-Type': 'application/json',
  55. 'X-LINE-ChannelId': str(ChannelId),
  56. 'X-LINE-Authorization-Nonce': str(nonce),
  57. 'X-LINE-Authorization': line_Signature(ChannelSecret, payload, nonce, url_path)
  58. }
  59. response = requests.request("POST", url + url_path, headers=headers, data=json.dumps(payload))
  60. print(response.text)