def test_processing_of_callback_payload(self): mpesa_account = frappe.db.get_value( "Payment Gateway Account", {"payment_gateway": "Mpesa-Payment"}, "payment_account") frappe.db.set_value("Account", mpesa_account, "account_currency", "KES") frappe.db.set_value("Customer", "_Test Customer", "default_currency", "KES") pos_invoice = create_pos_invoice(do_not_submit=1) pos_invoice.append( "payments", { "mode_of_payment": "Mpesa-Payment", "account": mpesa_account, "amount": 500 }) pos_invoice.contact_mobile = "093456543894" pos_invoice.currency = "KES" pos_invoice.save() pr = pos_invoice.create_payment_request() # test payment request creation self.assertEqual(pr.payment_gateway, "Mpesa-Payment") # submitting payment request creates integration requests with random id integration_req_ids = frappe.get_all( "Integration Request", filters={ "reference_doctype": pr.doctype, "reference_docname": pr.name, }, pluck="name", ) callback_response = get_payment_callback_payload( Amount=500, CheckoutRequestID=integration_req_ids[0]) verify_transaction(**callback_response) # test creation of integration request integration_request = frappe.get_doc("Integration Request", integration_req_ids[0]) # test integration request creation and successful update of the status on receiving callback response self.assertTrue(integration_request) self.assertEqual(integration_request.status, "Completed") pos_invoice.reload() integration_request.reload() self.assertEqual(pos_invoice.mpesa_receipt_number, "LGR7OWQX0R") self.assertEqual(integration_request.status, "Completed") frappe.db.set_value("Customer", "_Test Customer", "default_currency", "") integration_request.delete() pr.reload() pr.cancel() pr.delete() pos_invoice.delete()
def test_processing_of_callback_payload(self): create_mpesa_settings(payment_gateway_name="Payment") mpesa_account = frappe.db.get_value( "Payment Gateway Account", {"payment_gateway": 'Mpesa-Payment'}, "payment_account") frappe.db.set_value("Account", mpesa_account, "account_currency", "KES") frappe.db.set_value("Customer", "_Test Customer", "default_currency", "KES") pos_invoice = create_pos_invoice(do_not_submit=1) pos_invoice.append( "payments", { 'mode_of_payment': 'Mpesa-Payment', 'account': mpesa_account, 'amount': 500 }) pos_invoice.contact_mobile = "093456543894" pos_invoice.currency = "KES" pos_invoice.save() pr = pos_invoice.create_payment_request() # test payment request creation self.assertEquals(pr.payment_gateway, "Mpesa-Payment") callback_response = get_payment_callback_payload() verify_transaction(**callback_response) # test creation of integration request integration_request = frappe.get_doc("Integration Request", "ws_CO_061020201133231972") # test integration request creation and successful update of the status on receiving callback response self.assertTrue(integration_request) self.assertEquals(integration_request.status, "Completed") pos_invoice.reload() integration_request.reload() self.assertEquals(pos_invoice.mpesa_receipt_number, "LGR7OWQX0R") self.assertEquals(integration_request.status, "Completed") frappe.db.set_value("Customer", "_Test Customer", "default_currency", "")
def test_processing_of_only_one_succes_callback_payload(self): mpesa_account = frappe.db.get_value( "Payment Gateway Account", {"payment_gateway": "Mpesa-Payment"}, "payment_account") frappe.db.set_value("Account", mpesa_account, "account_currency", "KES") frappe.db.set_value("Mpesa Settings", "Payment", "transaction_limit", "500") frappe.db.set_value("Customer", "_Test Customer", "default_currency", "KES") pos_invoice = create_pos_invoice(do_not_submit=1) pos_invoice.append( "payments", { "mode_of_payment": "Mpesa-Payment", "account": mpesa_account, "amount": 1000 }) pos_invoice.contact_mobile = "093456543894" pos_invoice.currency = "KES" pos_invoice.save() pr = pos_invoice.create_payment_request() # test payment request creation self.assertEqual(pr.payment_gateway, "Mpesa-Payment") # submitting payment request creates integration requests with random id integration_req_ids = frappe.get_all( "Integration Request", filters={ "reference_doctype": pr.doctype, "reference_docname": pr.name, }, pluck="name", ) # create random receipt nos and send it as response to callback handler mpesa_receipt_numbers = [ frappe.utils.random_string(5) for d in integration_req_ids ] callback_response = get_payment_callback_payload( Amount=500, CheckoutRequestID=integration_req_ids[0], MpesaReceiptNumber=mpesa_receipt_numbers[0], ) # handle response manually verify_transaction(**callback_response) # test completion of integration request integration_request = frappe.get_doc("Integration Request", integration_req_ids[0]) self.assertEqual(integration_request.status, "Completed") # now one request is completed # second integration request fails # now retrying payment request should make only one integration request again pr = pos_invoice.create_payment_request() new_integration_req_ids = frappe.get_all( "Integration Request", filters={ "reference_doctype": pr.doctype, "reference_docname": pr.name, "name": ["not in", integration_req_ids], }, pluck="name", ) self.assertEqual(len(new_integration_req_ids), 1) frappe.db.set_value("Customer", "_Test Customer", "default_currency", "") frappe.db.sql( "delete from `tabIntegration Request` where integration_request_service = 'Mpesa'" ) pr.reload() pr.cancel() pr.delete() pos_invoice.delete()
def test_processing_of_multiple_callback_payload(self): mpesa_account = frappe.db.get_value( "Payment Gateway Account", {"payment_gateway": "Mpesa-Payment"}, "payment_account") frappe.db.set_value("Account", mpesa_account, "account_currency", "KES") frappe.db.set_value("Mpesa Settings", "Payment", "transaction_limit", "500") frappe.db.set_value("Customer", "_Test Customer", "default_currency", "KES") pos_invoice = create_pos_invoice(do_not_submit=1) pos_invoice.append( "payments", { "mode_of_payment": "Mpesa-Payment", "account": mpesa_account, "amount": 1000 }) pos_invoice.contact_mobile = "093456543894" pos_invoice.currency = "KES" pos_invoice.save() pr = pos_invoice.create_payment_request() # test payment request creation self.assertEqual(pr.payment_gateway, "Mpesa-Payment") # submitting payment request creates integration requests with random id integration_req_ids = frappe.get_all( "Integration Request", filters={ "reference_doctype": pr.doctype, "reference_docname": pr.name, }, pluck="name", ) # create random receipt nos and send it as response to callback handler mpesa_receipt_numbers = [ frappe.utils.random_string(5) for d in integration_req_ids ] integration_requests = [] for i in range(len(integration_req_ids)): callback_response = get_payment_callback_payload( Amount=500, CheckoutRequestID=integration_req_ids[i], MpesaReceiptNumber=mpesa_receipt_numbers[i], ) # handle response manually verify_transaction(**callback_response) # test completion of integration request integration_request = frappe.get_doc("Integration Request", integration_req_ids[i]) self.assertEqual(integration_request.status, "Completed") integration_requests.append(integration_request) # check receipt number once all the integration requests are completed pos_invoice.reload() self.assertEqual(pos_invoice.mpesa_receipt_number, ", ".join(mpesa_receipt_numbers)) frappe.db.set_value("Customer", "_Test Customer", "default_currency", "") [d.delete() for d in integration_requests] pr.reload() pr.cancel() pr.delete() pos_invoice.delete()