def test_successful_create_incoming_payment_request(self): response = requests.post( headers=ReceivePaymentTestCase.header, json=json_builder.mpesa_payment( json_builder.links( "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d" ), json_builder.amount('KES', 'python_sdk_value'), json_builder.subscriber('first_name', 'last_name', "+254712345678", 'Null'), 'payment_channel', 'K112233'), data=None, url=ReceivePaymentTestCase.incoming_payments_obj._build_url( receive_payments.CREATE_RECEIVE_MPESA_PAYMENT_PATH)) self.assertEqual(response.status_code, 201)
def test_mpesa_payment_method_with_non_str_required_arguments_fails(self): with self.assertRaises(exceptions.InvalidArgumentError): mpesa_payment_amount = json_builder.amount(currency=15462, value=15462) mpesa_links = json_builder.links(callback_url=15462) mpesa_payment_metadata = json_builder.metadata(**{15462: 15462}) mpesa_payment_subscriber = json_builder.subscriber(first_name=15462, last_name=15462, phone=15462, email=15462) json_builder.mpesa_payment(mpesa_links, mpesa_payment_amount, mpesa_payment_subscriber, 15462, 15462)
def test_mpesa_payment_method_without_required_arguments_fails(self): with self.assertRaises(exceptions.InvalidArgumentError): mpesa_payment_amount = json_builder.amount(currency=None, value=None) mpesa_links = json_builder.links(callback_url=None) mpesa_payment_metadata = json_builder.metadata(**{None: None}) mpesa_payment_subscriber = json_builder.subscriber(first_name=None, last_name=None, phone=None, email=None) json_builder.mpesa_payment(mpesa_links, mpesa_payment_amount, mpesa_payment_subscriber, None, None)
def test_subscriber_method_with_non_str_required_arguments_fails(self): with self.assertRaises(exceptions.InvalidArgumentError): json_builder.subscriber(first_name=98745, last_name=98745, phone=98745)
def test_subscriber_method_with_all_required_arguments_succeeds(self): subscriber_json = json_builder.subscriber(first_name='Jon', last_name='Snow', phone='+89456137822', ) self.assertIsNotNone(subscriber_json)
def create_payment_request(self, kwargs): """ Creates a request for the reception of payments from MPESA users. Returns a request response object < class, 'requests.models.Response'> :param kwargs: The values constitute all user input. :type kwargs: dict :return: requests.models.Response """ if 'access_token' not in kwargs: raise exceptions.InvalidArgumentError('Access Token not given.') if 'first_name' not in kwargs or \ 'last_name' not in kwargs or \ 'callback_url' not in kwargs or \ 'payment_channel' not in kwargs or \ 'phone_number' not in kwargs or \ 'till_number' not in kwargs or \ 'email' not in kwargs or \ 'amount' not in kwargs: raise exceptions.InvalidArgumentError('Invalid arguments for creating Incoming Payment Request.') if 'currency' not in kwargs: currency = 'KES' if 'metadata' not in kwargs: mpesa_payment_metadata = '' # iterate through kwargs if 'access_token' in kwargs: bearer_token = kwargs['access_token'] if 'phone_number' in kwargs: phone_number = kwargs['phone_number'] if validation.validate_phone_number(phone_number) is False: pass if 'email' in kwargs: email = kwargs['email'] validation.validate_email(email) if 'currency' in kwargs: currency = 'KES' if 'metadata' in kwargs: mpesa_payment_metadata = json_builder.metadata(kwargs['pay_metadata']) # define headers headers = dict(self._headers) # validate bearer_token validation.validate_string_arguments(bearer_token) # add bearer token headers['Authorization'] = 'Bearer ' + bearer_token + '' # build create mpesa payment request url mpesa_payment_request_url = self._build_url(CREATE_RECEIVE_MPESA_PAYMENT_PATH) # define amount JSON object mpesa_payment_request_amount = json_builder.amount(currency=currency, value=kwargs['amount']) # define links JSON object mpesa_payment_request_links = json_builder.links(callback_url=kwargs['callback_url']) # define subscriber JSON object mpesa_payment_subscriber = json_builder.subscriber(first_name=kwargs['first_name'], last_name=kwargs['last_name'], phone_number=phone_number, email=email) # define MPESA payment request JSON object mpesa_payment_request_payload = json_builder.mpesa_payment(mpesa_links=mpesa_payment_request_links, mpesa_payment_amount=mpesa_payment_request_amount, mpesa_payment_subscriber=mpesa_payment_subscriber, metadata=mpesa_payment_metadata, payment_channel=kwargs['payment_channel'], till_number=kwargs['till_number']) return self._make_requests(headers=headers, method='POST', url=mpesa_payment_request_url, payload=mpesa_payment_request_payload)
def create_payment_request(self, bearer_token, callback_url, first_name, last_name, payment_channel, phone, till_number, value, currency='KES', **kwargs): """ Creates a request for the reception of payments from MPESA users. Returns a request response object < class, 'requests.models.Response'> :param bearer_token: Access token to be used to make calls to the Kopo Kopo API. :type bearer_token: str :param callback_url: Callback URL where the result of the MPESA payment request will be posted. :type callback_url: str :param currency: Currency of amount being transacted :type currency: str :param first_name: First name of the subscriber. :type first_name: str :param last_name: Last name of the subscriber. :type last_name: str :param phone: Phone number of the subscriber from which the payment will be made. :type phone: str :param payment_channel: Payment channel to be used eg. MPESA. :type payment_channel: str :param till_number: Till to which the payment will be made. :type till_number: str :param value: Value of money to be received :type value: str :param kwargs: :type kwargs: dict :return: requests.models.Response """ # validate phone number if validation.validate_phone_number(phone) is False: pass # validate email address if present if 'email' in kwargs: validation.validate_email(str(kwargs['email'])) email = kwargs['email'] else: email = 'Null' # define headers headers = dict(self.headers) # validate bearer_token validation.validate_string_arguments(bearer_token) # add bearer token headers['Authorization'] = 'Bearer ' + bearer_token + '' # build create mpesa payment request url mpesa_payment_request_url = self._build_url( CREATE_RECEIVE_MPESA_PAYMENT_PATH) # define amount JSON object mpesa_payment_request_amount = json_builder.amount(currency=currency, value=value) # define links JSON object mpesa_payment_request_links = json_builder.links( callback_url=callback_url) # define metadata JSON object mpesa_payment_metadata = json_builder.metadata(', '.join( ['{}={}'.format(k, v) for k, v in kwargs.items()])) # define subscriber JSON object mpesa_payment_subscriber = json_builder.subscriber( first_name=first_name, last_name=last_name, phone=phone, email=email) # define MPESA payment request JSON object mpesa_payment_request_payload = json_builder.mpesa_payment( mpesa_links=mpesa_payment_request_links, mpesa_payment_amount=mpesa_payment_request_amount, mpesa_payment_subscriber=mpesa_payment_subscriber, metadata=mpesa_payment_metadata, payment_channel=payment_channel, till_number=till_number) return self._make_requests(headers=headers, method='POST', url=mpesa_payment_request_url, payload=mpesa_payment_request_payload)