-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsnap_simple_example.py
More file actions
45 lines (38 loc) · 1.5 KB
/
snap_simple_example.py
File metadata and controls
45 lines (38 loc) · 1.5 KB
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
import midtransclient
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
# Please refer to this docs for snap popup:
# https://docs.midtrans.com/en/snap/integration-guide?id=integration-steps-overview
# Please refer to this docs for snap-redirect:
# https://docs.midtrans.com/en/snap/integration-guide?id=alternative-way-to-display-snap-payment-page-via-redirect
# Initialize snap client object
# You can find it in Merchant Portal -> Settings -> Access keys
snap = midtransclient.Snap(
is_production=False,
server_key='YOUR_SERVER_KEY',
client_key='YOUR_CLIENT_KEY'
)
# prepare SNAP API parameter ( refer to: https://snap-docs.midtrans.com ) minimum parameter example
param = {
"transaction_details": {
"order_id": "test-transaction-123",
"gross_amount": 200000
}, "credit_card":{
"secure" : True
}
}
# create transaction
transaction = snap.create_transaction(param)
# transaction token
transaction_token = transaction['token']
print('transaction_token:')
print(transaction_token)
# transaction redirect url
transaction_redirect_url = transaction['redirect_url']
print('transaction_redirect_url:')
print(transaction_redirect_url)
# transaction is dictionary representation of API JSON response
# sample:
# {
# 'redirect_url': 'https://app.sandbox.midtrans.com/snap/v2/vtweb/f0a2cbe7-dfb7-4114-88b9-1ecd89e90121',
# 'token': 'f0a2cbe7-dfb7-4114-88b9-1ecd89e90121'
# }