M-Pesa Daraja API 集成:错误请求 - 无效电话号码

问题描述 投票:0回答:2

我正在使用 Daraja 门户将 M-Pesa 集成到我的应用程序中,但遇到了“错误请求 - 无效电话号码”错误的问题。尽管使用了有效的肯尼亚电话号码,但我无法成功继续。

代码和错误。

支付类

from api.services.payment.m_pesa_payment.mpes_credential import MpesaC2bCredential
import requests
import logging
import json
class MakePayment:
    @staticmethod
    def make_payment(payload):
        mepesa_acess_token = MpesaAccessToken()
        credential = MpesaC2bCredential()
        access_token = mepesa_acess_token.get_access_token()
        amount = payload["amount"]
        phone_number = payload["phone_number"]
        api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
        headers = {"Authorization": "Bearer %s" % access_token}
        
        request = {
        "BusinessShortCode": credential.short_code,
        "Password": mepesa_acess_token.generate_password(),
        "Timestamp": mepesa_acess_token.current_time,
        "TransactionType": "CustomerPayBillOnline",
        "Amount": amount,
        "PartyA": phone_number,
        "PartyB": credential.short_code,
        "PhoneNumber": phone_number,
        "CallBackURL": "https://ba3f-41-79-219-185.ngrok-free.app/payment",
        "AccountReference": "unknown",
        "TransactionDesc": "Testing stk push"
    }
        
        response = requests.post(api_url, json=request, headers=headers)
        
        response_data = response.json()
        logging.info("Mpesa request data {}".format(response_data))
        logging.info("Mpesa response info {}".format(response_data))

        return response_data

payload = {"phone_number":"+254708374149", "amount": 5000}   
print(MakePayment.make_payment(payload))

凭证类

class MpesaC2bCredential:
    mpesa_url = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'
    passe_key = 'xxxxxx'
    
    def __init__(self, consumer_key='xxxx', consumer_secret='xxx', api_urls = mpesa_url, short_code='174379',passe_key = passe_key):
        self.consumer_key = consumer_key
        self.consumer_secret = consumer_secret
        self.api_url = api_urls
        self.short_code = short_code
        self.api_url = api_urls
        self.passe_key = passe_key
        
        @property
        def consumer_key(self):
            return self.__consumer_key
        
        @consumer_key.setter
        def consumer_key(self, value):
            self.__consumer_key = value
            
        @property
        def consumer_secret(self):
            return self.__consumer_secret
        
        @consumer_secret.setter
        def consumer_secret(self, value):
            self.__consumer_secret = value
            
        @property
        def api_url(self):
            return self.__api_url
        
        @api_url.setter
        def api_url(self, value):
            self.__url = value
            
        @property
        def short_code(self):
            return self.__short_code
        
        @short_code.setter
        def short_code(self, value):
            self.__short_code = value
            
        @property
        def passe_key(self):
            return self.__short_code
        
        @passe_key.setter
        def short_code(self, value):
            self.__short_code = value
            



  


访问令牌类

from api.services.payment.m_pesa_payment.mpes_credential import MpesaC2bCredential
import requests
import json
from requests.auth import HTTPBasicAuth
from datetime import datetime
import base64
import logging
import time



class MpesaAccessToken:
    access_token_expiration = None
    current_time = datetime.now().strftime('%Y%m%d%H%M%S')
    
    def __init__(self, access_token = None, credential = MpesaC2bCredential(), current_time = current_time):
        self.access_token = access_token
        self.credential = credential
        self.current_time = current_time

        
    def get_access_token(self):
        try:
            res = requests.get(
                self.credential.api_url,
                auth=HTTPBasicAuth(self.credential.consumer_key, self.credential.consumer_secret),
            )
        except Exception as err:
            logging.error("Error {}".format(err))
            raise err
        else:
            token = res.json()["access_token"]
            self.headers = {"Authorization": "Bearer %s" % token}
            return token
        
    
    def generate_password(self):
        """Generates mpesa api password using the provided shortcode and passkey"""
        current_date = datetime.now()
        #timestamp = current_date.strftime("%Y%m%d%H%M%S")
        password_str = self.credential.short_code + self.credential.passe_key + self.current_time
        password_bytes = password_str.encode("ascii")
        return base64.b64encode(password_bytes).decode("utf-8")
    
 

错误信息:

{'requestId': '11231-10026868-1', 'errorCode': '400.002.02', 'errorMessage': 'Bad Request - Invalid PhoneNumber'}

flask mpesa
2个回答
1
投票

0
投票

客户端错误:

GET https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials
导致了
400 Bad Request
响应可能是什么问题

© www.soinside.com 2019 - 2024. All rights reserved.