1

我正在尝试记录确认的 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"})

我真的很感激任何帮助解决这个问题。谢谢!

4

1 回答 1

0

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

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

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

于 2021-10-28T20:18:39.467 回答