我正在尝试记录确认的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"})如果能帮助我弄清楚这一点,我将非常感激。谢谢!
发布于 2021-10-28 20:18:39
在模拟任何支付时,尽量不要使用自己的凭据。
不久前我也遇到过类似的情况,我浪费了很多时间试图找出我的应用程序可能出了什么问题。
相反,转到developer.safaricom.co.ke站点的api部分,单击c2b api并在右侧卡片(黑色的那张)上,选择您的应用程序,在右下角,您将看到一个由三个水平条组成的按钮。点击它来复制测试凭证,希望你的应用程序现在可以工作了。
发布于 2020-01-01 04:49:37
如果您想要保留事务日志,则需要修改代码记录/将响应存储在数据库中
https://stackoverflow.com/questions/59540695
复制相似问题