【问题标题】:MPESA C2B Validation and Confirmation Transaction Not RespondingMPESA C2B 验证和确认交易无响应
【发布时间】:2019-12-31 06:56:34
【问题描述】:

我正在尝试记录已确认的 mpesa 交易。我在 safaricom 沙盒上运行它们。我的register url 函数和simulate_transaction 都从我的本地终端返回成功。 但是,我在 heroku 上由应用程序托管,并且那里的日志没有显示任何类型的响应(我有基于函数的视图,其中包含打印两个事务的代码。)

我的c2b.py

import keys
import requests
from requests.auth import HTTPBasicAuth
# import lipanampesa

# Getting MPESA Access Token
consumer_key = keys.consumer_key
consumer_secret = keys.consumer_secret
api_URL = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"

try:
    r = requests.get(api_URL, auth=HTTPBasicAuth(
        consumer_key, consumer_secret))
except:
    r = requests.get(api_URL, auth=HTTPBasicAuth(
        consumer_key, consumer_secret), verify=False)
print(r.json())
json_response = r.json()
my_access_token = json_response["access_token"]


def register_url():
    access_token = my_access_token  # lipanampesa.my_access_token  # my_access_token
    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {"ShortCode": keys.shortcode,
               "ResponseType": "Completed",
               "ConfirmationURL": "https://sheltered-river-94769.herokuapp.com/api/payments/C2B-CONFIRMATION/",
               "ValidationURL": "https://sheltered-river-94769.herokuapp.com/api/payments/C2B-VALIDATION/",
               }

    try:
        response = requests.post(api_url, json=request, headers=headers)
    except:
        response = requests.post(
            api_url, json=request, headers=headers, verify=False)

    print(response.text)


register_url()


def simulate_c2btransaction():
    access_token = my_access_token
    api_url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {"ShortCode": keys.shortcode,
               # "CustomerPayBillOnline",  # CustomerBuyGoodsOnline
               "CommandID": "CustomerPayBillOnline",
               "Amount": "50",
               # phone_number sendng the trxn, starting with Xtrycode minus plus (+) sign
               "Msisdn": keys.test_msisdn,
               "BillRefNumber": "123456789",
               }

    try:
        response = requests.post(api_url, json=request, headers=headers)

    except:
        response = requests.post(
            api_url, json=request, headers=headers, verify=False)

    print(response.text)


simulate_c2btransaction()

我的urls.py

from django.contrib import admin
from django.urls import path, include, re_path
from django.views.generic.base import TemplateView
from mpesa.api.views import LNMCallbackUrlAPIView, C2BConfirmationAPIView, C2BValidationAPIView, TestConfirmation, TestValidation
#from django.conf.urls import patterns

# NOTE:
# URLS transacting with mpesa should not have words like mpesa,
# safaricom, etc
app_name = "mpesa-api"

urlpatterns = [
    path('lnm/', LNMCallbackUrlAPIView.as_view(), name="lnm-callback"),
    path('C2B-VALIDATION/', TestValidation, name="c2b-validation"),
    path('C2B-CONFIRMATION/', TestConfirmation,
         name="c2b-confirmation"),
    # path('C2B-VALIDATION/', C2BValidationAPIView.as_view(), name="c2b-validation"),
    # path('C2B-CONFIRMATION/', C2BConfirmationAPIView.as_view(),
    #      name="c2b-confirmation"),
]

我的views.py

@csrf_exempt
def TestValidation(request):
    mpesa_body =request.body.decode('utf-8')
    print(mpesa_body, "This is request data in validation")
    context = {
        "ResultCode": 0,
        "ResultDesc": "Accepted"
    }
    return JsonResponse(dict(context))
    # return Response({"ResultCode": 0, "ResultDesc": "Accepted"})


@csrf_exempt
def TestConfirmation(request):
    mpesa_body =request.body.decode('utf-8')
    print(mpesa_body, "This is request data in confirmation")
    context = {
        "ResultCode": 0,
        "ResultDesc": "Accepted"
    }
    return JsonResponse(dict(context))
    # return Response({"ResultCode": 0,"ResultDesc": "Accepted"})

我非常感谢任何帮助解决这个问题。谢谢!

【问题讨论】:

  • 你为你的生产环境解决了这个问题吗?

标签: python django api mpesa


【解决方案1】:

在模拟任何付款时尽量不要使用您自己的凭据。

我曾经遇到过类似的情况,我浪费了很多时间试图找出我的应用可能出了什么问题。

相反,请转到 developer.safaricom.co.ke 网站的 api 部分,点击 c2b api 并在右侧卡片(黑色)上,选择您的应用,然后在右下角,您会看到一个由三个水平条组成的按钮。 单击它以复制测试凭据,希望您的应用现在可以运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 2021-06-29
    • 2015-02-25
    • 2011-12-18
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    相关资源
    最近更新 更多