-2

我很难理解以下行为。

我正在集成某个移动支付 API。在运行支付功能时,支付 API 会被调用,然后它应该返回一个响应,确认收到请求,并在用户手机上触发 STKPush 屏幕后不久(该 API 由一家处理移动支付的电信公司提供)。我首先编写了简单的 Python 脚本来测试集成,它们运行良好。下面是我如何在脚本中运行它的摘录:

def get_oauth_token():
    resource_url = "https://xxxxxxxxxxx.co.ke/oauth/v1/generate?grant_type=client_credentials"
    key = "#################"
    secret = "################"
    r = requests.get(resource_url, auth=HTTPBasicAuth(key, secret))
    return r.json()['access_token']


def lipa_online(token):
    time = str(datetime.datetime.now()).split(".")[0].replace("-", "").replace(" ", "").replace(":", "")
    password = "{0}{1}{2}".format("174379", "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", time)
    encoded = base64.b64encode(password)
    payload = {
      "BusinessShortCode": XXXXXX,
      "Password": encoded,
      "Timestamp": time,
      "TransactionType": "PayOnline",
      "Amount": 1,
      "PartyA": 25470000000,
      "PartyB": XXXXXX,
      "PhoneNumber": 25470000000,
      "CallBackURL": "https://97ff8c8c.ngrok.io/callback",
      "AccountReference": "13dbd6hg",
      "TransactionDesc": "Just a trial"
    }
    headers = {'Authorization': 'Bearer {0}'.format(token), 'Content-Type': "application/json"}
    r = requests.post("https://xxxxxxxx.co.ke/pay/stkpush/v1/processrequest", headers=headers, json=payload)
    print r.json()

if __name__ == "__main__":
    token = get_oauth_token()
    #The line below fires the payment action.
    request_id = pay_online(token)

直接运行脚本会从支付 API 返回相应的响应:

{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'}

并在用户手机上启动 STKpush 操作(由支付 API 完成)。

但是,在 Flask Web 应用程序控制器中作为逻辑的一部分运行相同的操作会设法返回适当的响应,但永远不会触发 STKPush 操作。我尝试将它作为异步任务运行是 Celery,但我仍然得到相同的结果。

@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()

什么可能导致这种行为差异?

提前致谢。

4

1 回答 1

0

原来 API 返回了一个尚未处理的错误。现在可以正常工作了。

于 2017-11-08T09:52:34.883 回答