【问题标题】:How can I integrate SSLCommerze to my django app?如何将 SSLCommerze 集成到我的 django 应用程序中?
【发布时间】:2021-09-17 08:36:51
【问题描述】:

我创建了一个电子商务网站。现在我想整合付款方式。通过将 SSLCommerce 添加到我的网站,所有付款方式都将在孟加拉国处理。但我不知道如何将它添加到我的 Django 应用程序中。请帮忙!

他们说了一些话。但我没有得到它。这是他们的 github 仓库https://github.com/sslcommerz/SSLCommerz-Python?fbclid=IwAR0KkEH3H-AOwaWneQy0POGkTw6O3vvL9NiRM4amflyQEt54_W1g1rgYB48

【问题讨论】:

  • pip install sslcommerz-lib 怎么样

标签: django session integration payment


【解决方案1】:

有一个名为“sslcommerz-lib”的包装库。要使用它,您首先需要an account on their sandbox environment。完成注册后,从电子邮件中收集您的用户凭据。

  1. 首先用pip install sslcommerz-lib安装包
  2. 在您的模块上导入库from sslcommerz_lib import SSLCOMMERZ
  3. 使用沙盒用户凭据实例化 SSLCOMMERZ 类的对象
sslcz = SSLCOMMERZ({ 'store_id': <your_store_id>, 'store_pass': <your_password>, 'issandbox': True })
  1. 构建包含有关交易和客户的一些信息的字典。在实际应用中,这些数据大部分都是从用户输入中收集的。
data = {
    'total_amount': "100.26",
    'currency': "BDT",
    'tran_id': "tran_12345",
    'success_url': "http://127.0.0.1:8000/payment-successful", # if transaction is succesful, user will be redirected here
    'fail_url': "http://127.0.0.1:8000/payment-failed", # if transaction is failed, user will be redirected here
    'cancel_url': "http://127.0.0.1:8000/payment-cancelled", # after user cancels the transaction, will be redirected here
    'emi_option': "0",
    'cus_name': "test",
    'cus_email': "test@test.com",
    'cus_phone': "01700000000",
    'cus_add1': "customer address",
    'cus_city': "Dhaka",
    'cus_country': "Bangladesh",
    'shipping_method': "NO",
    'multi_card_name': "",
    'num_of_item': 1,
    'product_name': "Test",
    'product_category': "Test Category",
    'product_profile': "general",
}
  1. 获取 api 响应
response = sslcz.createSession(post_body)

  1. 完成更新数据库等杂务后,将用户从我们之前得到的响应中重定向到“GatewayPageURL” -
from django.shortcuts import redirect
return redirect(response['GatewayPageURL'])

【讨论】:

  • 这里你完成了使用 SSLCommerz Session API 创建一个交易会话,之后我们如何创建一个 IPN(即时支付通知)监听器来接收成功的支付通知,然后 SSLCommerz 将客户重定向到你的回调地址
【解决方案2】:

SSLCOMMERZ - Python (sslcommerz-lib) 注意:如果您在我们的沙盒环境中使用此包装器,则 issandbox 为 true,live issandbox 为 false。 (详细信息:测试或沙盒帐户)。

settings = { 'store_id': 'testbox', 'store_pass': 'qwerty', 'issandbox': True } 
sslcommerz = SSLCOMMERZ(settings)

安装

pip install sslcommerz-lib

身份验证密钥 您可以在 API 文档页面找到您的 store_id 和 store_pass。在 SSLCOMMERZ 上创建一个帐户,登录并访问此链接:https://developer.sslcommerz.com/registration/

用法 创建初始付款请求会话

