-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path02-webhook-verification.py
67 lines (55 loc) · 1.72 KB
/
02-webhook-verification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# Example: How to verify Mollie API Payments in a webhook.
#
import os
import flask
from app import database_write
from mollie.api.client import Client
from mollie.api.error import Error
def main():
try:
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.mollie.com/dashboard/settings/profiles
#
api_key = os.environ.get("MOLLIE_API_KEY", "test_test")
mollie_client = Client()
mollie_client.set_api_key(api_key)
#
# Retrieve the payment's current state.
#
if "id" not in flask.request.form:
flask.abort(404, "Unknown payment id")
payment_id = flask.request.form["id"]
payment = mollie_client.payments.get(payment_id)
my_webshop_id = payment.metadata["my_webshop_id"]
#
# Update the order in the database.
#
data = {"status": payment.status}
database_write(my_webshop_id, data)
if payment.is_paid():
#
# At this point you'd probably want to start the process of delivering the product to the customer.
#
return "Paid"
elif payment.is_pending():
#
# The payment has started but is not complete yet.
#
return "Pending"
elif payment.is_open():
#
# The payment has not started yet. Wait for it.
#
return "Open"
else:
#
# The payment isn't paid, pending nor open. We can assume it was aborted.
#
return "Cancelled"
except Error as err:
return f"API call failed: {err}"
if __name__ == "__main__":
print(main())