我一直在尝试集成 mpesa API 并动态付款我设法在我的手机 stk push 上看到我的购物车中的正确值但是在网络浏览器上我得到一个错误本地变量“订单”在分配之前引用并且控制台给了我没有我错误地输入我的密码的成功消息,谢谢。
请帮我解决这个问题我仍然是PYTHON的新手。
def mpesaToken(request):
ckey = 'hOdosm1nU1tXMsT0yPTGjnHDgIUSxx8z'
csecret = 'mrgGSIdbDOZaVrZl'
apiurl = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'
r = requests.get(apiurl, auth=HTTPBasicAuth(ckey, csecret))
mptoken = json.loads(r.json)
valida = mptoken['access_token']
return HttpResponse(valida)
def lipaOnline(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
else:
items = []
accesstoken = MpesaToken.valida
apiurl = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
headers = {"Authorization":"Bearer %s" % accesstoken}
request = {
"BusinessShortCode": Lipa.bscode,
"Password": Lipa.decode_pass,
"Timestamp": Lipa.lipatime,
"TransactionType": "CustomerPayBillOnline",
"Amount": order.get_cart_total,
"PartyA": 25471234567,
"PartyB": Lipa.bscode,
"PhoneNumber": 25471234567,
"CallBackURL": "https://sandbox.safaricom.co.ke/mpesa/",
"AccountReference": "HAC",
"TransactionDesc": "testeltd"}
response = requests.post(apiurl, json=request, headers=headers)
return HttpResponse('Success')
print(request)