在paytm网站上的集成步骤中,第一步是启动交易API ,他们为此提供了以下代码:https://developer.paytm.com/docs/initiate-transaction-api/?ref=payments
我将其复制粘贴到processOrder视图下的views.py中:
def processOrder(request):
transaction_id = datetime.datetime.now().timestamp()
data = json.loads(request.body)
if request.user.is_authenticated:
customer=request.user.customer
order, created=Order.objects.get_or_create(customer=customer, complete=False)
total=float(data['form']['total'])
order.transaction_id=transaction_id
if total == order.get_cart_total:
order.complete = True
order.save()
ShippingAddress.objects.create(
customer=customer,
order=order,
address=data['shipping']['address'],
city=data['shipping']['city'],
state=data['shipping']['state'],
zipcode=data['shipping']['zipcode'],
name=data['form']['name'],
email=data['form']['email'],
mobile=data['form']['mobile'],
)
paytmParams = dict()
paytmParams["body"] = {
"requestType" : "Payment",
"mid" : "YOUR_MID_HERE",
"websiteName" : "WEBSTAGING",
"orderId" : "ORDERID_98765",
"callbackUrl" : "https://localhost/handlerequest",
"txnAmount" : {
"value" : "1.00",
"currency" : "INR",
},
"userInfo" : {
"custId" : "CUST_001",
},
}
checksum = PaytmChecksum.generateSignature(json.dumps(paytmParams["body"]), "YOUR_MERCHANT_KEY")
paytmParams["head"] = {
"signature" : checksum
}
post_data = json.dumps(paytmParams)
# for Staging
url = "https://securegw-stage.paytm.in/theia/api/v1/initiateTransaction?mid=YOUR_MID_HERE&orderId=ORDERID_98765"
mid=YOUR_MID_HERE&orderId=ORDERID_98765"
response = requests.post(url, data = post_data, headers = {"Content-type": "application/json"}).json()
print(response)
return render(request,"paytm.html",{'paytmParams':paytmParams})
return JsonResponse('Payment Complete',safe=False)
这是我的html js
html:<button class="btnabc btnabc-secondary btnabc-lg d-none" id='payment-info'>Proceed</button>
<script type="text/javascript">
var total = '{{order.get_cart_total}}'
var form = document.getElementById('form')
form.addEventListener('submit', function (e) {
e.preventDefault()
console.log('form submitted..')
document.getElementById('form-button').classList.add("d-none");
document.getElementById('payment-info').classList.remove("d-none");
})
document.getElementById('payment-info').addEventListener('click', function (e) {
submitFormData()
})
function submitFormData() {
console.log('Payment Button Clicked')
var userFormData = {
'name': null,
'email': null,
'mobile':null,
'total': total,
}
var shippingInfo = {
'address': null,
'city': null,
'state': null,
'zipcode': null,
}
shippingInfo.address = form.address.value
shippingInfo.city = form.city.value
shippingInfo.state = form.state.value
shippingInfo.zipcode = form.zipcode.value
userFormData.name=form.name.value
userFormData.email=form.email.value
userFormData.mobile=form.mobile.value
var url = "/process_order/"
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken,
},
body:JSON.stringify({'form': userFormData, 'shipping': shippingInfo }),
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
alert('Transaction Completed')
window.location.href = "{% url 'index' %}"
})
}
</script>
这是我的 paytm.html
<body>
<form action="https://securegw-stage.paytm.in/theia/processTransaction" method="POST" name="paytm">
{% for key,value in paytmParams.items %}
<input type="hidden" name="{{key}}" value="{{value}}">
{% endfor %}
</form>
</body>
<script>
document.paytm.submit()
</script>
这些是我点击付款按钮时遇到的错误:
POST http://127.0.0.1:8000/process_order/ 500 (Internal Server Error)
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
Promise.then (async)
如果以前我不上传任何paytm集成代码,则单击时,我会成功完成交易的警报(如我的代码所示),但现在不起作用。我不知道是什么问题。任何帮助都将被申请。谢谢。
答案 0 :(得分:1)
您的json之一似乎格式错误。基于错误,我认为它是html而不是JSON。
您可以手动记录每个api网络调用的请求和响应,以查看它是否为有效的json。您也可以使用Hoss之类的工具。它们具有用于javascript和python的代理,可自动跟踪所有API调用并显示它们,以便您检查任何问题