我很难理解以下行为。
我正在整合某个移动支付API。在运行支付功能时,支付API被呼叫,并且它反过来应该返回确认收到请求的响应以及在用户的移动电话上发射STKPush屏幕后不久(该API由电信公司提供)处理移动支付)。我首先编写了简单的Python脚本来测试集成,它们运行良好。以下是我在脚本中运行它的方法的摘录:
{u'CustomerMessage': u'Success. Request accepted for processing', u'CheckoutRequestID': u'ws_CO_07112017182534915', u'ResponseDescription': u'Success. Request accepted for processing', u'MerchantRequestID': u'8955-555026-1', u'ResponseCode': u'0'}
直接运行脚本会从付款API返回相应的响应:
@celery.task
def lipa(token, payload):
headers = {'Authorization': 'Bearer {0}'.format(token), 'Content-Type': "application/json"}
saf_url = "https://xxxxxxxxxx.co.ke/pay/stkpush/v1/processrequest"
r = requests.post(saf_url, headers=headers, json=payload)
print r.json()
@api.route('/payment/pay/', methods=['POST'])
def make_payment():
if not 'Authorization' in request.headers:
abort(401)
_data = request.headers['Authorization'].encode('ascii', 'ignore')
token = str.replace(str(_data), 'Bearer ', '')
if token == application.config['PAYMENT_API_KEY']:
pass
else:
return jsonify({"error": "Invalid authentication token."})
time = str(datetime.datetime.now()).split(".")[0].replace("-", "").replace(" ", "").replace(":", "")
password = "{0}{1}{2}".format(str(application.config.get('SHORTCODE')), application.config.get('PASSCODE'), time)
encoded = base64.b64encode(password)
data = request.get_json(force=True)
try:
if data["sandbox"] == "true":
amount = 1
else:
amount = data.get('amount')
except KeyError:
amount = data.get('amount')
callback_url = "{0}/api/payment/mpesa/callback".format(application.config.get('SITE'))
payload = {
"BusinessShortCode": application.config.get('SHORTCODE'),
"Password": encoded,
"Timestamp": time,
"TransactionType": "PayOnline",
"Amount": amount,
"PartyA": data.get('phone_number'),
"PartyB": application.config.get('SHORTCODE'),
"PhoneNumber": data.get('phone_number'),
"CallBackURL": callback_url,
"AccountReference": str(uuid.uuid4()),
"TransactionDesc": data.get('description')
}
token = str(get_oauth_token())
task = lipa.apply_async(args=[token, payload])
return Response()
并触发用户手机上的STKpush操作(由付款API完成。)。
但是,在Flask Web应用程序控制器中运行相同的逻辑部分会设置返回相应的响应,但永远不会触发STKPush操作。我试过运行它作为异步任务是芹菜,但我仍然得到相同。
{{1}}
可能导致这种行为差异的原因是什么?
提前致谢。
答案 0 :(得分:0)
原来API返回了一个尚未处理的错误。它现在可以正常工作。