MPESA C2B验证和确认事务没有响应

2024-04-29 22:01:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的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"})

如果有人能帮我解决这个问题,我将不胜感激。谢谢!你知道吗


Tags: pathimporttokenapijsonurlaccessconsumer