Skip to content

Commit 4d3536a

Browse files
Merge pull request #19 from omarxp/3ds-new-flow
sample app - add note and warning message
2 parents 6f60805 + ccc7bf1 commit 4d3536a

11 files changed

+77
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ dist/
77
build/
88
push_to_twine.sh
99
./Pipfile
10+
env/
11+
.DS_STORE

examples/core_api/core_api_credit_card_example.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import midtransclient
2-
# initialize core api client object
2+
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
3+
4+
# Initialize core api client object
5+
# You can find it in Merchant Portal -> Settings -> Access keys
36
core = midtransclient.CoreApi(
47
is_production=False,
58
server_key='YOUR_SERVER_KEY',
@@ -20,10 +23,13 @@
2023
# core.api_config.server_key='YOUR_SERVER_KEY'
2124
# core.api_config.client_key='YOUR_CLIENT_KEY'
2225

23-
# IMPORTANT NOTE: You should do credit card get token via frontend using `midtrans.min.js`, to avoid card data breach risks on your backend
24-
# ( refer to: https://api-docs.midtrans.com )
26+
# IMPORTANT NOTE: You should do credit card get token via frontend using `midtrans-new-3ds.min.js`, to avoid card data breach risks on your backend
27+
# ( refer to: https://docs.midtrans.com/en/core-api/credit-card?id=_1-getting-the-card-token )
28+
# For full example on Credit Card 3DS transaction refer to:
29+
# (/examples/flask_app) that implement Snap & Core Api
2530

2631
# prepare CORE API parameter to get credit card token
32+
# another sample of card number can refer to https://docs.midtrans.com/en/technical-reference/sandbox-test?id=card-payments
2733
params = {
2834
'card_number': '5264 2210 3887 4659',
2935
'card_exp_month': '12',
@@ -34,7 +40,7 @@
3440
card_token_response = core.card_token(params)
3541
cc_token = card_token_response['token_id']
3642

37-
# prepare CORE API parameter to charge credit card ( refer to: https://api-docs.midtrans.com )
43+
# prepare CORE API parameter to charge credit card ( refer to: https://docs.midtrans.com/en/core-api/credit-card?id=_2-sending-transaction-data-to-charge-api )
3844
param = {
3945
"payment_type": "credit_card",
4046
"transaction_details": {

examples/core_api/core_api_simple_example.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import midtransclient
2-
# initialize core api client object
2+
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
3+
4+
# Initialize core api client object
5+
# You can find it in Merchant Portal -> Settings -> Access keys
36
core = midtransclient.CoreApi(
47
is_production=False,
58
server_key='YOUR_SERVER_KEY',
69
client_key='YOUR_CLIENT_KEY'
710
)
811

9-
# prepare CORE API parameter ( refer to: https://api-docs.midtrans.com ) charge bank_transfer parameter example
12+
# prepare CORE API parameter ( refer to: https://docs.midtrans.com/en/core-api/bank-transfer?id=sample-request-and-request-body ) charge bank_transfer parameter example
1013
param = {
1114
"payment_type": "bank_transfer",
1215
"transaction_details": {

examples/flask_app/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h4>Advanced usage:</h4>
1111
<ul>
1212
<li><a href="/simple_core_api_checkout">/simple_core_api_checkout</a> via Core Api - Credit Card</li>
1313
<li><a href="/simple_core_api_checkout_permata">/simple_core_api_checkout_permata</a> via Core Api - Permata VA</li>
14-
<li><a href="/core_api_credit_card_frontend_sample">/core_api_credit_card_frontend_sample</a> Core Api - Credit Card to demonstrate how Midtrans.min.js will be used</li>
14+
<li><a href="/core_api_credit_card_frontend_sample">/core_api_credit_card_frontend_sample</a> Core Api - Credit Card to demonstrate how midtrans-new-3ds.min.js will be used</li>
1515
</ul>
1616
</body>
1717
</html>

examples/flask_app/templates/simple_core_api_checkout.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ <h1>Checkout</h1>
6161
</code>
6262

6363
<hr>
64-
<small>Check `app.js` file, section `Using Core API - Credit Card` for the backend implementation</small>
64+
<small>Check `web.py` file, section `Using Core API - Credit Card` for the backend implementation</small>
6565

6666
<!-- Import MidtransNew3ds library -->
6767
<!-- TODO change data-environment to `production` for Production mode -->
@@ -91,7 +91,7 @@ <h1>Checkout</h1>
9191
onSuccess: function(response){
9292
// success to get card token
9393
// [2] Send AJAX to let backend charge the card using the card token_id
94-
// Check backend implementation on `app.js` file, section `[2]`
94+
// Check backend implementation on `web.py` file, section `[2]`
9595
fetch("/charge_core_api_ajax", {
9696
method : "POST",
9797
body: JSON.stringify({
@@ -147,7 +147,7 @@ <h1>Checkout</h1>
147147

148148
if (responseObj.transaction_id){
149149
// [4] Inform the result to backend update DB status and verify to Midtrans
150-
// Check backend implementation on `app.js` file, section `[4]`
150+
// Check backend implementation on `web.py` file, section `[4]`
151151
fetch('/check_transaction_status', {
152152
method : "POST",
153153
body: JSON.stringify({ "transaction_id" : responseObj.transaction_id }),

examples/flask_app/templates/simple_core_api_checkout_permata.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h3>Permata VA Transaction Created</h3>
1313
<b>Order ID:</b> <br>
1414
<code> {{ order_id }}</code> <br>
1515
<br><hr>
16-
<small>Check `app.js` file, section `/simple_core_api_checkout_permata` for the backend implementation</small>
16+
<small>Check `web.py` file, section `/simple_core_api_checkout_permata` for the backend implementation</small>
1717
</div>
1818
</body>
1919
</html>

examples/flask_app/web.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
2+
13
import datetime
24
import json
5+
import os
36
from flask import Flask, render_template, request, jsonify
4-
57
from midtransclient import Snap, CoreApi
68

9+
# Set Your server key
10+
# You can find it in Merchant Portal -> Settings -> Access keys
11+
SERVER_KEY = 'SB-Mid-server-GwUP_WGbJPXsDzsNEBRs8IYA'
12+
CLIENT_KEY = 'SB-Mid-client-61XuGAwQ8Bj8LxSS'
13+
714
app = Flask(__name__)
815

916
#==============#
@@ -15,8 +22,8 @@
1522
def simple_checkout():
1623
snap = Snap(
1724
is_production=False,
18-
server_key='SB-Mid-server-GwUP_WGbJPXsDzsNEBRs8IYA',
19-
client_key='SB-Mid-client-61XuGAwQ8Bj8LxSS',
25+
server_key=SERVER_KEY,
26+
client_key=CLIENT_KEY
2027
)
2128
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2229
transaction_token = snap.create_transaction_token({
@@ -27,20 +34,20 @@ def simple_checkout():
2734
"secure" : True
2835
}
2936
})
37+
3038
return render_template('simple_checkout.html',
3139
token = transaction_token,
3240
client_key = snap.api_config.client_key)
3341

34-
3542
#==============#
3643
# Using Core API - Credit Card
3744
#==============#
3845

3946
# [0] Setup API client and config
4047
core = CoreApi(
4148
is_production=False,
42-
server_key='SB-Mid-server-GwUP_WGbJPXsDzsNEBRs8IYA',
43-
client_key='SB-Mid-client-61XuGAwQ8Bj8LxSS',
49+
server_key=SERVER_KEY,
50+
client_key=CLIENT_KEY
4451
)
4552
# [1] Render HTML+JS web page to get card token_id and [3] 3DS authentication
4653
@app.route('/simple_core_api_checkout')
@@ -171,12 +178,27 @@ def simple_core_api_checkout_permata():
171178
# Homepage of this web app
172179
@app.route('/')
173180
def index():
181+
if not SERVER_KEY or not CLIENT_KEY:
182+
# non-relevant function only used for demo/example purpose
183+
return printExampleWarningMessage()
184+
174185
return render_template('index.html')
186+
175187
# credit card frontend demo
176188
@app.route('/core_api_credit_card_frontend_sample')
177189
def core_api_credit_card_frontend_sample():
178190
return render_template('core_api_credit_card_frontend_sample.html',
179191
client_key = core.api_config.client_key)
180192

193+
194+
def printExampleWarningMessage():
195+
pathfile = os.path.abspath("web.py")
196+
message = "<code><h4>Please set your server key and client key from sandbox</h4>In file: " + pathfile
197+
message += "<br><br># Set Your server key"
198+
message += "<br># You can find it in Merchant Portal -> Settings -> Access keys"
199+
message += "<br>SERVER_KEY = ''"
200+
message += "<br>CLIENT_KEY = ''</code>"
201+
return message
202+
181203
if __name__ == '__main__':
182-
app.run(debug=True,port=5000,host='0.0.0.0')
204+
app.run(debug=True,port=5000,host='0.0.0.0')

examples/snap/snap_advanced_example.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import midtransclient
2-
# initialize snap client object
2+
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
3+
# Please refer to this docs for snap popup:
4+
# https://docs.midtrans.com/en/snap/integration-guide?id=integration-steps-overview
5+
6+
# Please refer to this docs for snap-redirect:
7+
# https://docs.midtrans.com/en/snap/integration-guide?id=alternative-way-to-display-snap-payment-page-via-redirect
8+
9+
# Initialize snap client object
10+
# You can find it in Merchant Portal -> Settings -> Access keys
311
snap = midtransclient.Snap(
412
is_production=False,
513
server_key='YOUR_SERVER_KEY',

examples/snap/snap_simple_example.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import midtransclient
2-
# initialize snap client object
2+
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
3+
# Please refer to this docs for snap popup:
4+
# https://docs.midtrans.com/en/snap/integration-guide?id=integration-steps-overview
5+
6+
# Please refer to this docs for snap-redirect:
7+
# https://docs.midtrans.com/en/snap/integration-guide?id=alternative-way-to-display-snap-payment-page-via-redirect
8+
9+
# Initialize snap client object
10+
# You can find it in Merchant Portal -> Settings -> Access keys
311
snap = midtransclient.Snap(
412
is_production=False,
513
server_key='YOUR_SERVER_KEY',

examples/transaction_actions/notification_example.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import midtransclient
2+
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
3+
# Please refer to this docs for sample HTTP POST notifications:
4+
# https://docs.midtrans.com/en/after-payment/http-notification?id=sample-of-different-payment-channels
25

3-
# initialize api client object
6+
# Initialize api client object
7+
# You can find it in Merchant Portal -> Settings -> Access keys
48
api_client = midtransclient.CoreApi(
59
is_production=False,
610
server_key='YOUR_SERVER_KEY',

0 commit comments

Comments
 (0)