官方midtrans支付api客户端

midtransclient的Python项目详细描述


midtrans客户端-python

Build StatusPyPI versionDownloadsDownloads

MidtransPython!

这是midtrans支付api的官方pythonapi客户端/库。访问https://midtrans.com。有关产品的更多信息,请参阅http://docs.midtrans.com上的文档以了解更多技术细节。

一。安装

1.a使用pip

pip install midtransclient

1.b手动安装

如果不使用pip,则可以克隆或download此存储库。 然后从midtransclient文件夹导入。

或者从repo文件夹运行pip安装。

pip install .

2.用法

2.1选择产品/方法

我们有2 different products付款,您可以使用:

  • Snap-可自定义的付款弹出窗口将出现在your web/app上(无重定向)。doc ref
  • Snap Redirect-客户需要重定向到midtrans托管的付款urldoc ref
  • Core API (VT-Direct)-基本的后端实现,您可以根据自己的喜好自定义嵌入在web/app上的前端(无重定向)。doc ref

选择一个你认为最适合你独特需求的。

2.2客户端初始化和配置

Midtrans Dashboard获取客户端密钥和服务器密钥

创建API客户端对象

# Create Core API instancecore_api=midtransclient.CoreApi(is_production=False,server_key='YOUR_SERVER_KEY',client_key='YOUR_CLIENT_KEY')
# Create Snap API instancesnap=midtransclient.Snap(is_production=False,server_key='YOUR_SERVER_KEY',client_key='YOUR_CLIENT_KEY')

也可以使用Snap.api_config.set( ... )重新设置配置 示例:

# initialize object, empty configsnap=midtransclient.Snap()# re-set full configsnap.api_config.set(is_production=False,server_key='YOUR_SERVER_KEY',client_key='YOUR_CLIENT_KEY')# re-set server_key onlysnap.api_config.set(server_key='YOUR_SERVER_KEY')# re-set is_production onlysnap.api_config.set(is_production=True)

您还可以直接从属性设置配置

# initialize object, empty configsnap=midtransclient.Snap()# set configsnap.api_config.is_production=Falsesnap.api_config.server_key='YOUR_SERVER_KEY'snap.api_config.client='YOUR_CLIENT_KEY'

2.2.a快照

您可以看到快照示例here

Snap类的可用方法

# return Snap API /transaction response as Dictionarydefcreate_transactions(parameter):# return Snap API /transaction token as Stringdefcreate_transactions_token(parameter):# return Snap API /transaction redirect_url as Stringdefcreate_transactions_redirect_url(parameter):

parameterSNAP Parameter的字典或json字符串

获取快照标记

# Create Snap API instancesnap=midtransclient.Snap(is_production=False,server_key='YOUR_SERVER_KEY',client_key='YOUR_CLIENT_KEY')# Build API parameterparam={"transaction_details":{"order_id":"test-transaction-123","gross_amount":200000},"credit_card":{"secure":True}}transaction=snap.create_transaction(param)transaction_token=transaction['token']# alternative way to create transaction_token:# transaction_token = snap.create_transaction_token(param)

当客户单击“付款”按钮时初始化Snap JS

用上面获得的transaction_token替换PUT_TRANSACTION_TOKEN_HERE

