0

我一直在尝试集成 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)
4

1 回答 1

0

问题是您正在尝试从计算值中选择一个值,它不明白它应该query(order.get_cart_total)在将该值分配给您的其他变量之前检索它,Amount所以对我有用的是我在调用它之前先设置变量并确保您也可以将其作为全局变量。所以简单地做

global totalcart = order.get_cart_total

首先 然后

"Amount" : totalcart'
...
于 2021-03-08T11:36:47.220 回答