-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli_test_payline.py
106 lines (83 loc) · 2.9 KB
/
cli_test_payline.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import argparse
from collectives import create_app
from collectives.utils import payline
def doWebPayment():
"""
Initiates a new Payline web payment.
:return: The resulting Payline token
:rtype: str
"""
order = payline.OrderInfo()
order.payment_id = 2
order.amount_in_cents = "10000"
order.date = "05/05/2020 00:05"
order.details = {"details": [{"ref": "12"}]}
order.metadata = {
"ref": "12",
"coll": ["name", "Ski' de randonnée"],
"coll2": "Ski' de randonnée",
}
buyer = payline.BuyerInfo()
buyer.title = "4"
buyer.lastName = "DO"
buyer.firstName = "JOHN"
buyer.email = "johndoe@yopmail.com"
buyer.mobilePhone = "0600000000"
buyer.birthDate = "1980-01-20"
doWebPaymentResponse = payline.api.doWebPayment(order, buyer)
if doWebPaymentResponse is None:
raise RuntimeError("Payline API error")
print(
f"token : {doWebPaymentResponse.token}, URL de paiement : {doWebPaymentResponse.redirect_url}"
)
input("appuyer sur une touche lorsque le paiement est validé")
return doWebPaymentResponse.token
def getPaymentDetails(token):
"""Displays and returns information about a payment
:param token: The payline token
:type token: str
:return: The payment information
:rtype: :py:class:`collectives.utils.payline.PaymentDetails`
"""
details = payline.api.getWebPaymentDetails(token)
print(
f"Result: {details.result.short_message} Transaction: {details.transaction['id']} Date: {details.transaction['date']}"
)
print(details.raw_metadata())
return details
def doRefund(payment_details):
"""Initiates a refind request
:param payment_details: Payment details as returned by :py:func:`getPaymentDetails`
:type payment_details: :py:class:`collectives.utils.payline.PaymentDetails`
"""
# Try refund
refundResponse = payline.api.doRefund(payment_details)
if refundResponse is None:
raise RuntimeError("Payline API error")
print(
f"Refund result: {refundResponse.result.code} {refundResponse.result.long_message}"
)
print(refundResponse.raw_metadata())
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-t",
"--token",
help="If provided fetch payment information from an existing payline token. Otherwise initiate a new payment",
)
parser.add_argument(
"-r",
"--refund",
action="store_true",
help="If provided attempt to refund the payment",
)
args = parser.parse_args()
app = create_app()
app.config["SERVER_NAME"] = "localhost"
with app.app_context():
payment_token = args.token
if payment_token is None:
payment_token = doWebPayment()
retrieved_details = getPaymentDetails(payment_token)
if args.refund:
doRefund(retrieved_details)