<html><body><buttonid="pay-button">Pay!</button><pre><divid="result-json">JSON result will appear here after payment:<br></div></pre><!-- TODO: Remove ".sandbox" from script src URL for production environment. Also input your client key in "data-client-key" --><scriptsrc="https://app.sandbox.midtrans.com/snap/snap.js"data-client-key="<Set your ClientKey here>"></script><scripttype="text/javascript">document.getElementById('pay-button').onclick=function(){// SnapToken acquired from previous stepsnap.pay('PUT_TRANSACTION_TOKEN_HERE',{// OptionalonSuccess:function(result){/* You may add your own js here, this is just example */document.getElementById('result-json').innerHTML+=JSON.stringify(result,null,2);},// OptionalonPending:function(result){/* You may add your own js here, this is just example */document.getElementById('result-json').innerHTML+=JSON.stringify(result,null,2);},// OptionalonError:function(result){/* You may add your own js here, this is just example */document.getElementById('result-json').innerHTML+=JSON.stringify(result,null,2);}});};</script></body></html>

实现通知处理程序

Refer to this section

2.2.b快照重定向

也可用作示例here

获取付款页的重定向URL

# Create Snap API instancesnap=midtransclient.Snap(is_production=False,server_key='YOUR_SERVER_KEY',client_key='YOUR_CLIENT_KEY')# Build API parameterparam={"transaction_details":{"order_id":"test-transaction-123","gross_amount":200000},"credit_card":{"secure":True}}transaction=snap.create_transaction(param)transaction_redirect_url=transaction['redirect_url']# alternative way to create redirect_url:# transaction_redirect_url = snap.create_redirect_url(param)

实现通知处理程序

Refer to this section

2.2.c核心api(vt direct)

您可以看到一些核心api示例here

CoreApi类的可用方法

defcharge(self,parameters=dict()):"""    Trigger `/charge` API call to Core API    :param parameters: dictionary of Core API JSON body as parameter, will be converted to JSON    (more params detail refer to: https://api-docs.midtrans.com)    :return: Dictionary from JSON decoded response    """defcapture(self,parameters=dict()):"""    Trigger `/capture` API call to Core API    Capture is only used for pre-authorize transaction only    :param parameters: dictionary of Core API JSON body as parameter, will be converted to JSON    (more params detail refer to: https://api-docs.midtrans.com)    :return: Dictionary from JSON decoded response    """defcard_register(self,parameters=dict()):"""    Trigger `/card/register` API call to Core API    :param parameters: dictionary of Core API JSON body as parameter, will be converted to JSON    (more params detail refer to: https://api-docs.midtrans.com)    :return: Dictionary from JSON decoded response    """defcard_token(self,parameters=dict()):"""    Trigger `/token` API call to Core API    :param parameters: dictionary of Core API JSON body as parameter, will be converted to JSON    (more params detail refer to: https://api-docs.midtrans.com)    :return: Dictionary from JSON decoded response    """defcard_point_inquiry(self,token_id):"""    Trigger `/point_inquiry/<token-id>` API call to Core API    :param parameters: dictionary of Core API JSON body as parameter, will be converted to JSON    (more params detail refer to: https://api-docs.midtrans.com)    :return: Dictionary from JSON decoded response    """

parameterCore API Parameter的字典或json字符串

信用卡获得代币

应在前端处理get令牌请参阅API docs

信用卡费用
# Create Core API instancecore_api=midtransclient.Snap(is_production=False,server_key='YOUR_SERVER_KEY',client_key='YOUR_CLIENT_KEY')# Build API parameterparam={"payment_type":"credit_card","transaction_details":{"gross_amount":12145,"order_id":"test-transaction-54321",},"credit_card":{"token_id":'CREDIT_CARD_TOKEN',# change with your card token"authentication":True}}# charge transactioncharge_response=core_api.charge(param)print('charge_response:')print(charge_response)

信用卡3DS身份验证

信用卡收费结果可能包含用于3DS身份验证的redirect_url。3DS身份验证应在前端处理请参考API docs

有关信用卡3DS交易的完整示例,请参阅:

2.3处理http通知

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.

创建单独的web端点(通知url)以接收http post通知回调/webhook。 当事务状态更改时,将发送http通知。 示例也可用here

# Create Core API / Snap instance (both have shared `transactions` methods)api_client=midtransclient.CoreApi(is_production=False,server_key='YOUR_SERVER_KEY',client_key='YOUR_CLIENT_KEY')status_response=api_client.transactions.notification(mock_notification)order_id=status_response['order_id']transaction_status=status_response['transaction_status']fraud_status=status_response['fraud_status']print('Transaction notification received. Order ID: {0}. Transaction status: {1}. Fraud status: {3}'.format(order_id,transaction_status,fraud_status))# Sample transaction_status handling logiciftransaction_status=='capture':iffraud_status=='challenge':# TODO set transaction status on your databaase to 'challenge'elseiffraud_status=='accept':# TODO set transaction status on your databaase to 'success'elseiftransaction_status=='cancel'ortransaction_status=='deny'ortransaction_status=='expire':# TODO set transaction status on your databaase to 'failure'elseiftransaction_status=='pending':# TODO set transaction status on your databaase to 'pending' / waiting payment

2.4交易行为

也可作为示例here

获取状态

# get status of transaction that already recorded on midtrans (already `charge`-ed) status_response=api_client.transactions.status('YOUR_ORDER_ID OR TRANSACTION_ID')

获取状态B2B
# get transaction status of VA b2b transactionstatusb2b_response=api_client.transactions.statusb2b('YOUR_ORDER_ID OR TRANSACTION_ID')

批准交易

# approve a credit card transaction with `challenge` fraud statusapprove_response=api_client.transactions.approve('YOUR_ORDER_ID OR TRANSACTION_ID')

拒绝交易
# deny a credit card transaction with `challenge` fraud statusdeny_response=api_client.transactions.deny('YOUR_ORDER_ID OR TRANSACTION_ID')

取消交易
# cancel a credit card transaction or pending transactioncancel_response=api_client.transactions.cancel('YOUR_ORDER_ID OR TRANSACTION_ID')

终止交易

# expire a pending transactionexpire_response=api_client.transactions.expire('YOUR_ORDER_ID OR TRANSACTION_ID')

退款交易
# refund a transaction (not all payment channel allow refund via API)param={"amount":5000,"reason":"Item out of stock"}refund_response=api_client.transactions.refund('YOUR_ORDER_ID OR TRANSACTION_ID',param)

三。示例

示例可在/examples文件夹中找到。 有:

获取帮助

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java:不解析XML的简单XML。例外   KIE Workbench的java自定义UI   java将元素从bucket移动到LinkedList,但有一个元素被完全删除   如何将java stream collect转换为scala   java运行AsynkTask多次不工作   java组织。xml。萨克斯。SAXParseException:cvccomplextype。2.4.c:匹配的通配符是严格的   java是一种计算排序算法所需时间的合适方法   java在O(logn)时间内对排序整数数组中具有相同数字的数字进行计数   xpages从当前数据库javaAgent调用另一个数据库的javaAgent   java如何在instagram中上传特定位置的所有照片   JavaApachePOI可以有效地删除多个列   java创建的对象数   java我可以在关闭连接时关闭Oracle JDBC自动提交吗?