from sslcommerz_lib import SSLCOMMERZ 
settings = { 'store_id': 'testbox', 'store_pass': 'qwerty', 'issandbox': True }
sslcz = SSLCOMMERZ(settings)
post_body = {}
post_body['total_amount'] = 100.26
post_body['currency'] = "BDT"
post_body['tran_id'] = "12345"
post_body['success_url'] = "your success url"
post_body['fail_url'] = "your fail url"
post_body['cancel_url'] = "your cancel url"
post_body['emi_option'] = 0
post_body['cus_name'] = "test"
post_body['cus_email'] = "test@test.com"
post_body['cus_phone'] = "01700000000"
post_body['cus_add1'] = "customer address"
post_body['cus_city'] = "Dhaka"
post_body['cus_country'] = "Bangladesh"
post_body['shipping_method'] = "NO"
post_body['multi_card_name'] = ""
post_body['num_of_item'] = 1
post_body['product_name'] = "Test"
post_body['product_category'] = "Test Category"
post_body['product_profile'] = "general"


response = sslcz.createSession(post_body) # API response
print(response)
# Need to redirect user to response['GatewayPageURL']

使用 IPN 验证付款 从 sslcommerz_lib 导入 SSLCOMMERZ

settings = { 'store_id': 'test_testemi', 'store_pass': 'test_testemi@ssl', 'issandbox': True } 

sslcz = SSLCOMMERZ(settings)
post_body = {}
post_body['tran_id'] = '5E121A0D01F92'
post_body['val_id'] = '200105225826116qFnATY9sHIwo'
post_body['amount'] = "10.00"
post_body['card_type'] = "VISA-Dutch Bangla"
post_body['store_amount'] = "9.75"
post_body['card_no'] = "418117XXXXXX6675"
post_body['bank_tran_id'] = "200105225825DBgSoRGLvczhFjj"
post_body['status'] = "VALID"
post_body['tran_date'] = "2020-01-05 22:58:21"
post_body['currency'] = "BDT"
post_body['card_issuer'] = "TRUST BANK, LTD."
post_body['card_brand'] = "VISA"
post_body['card_issuer_country'] = "Bangladesh"
post_body['card_issuer_country_code'] = "BD"
post_body['store_id'] = "test_testemi"
post_body['verify_sign'] = "d42fab70ae0bcbda5280e7baffef60b0"
post_body['verify_key'] = "amount,bank_tran_id,base_fair,card_brand,card_issuer,card_issuer_country,card_issuer_country_code,card_no,card_type,currency,currency_amount,currency_rate,currency_type,risk_level,risk_title,status,store_amount,store_id,tran_date,tran_id,val_id,value_a,value_b,value_c,value_d"
post_body['verify_sign_sha2'] = "02c0417ff467c109006382d56eedccecd68382e47245266e7b47abbb3d43976e"
post_body['currency_type'] = "BDT"
post_body['currency_amount'] = "10.00"
post_body['currency_rate'] = "1.0000"
post_body['base_fair'] = "0.00"
post_body['value_a'] = ""
post_body['value_b'] = ""
post_body['value_c'] = ""
post_body['value_d'] = ""
post_body['risk_level'] = "0"
post_body['risk_title'] = "Safe"
if sslcz.hash_validate_ipn(post_body):
    response = sslcz.validationTransactionOrder(post_body['val_id'])
    print(response)
else:
    print("Hash validation failed")

通过 sessionkey 获取付款请求的状态或详细信息

from sslcommerz_lib import SSLCOMMERZ 
settings = { 'store_id': 'testbox', 'store_pass': 'qwerty', 'issandbox': True }
sslcz = SSLCOMMERZ(settings)

sessionkey = 'A8EF93B75B8107E4F36049E80B4F9149'
response = sslcz.transaction_query_session(sessionkey)
print(response)

通过 tranid 获取付款请求的状态或详细信息

from sslcommerz_lib import SSLCOMMERZ 
settings = { 'store_id': 'testbox', 'store_pass': 'qwerty', 'issandbox': True }
sslcz = SSLCOMMERZ(settings)

tranid = '59C2A4F6432F8'
response = sslcz.transaction_query_tranid(tranid)
print(response)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 2017-06-01
    • 2015-07-23
    • 2012-09-07
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多