-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from omarxp/tokenization-and-subscription
add API subscription & Gopay Tokenization implementation
- Loading branch information
Showing
11 changed files
with
903 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,7 +123,7 @@ snap = midtransclient.Snap( | |
server_key='YOUR_SERVER_KEY', | ||
client_key='YOUR_CLIENT_KEY' | ||
) | ||
# Build API parameter | ||
# Prepare parameter | ||
param = { | ||
"transaction_details": { | ||
"order_id": "test-transaction-123", | ||
|
@@ -191,7 +191,7 @@ snap = midtransclient.Snap( | |
server_key='YOUR_SERVER_KEY', | ||
client_key='YOUR_CLIENT_KEY' | ||
) | ||
# Build API parameter | ||
# Prepare parameter | ||
param = { | ||
"transaction_details": { | ||
"order_id": "test-transaction-123", | ||
|
@@ -277,7 +277,7 @@ core_api = midtransclient.Snap( | |
server_key='YOUR_SERVER_KEY', | ||
client_key='YOUR_CLIENT_KEY' | ||
) | ||
# Build API parameter | ||
# Prepare parameter | ||
param = { | ||
"payment_type": "credit_card", | ||
"transaction_details": { | ||
|
@@ -303,6 +303,108 @@ The credit card charge result may contains `redirect_url` for 3DS authentication | |
For full example on Credit Card 3DS transaction refer to: | ||
- [Flask App examples](/examples/flask_app) that implement Snap & Core Api | ||
|
||
### 2.2.D Subscription API | ||
|
||
You can see some Subscription API examples [here](examples/subscription), [Subscription API Docs](https://api-docs.midtrans.com/#subscription-api) | ||
|
||
#### Subscription API for Credit Card | ||
|
||
To use subscription API for credit card, you should first obtain the 1-click saved token, [refer to this docs.](https://docs.midtrans.com/en/core-api/advanced-features?id=recurring-transaction-with-subscriptions-api) | ||
|
||
You will receive `saved_token_id` as part of the response when the initial card payment is accepted (will also available in the HTTP notification's JSON), [refer to this docs.](https://docs.midtrans.com/en/core-api/advanced-features?id=sample-3ds-authenticate-json-response-for-the-first-transaction) | ||
|
||
```python | ||
# Create Subscription API instance | ||
core_api = midtransclient.CoreApi( | ||
is_production=False, | ||
server_key='YOUR_SERVER_KEY', | ||
client_key='YOUR_CLIENT_KEY' | ||
) | ||
# Prepare parameter | ||
param = { | ||
"name": "SUBSCRIPTION-STARTER-1", | ||
"amount": "100000", | ||
"currency": "IDR", | ||
"payment_type": "credit_card", | ||
"token": "436502qFfqfAQKScMtPRPdZDOaeg7199", | ||
"schedule": { | ||
"interval": 1, | ||
"interval_unit": "month", | ||
"max_interval": 3, | ||
"start_time": "2021-10-01 07:25:01 +0700" | ||
}, | ||
"metadata": { | ||
"description": "Recurring payment for STARTER 1" | ||
}, | ||
"customer_details": { | ||
"first_name": "John A", | ||
"last_name": "Doe A", | ||
"email": "[email protected]", | ||
"phone": "+62812345678" | ||
} | ||
} | ||
create_subscription_response = core_api.create_subscription(param) | ||
|
||
subscription_id_response = create_subscription_response['id'] | ||
# get subscription by subscription_id | ||
get_subscription_response = core_api.get_subscription(subscription_id_response) | ||
|
||
# disable subscription by subscription_id | ||
disable_subscription_response = core_api.disable_subscription(subscription_id_response) | ||
|
||
# enable subscription by subscription_id | ||
enable_subscription_response = core_api.enable_subscription(subscription_id_response) | ||
|
||
# update subscription by subscription_id | ||
update_param = { | ||
"name": "SUBSCRIPTION-STARTER-1-UPDATE", | ||
"amount": "100000", | ||
"currency": "IDR", | ||
"token": "436502qFfqfAQKScMtPRPdZDOaeg7199", | ||
"schedule": { | ||
"interval": 1 | ||
} | ||
update_subscription_response = core_api.update_subscription(subscription_id_response, update_param) | ||
``` | ||
|
||
#### Subscription API for Gopay | ||
|
||
To use subscription API for gopay, you should first link your customer gopay account with gopay tokenization API, [refer to this section](#22e-tokenization-api) | ||
|
||
You will receive gopay payment token using `get_payment_account` API call | ||
|
||
You can see some Subscription API examples [here](examples/subscription) | ||
|
||
### 2.2.E Tokenization API | ||
You can see some Tokenization API examples [here](examples/tokenization), [Tokenization API Docs](https://api-docs.midtrans.com/#gopay-tokenization) | ||
|
||
```python | ||
# Create Tokenization API instance | ||
core_api = midtransclient.CoreApi( | ||
is_production=False, | ||
server_key='YOUR_SERVER_KEY', | ||
client_key='YOUR_CLIENT_KEY' | ||
) | ||
# Prepare parameter | ||
param = { | ||
"payment_type": "gopay", | ||
"gopay_partner": { | ||
"phone_number": "81234567891", | ||
"country_code": "62", | ||
"redirect_url": "https://mywebstore.com/gopay-linking-finish" #please update with your redirect URL | ||
} | ||
} | ||
|
||
# link payment account | ||
link_payment_account_response = core_api.link_payment_account(param) | ||
|
||
# get payment account | ||
get_payment_account_response = core_api.get_payment_account(active_account_id) | ||
|
||
# unlink account | ||
unlink_payment_account_response = core_api.unlink_payment_account(active_account_id) | ||
``` | ||
|
||
### 2.3 Handle HTTP Notification | ||
|
||
> **IMPORTANT NOTE**: To update transaction status on your backend/database, **DO NOT** solely rely on frontend callbacks! For security reason to make sure the status is authentically coming from Midtrans, only update transaction status based on HTTP Notification or API Get Status. | ||
|
@@ -479,6 +581,8 @@ Under the hood this API wrapper is using [Requests](https://github.com/requests/ | |
Examples are available on [/examples](/examples) folder. | ||
There are: | ||
- [Core Api examples](/examples/core_api) | ||
- [Subscription examples](/examples/subscription) | ||
- [Tokenization examples](/examples/tokenization) | ||
- [Snap examples](/examples/snap) | ||
- [Flask App examples](/examples/flask_app) that implement Snap & Core Api | ||
|
||
|
118 changes: 118 additions & 0 deletions
118
examples/subscription/credit_card_subscription_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely. | ||
import midtransclient | ||
import datetime | ||
|
||
# Initialize core api client object | ||
# You can find it in Merchant Portal -> Settings -> Access keys | ||
core_api = midtransclient.CoreApi( | ||
is_production=False, | ||
server_key='YOUR_SERVER_KEY', | ||
client_key='YOUR_CLIENT_KEY' | ||
) | ||
|
||
# To use API subscription for credit card, you should first obtain the 1 click token | ||
# Refer to this docs: https://docs.midtrans.com/en/core-api/advanced-features?id=recurring-transaction-with-subscriptions-api | ||
|
||
# You will receive saved_token_id as part of the response when the initial card payment is accepted (will also available in the HTTP notification's JSON) | ||
# Refer to this docs: https://docs.midtrans.com/en/core-api/advanced-features?id=sample-3ds-authenticate-json-response-for-the-first-transaction | ||
# { | ||
# ... | ||
# "card_type": "credit", | ||
# "saved_token_id":"481111xDUgxnnredRMAXuklkvAON1114", | ||
# "saved_token_id_expired_at": "2022-12-31 07:00:00", | ||
# ... | ||
# } | ||
# Sample saved token id for testing purpose | ||
SAVED_TOKEN_ID = '436502qFfqfAQKScMtPRPdZDOaeg7199' | ||
|
||
# prepare subscription parameter ( refer to: https://api-docs.midtrans.com/#create-subscription ) | ||
param = { | ||
"name": "SUBS-PY-"+datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S"), | ||
"amount": "100000", | ||
"currency": "IDR", | ||
"payment_type": "credit_card", | ||
"token": SAVED_TOKEN_ID, | ||
"schedule": { | ||
"interval": 1, | ||
"interval_unit": "day", | ||
"max_interval": 7 | ||
}, | ||
"metadata": { | ||
"description": "Recurring payment for A" | ||
}, | ||
"customer_details": { | ||
"first_name": "John A", | ||
"last_name": "Doe A", | ||
"email": "[email protected]", | ||
"phone": "+62812345678" | ||
} | ||
} | ||
|
||
# create subscription | ||
create_subscription_response = core_api.create_subscription(param) | ||
print('create_subscription_response:') | ||
print(create_subscription_response) | ||
|
||
# subscription_response is dictionary representation of API JSON response | ||
# sample: | ||
# { | ||
# 'id': 'b6eb6a04-33e6-46a2-a298-cd78e55b3a3f', | ||
# 'name': 'SUBS-PY-1', | ||
# 'amount': '100000', | ||
# 'currency': 'IDR', | ||
# 'created_at': '2021-10-27 13:29:51', | ||
# 'schedule': { | ||
# 'interval': 1, | ||
# 'current_interval': 0, | ||
# 'max_interval': 7, | ||
# 'interval_unit': 'day', | ||
# 'start_time': '2021-10-27 13:30:01', | ||
# 'next_execution_at': '2021-10-27 13:30:01' | ||
# }, | ||
# 'status': 'active', | ||
# 'token': '436502qFfqfAQKScMtPRPdZDOaeg7199', | ||
# 'payment_type': 'credit_card', | ||
# 'transaction_ids': [ | ||
|
||
# ], | ||
# 'metadata': { | ||
# 'description': 'Recurring payment for A' | ||
# }, | ||
# 'customer_details': { | ||
# 'email': '[email protected]', | ||
# 'first_name': 'John', | ||
# 'last_name': 'Doe', | ||
# 'phone': '+62812345678' | ||
# } | ||
# } | ||
|
||
subscription_id_response = create_subscription_response['id'] | ||
|
||
# get subscription by subscription_id | ||
get_subscription_response = core_api.get_subscription(subscription_id_response) | ||
print('get_subscription_response:') | ||
print(get_subscription_response) | ||
|
||
# enable subscription by subscription_id | ||
enable_subscription_response = core_api.enable_subscription(subscription_id_response) | ||
print('enable_subscription_response:') | ||
print(enable_subscription_response) | ||
|
||
# update subscription by subscription_id | ||
update_param = { | ||
"name": "SUBS-PY-UPDATE", | ||
"amount": "100000", | ||
"currency": "IDR", | ||
"token": SAVED_TOKEN_ID, | ||
"schedule": { | ||
"interval": 1 | ||
} | ||
} | ||
update_subscription_response = core_api.update_subscription(subscription_id_response, update_param) | ||
print('update_subscription_response:') | ||
print(update_subscription_response) | ||
|
||
# disable subscription by subscription_id | ||
disable_subscription_response = core_api.disable_subscription(subscription_id_response) | ||
print('disable_subscription_response:') | ||
print(disable_subscription_response) |
Oops, something went wrong.