【问题标题】:I am trying to intergrate mpesa payment api to dynamically pass the store price and the correct response我正在尝试集成 mpesa 支付 api 以动态传递商店价格和正确响应
【发布时间】:2020-05-22 13:08:52
【问题描述】:

我一直在尝试集成 mpesa API 并动态付款控制台给了我一条成功消息,而我没有错误地输入我的密码,谢谢。

请帮我解决这个问题,我还是 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)

【问题讨论】:

    标签: python django mpesa


    【解决方案1】:

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

    global totalcart = order.get_cart_total
    

    先然后

    "Amount" : totalcart'
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-01
      • 2019-08-25
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2015-11-10
      相关资源
      最近更新 更多