def test_xml_to_dir(self): """Test for xml_to_dir function.""" xml = """ <foo> <bar></bar> </foo>""" results = helper.xml_to_dir(parseXML(xml)) self.assertIsInstance(results, dict) self.assertIn("bar", results) xml = """ <foo> <bar1></bar1> <bar2></bar2> </foo>""" results = helper.xml_to_dir(parseXML(xml)) self.assertIsInstance(results, dict) for label in ["bar1", "bar2"]: self.assertIn(label, results) xml = """ <foo> <bar1>foo1</bar1> <bar2>foo2</bar2> </foo>""" results = helper.xml_to_dir(parseXML(xml)) self.assertIsInstance(results, dict) self.assertEqual("foo1", results["bar1"]) self.assertEqual("foo2", results["bar2"])
def test_make_transaction_request_mock(self, merchant_reference, transaction_type, amount, transaction_id, add_bill_card, billing_id, dps_billing_id, data_1, data_2, data_3, email, optional_text): """Test XML generation of make_reques method""" xml = self.pxpay.make_transaction_request( merchant_reference=merchant_reference, transaction_type=transaction_type, amount=amount, currency="NZD", transaction_id=transaction_id, url_success="https://pass.nzrs.nz", url_fail="https://fail.nzrs.nz", add_bill_card=add_bill_card, billing_id=billing_id, dps_billing_id=dps_billing_id, data_1=data_1, data_2=data_2, data_3=data_3, email_address=email, optional_text=optional_text, mock=True) self.assertIsInstance(parseXML(xml), Element)
def get_xml(response): """Returns XML object from web response""" if response.status_code != 200: raise(Exception("Server responded with {status}: {reason}".format( status=response.status_code, reason=response.reason))) else: try: et = parseXML(response.text) except Exception as e: raise(Exception("Error parsing response: {description}".format( description=str(e)))) return et
def test_make_transaction_request(self): """Tests make_transaction_request method""" # authentication transaction response = self.pxpay.make_transaction_request( **self.generic_request, transaction_type=pxpay.TXN_AUTH, transaction_id=str(uuid.uuid4()).replace("-", "")[0:16], add_bill_card=True, billing_id="BILLID") # text XML self.assertIsInstance(parseXML(response["xml"]), Element) # test URL url = response["url"] self.assertIsNotNone(url) self.assertIsInstance(url, str) response = requests.get(url) self.assertEqual(response.status_code, 200) # purchase transaction response = self.pxpay.make_transaction_request( **self.generic_request, transaction_type=pxpay.TXN_PURCHASE, transaction_id=str(uuid.uuid4()).replace("-", "")[0:16]) url = response["url"] self.assertIsNotNone(url) self.assertIsInstance(url, str) response = requests.get(url) self.assertEqual(response.status_code, 200) # duplicate transaction IDs # attempt 1 try: self.pxpay.make_transaction_request( **self.generic_request, transaction_type=pxpay.TXN_AUTH, transaction_id="TID-A") except Exception as e: # exception due to duplicate transaction ID may happen pass # attempt 2 with self.assertRaises(Exception, msg="Request failed: TxnId/TxnRef duplicate"): self.pxpay.make_transaction_request( **self.generic_request, transaction_type=pxpay.TXN_AUTH, transaction_id="TID-A")
def test_make_token_based_transaction_mock( self, merchant_reference, transaction_type, amount, transaction_id, card_number_2, billing_id, data_1, data_2, data_3): """Test XML generation of make_token_based_transaction""" xml = self.pxpost.make_token_based_transaction( merchant_reference=merchant_reference, transaction_type=transaction_type, amount=amount, currency="NZD", transaction_id=transaction_id, card_number_2=card_number_2, billing_id=billing_id, data_1=data_1, data_2=data_2, data_3=data_3, mock=True) self.assertIsInstance(parseXML(xml), Element)
def test_make_transaction_request_rebill(self): """Test for PxPay rebilling with user's CSC input.""" try: environ["NO_MANUAL"] raise unittest.SkipTest("Needs manual intervention.") except KeyError as e: pass # authentication transaction billing_id = str(uuid.uuid4()).replace("-", "") response = self.pxpay.make_transaction_request( **self.generic_request, transaction_type=pxpay.TXN_AUTH, transaction_id=str(uuid.uuid4()).replace("-", "")[0:16], add_bill_card=True, billing_id=billing_id) url = response["url"] webbrowser.open(url) print("\n") print("Make sure following transaction succeeds.") result_url = input("Enter forwarding URL:") query_string = parse.urlparse(result_url).query self.assertTrue(len(query_string) > 0) parsed_query_string = parse.parse_qs(query_string) self.assertIn("userid", parsed_query_string) self.assertIn("result", parsed_query_string) result = parsed_query_string["result"][0] # get transaction status response = self.pxpay.get_transaction_status(result) # text XML self.assertIsInstance(parseXML(response["xml"]), Element) # test result result = response["result"] self.assertIn("BillingId", result) self.assertIn("DpsBillingId", result) dps_billing_id = result["DpsBillingId"] # attempt to rebill with BillingID response = self.pxpay.make_transaction_request( **self.generic_request, transaction_type=pxpay.TXN_PURCHASE, transaction_id=str(uuid.uuid4()).replace("-", "")[0:16], billing_id=billing_id) url = response["url"] webbrowser.open(url) print("\n") result_url = input("Enter forwarding URL:") query_string = parse.urlparse(result_url).query self.assertTrue(len(query_string) > 0) parsed_query_string = parse.parse_qs(query_string) self.assertIn("userid", parsed_query_string) self.assertIn("result", parsed_query_string) result = parsed_query_string["result"][0] # get transaction status response = self.pxpay.get_transaction_status(result) result = response["result"] for label in self.labels: self.assertIn(label, result) self.assertEqual(result["TxnType"], pxpay.TXN_PURCHASE) self.assertEqual(result["BillingId"], billing_id) print("Transaction success: {}".format(result["Success"])) # attempt to rebill with DpsBillingID response = self.pxpay.make_transaction_request( **self.generic_request, transaction_type=pxpay.TXN_PURCHASE, transaction_id=str(uuid.uuid4()).replace("-", "")[0:16], dps_billing_id=dps_billing_id) url = response["url"] webbrowser.open(url) print("\n") result_url = input("Enter forwarding URL:") query_string = parse.urlparse(result_url).query self.assertTrue(len(query_string) > 0) parsed_query_string = parse.parse_qs(query_string) self.assertIn("userid", parsed_query_string) self.assertIn("result", parsed_query_string) result = parsed_query_string["result"][0] # get transaction status response = self.pxpay.get_transaction_status(result) result = response["result"] for label in self.labels: self.assertIn(label, result) self.assertEqual(result["TxnType"], pxpay.TXN_PURCHASE) self.assertEqual(result["DpsBillingId"], dps_billing_id) print("Transaction success: {}".format(result["Success"]))
def test_get_transaction_status_mock(self, result): """Tests XML generation of get_transaction_status method""" xml = self.pxpay.get_transaction_status(result=result, mock=True) self.assertIsInstance(parseXML(xml), Element)
def test_process_status(self): """Tests process_status method""" # valid PxPay XML output valid_xml = { """ <Request valid="1"> <URI>https://sec.paymentexpress.com/pxmi3/EF4054F622D6C4C1B4F9AEA59DC</URI> </Request>""", """ <Response valid="1"> <Success>1</Success> <TxnType>Purchase</TxnType> <CurrencyInput>NZD</CurrencyInput> <MerchantReference>Purchase Example</MerchantReference> <TxnData1></TxnData1> <TxnData2></TxnData2> <TxnData3></TxnData3> <AuthCode>113837</AuthCode> <CardName>Visa</CardName> <CardHolderName>CARDHOLDER NAME</CardHolderName> <CardNumber>411111........11</CardNumber> <DateExpiry>1111</DateExpiry> <ClientInfo>127.0.0.1</ClientInfo> <TxnId>P03E57DA8A9DD700</TxnId> <EmailAddress></EmailAddress> <DpsTxnRef>000000060495729b</DpsTxnRef> <BillingId></BillingId> <DpsBillingId></DpsBillingId> <AmountSettlement>1.00</AmountSettlement> <CurrencySettlement>NZD</CurrencySettlement> <DateSettlement>20100924</DateSettlement> <TxnMac>BD43E619</TxnMac> <ResponseText>APPROVED</ResponseText> <CardNumber2></CardNumber2> <Cvc2ResultCode>M</Cvc2ResultCode> </Response>""", """ <foo valid="1"> <bar></bar> </foo>""", """ <foo valid="1"></foo>""", """ <foo bar="2" valid="1"></foo>""", """ <foo valid="1" bar="2"></foo>""", } for xml in valid_xml: self.assertIsInstance(helper.process_status(parseXML(xml)), dict) # invalid PxPay XML output invalid_xml = { """ <Request valid="2"> <URI>https://sec.paymentexpress.com/pxmi3/EF4054F622D6C4C1B4F9AEA59DC</URI> </Request>""", """ <foo valid="10"></foo>""", """ <foo bar="1" valid="3"></foo>""", """ <foo valid="2" bar="1"></foo>""", } for xml in invalid_xml: with self.assertRaises(Exception, msg="Invalid request."): helper.process_status(parseXML(xml)) # valid PxPost XML output valid_xml = { """ <Txn> <Transaction success="1" reco="00" responseText="APPROVED" pxTxn="true"> <Authorized>1</Authorized> <ReCo>00</ReCo> <RxDate>20170519021539</RxDate> <RxDateLocal>20170519141539</RxDateLocal> <LocalTimeZone>NZT</LocalTimeZone> <MerchantReference>My Reference</MerchantReference> <CardName>Visa</CardName> <Retry>0</Retry> <StatusRequired>0</StatusRequired> <AuthCode>006098</AuthCode> <AmountBalance>0.00</AmountBalance> <Amount>1.00</Amount> <CurrencyId>840</CurrencyId> <InputCurrencyId>840</InputCurrencyId> <InputCurrencyName>USD</InputCurrencyName> <CurrencyRate>1.00</CurrencyRate> <CurrencyName>USD</CurrencyName> <CardHolderName>C. HOLDER</CardHolderName> <DateSettlement>20170519</DateSettlement> <TxnType>Purchase</TxnType> <CardNumber>411111........11</CardNumber> <TxnMac>2BC20210</TxnMac> <DateExpiry>1212</DateExpiry> <ProductId></ProductId> <AcquirerDate></AcquirerDate> <AcquirerTime></AcquirerTime> <AcquirerId>0</AcquirerId> <Acquirer></Acquirer> <AcquirerReCo>00</AcquirerReCo> <AcquirerResponseText>APPROVED</AcquirerResponseText> <TestMode>0</TestMode> <CardId>2</CardId> <CardHolderResponseText>APPROVED</CardHolderResponseText> <CardHolderHelpText>The Transaction was approved</CardHolderHelpText> <CardHolderResponseDescription>The Transact</CardHolderResponseDescription> <MerchantResponseText>APPROVED</MerchantResponseText> <MerchantHelpText>The Transaction was approved</MerchantHelpText> <MerchantResponseDescription>The Transaction </MerchantResponseDescription> <UrlFail></UrlFail> <UrlSuccess></UrlSuccess> <EnablePostResponse>0</EnablePostResponse> <PxPayName></PxPayName> <PxPayLogoSrc></PxPayLogoSrc> <PxPayUserId></PxPayUserId> <PxPayXsl></PxPayXsl> <PxPayBgColor></PxPayBgColor> <PxPayOptions></PxPayOptions> <Cvc2ResultCode>P</Cvc2ResultCode> <AcquirerPort>10000000-10001407</AcquirerPort> <AcquirerTxnRef>677381</AcquirerTxnRef> <GroupAccount>9997</GroupAccount> <DpsTxnRef>000000010093aa3c</DpsTxnRef> <AllowRetry>1</AllowRetry> <DpsBillingId></DpsBillingId> <BillingId></BillingId> <TransactionId>0093aa3c</TransactionId> <PxHostId>00000001</PxHostId> <RmReason></RmReason> <RmReasonId>0000000000000000</RmReasonId> <RiskScore>-1</RiskScore> <RiskScoreText></RiskScoreText> </Transaction> <RmReason></RmReason> <RmReasonId>0000000000000000</RmReasonId> <RiskScore>-1</RiskScore> <RiskScoreText></RiskScoreText> <ReCo>00</ReCo> <ResponseText>APPROVED</ResponseText> <HelpText>Transaction Approved</HelpText> <Success>1</Success> <DpsTxnRef>000000010093aa3c</DpsTxnRef> <ICCResult></ICCResult> <TxnRef></TxnRef> </Txn> """, """ <foo bar="1"> <bar></bar> </foo>""", """ <foo></foo>""", """ <foo> <bar>foobar</bar> <bar>foobar</bar> </foo>""", """ <foo> <bar>foobar</bar> <foobar></foobar> </foo>""", } for xml in valid_xml: self.assertIsInstance( helper.process_status(parseXML(xml), pxpost=True), dict)
def test_make_token_based_transaction(self): """Test token based transaction""" try: environ["NO_MANUAL"] raise unittest.SkipTest("Needs manual intervention.") except KeyError as e: pass # Initiate PxPay _config = self.config["DEFAULT"] _pxpay = pxpay.PxPay( url=_config["URL"], user_id=_config["UserID"], auth_key=_config["AuthKey"]) # Create token # authentication transaction billing_id = str(uuid.uuid4()).replace("-", "") response = _pxpay.make_transaction_request( merchant_reference="MREF", amount=1.00, currency="NZD", url_success=_config["URL_SUCCESS"], url_fail=_config["URL_FAIL"], transaction_type=pxpay.TXN_AUTH, transaction_id=str(uuid.uuid4()).replace("-", "")[0:16], add_bill_card=True, billing_id=billing_id) url = response["url"] print("\n") print("Make sure following transaction succeeds.") webbrowser.open(url) result_url = input("Enter forwarding URL:") query_string = parse.urlparse(result_url).query self.assertTrue(len(query_string) > 0) parsed_query_string = parse.parse_qs(query_string) self.assertIn("userid", parsed_query_string) self.assertIn("result", parsed_query_string) result = parsed_query_string["result"][0] # get transaction status response = _pxpay.get_transaction_status(result) result = response["result"] self.assertIn("Success", result) self.assertIn("CardNumber2", result) self.assertIn("BillingId", result) self.assertEqual(int(result["Success"]), 1) self.assertEqual(result["BillingId"], billing_id) card_number_2 = result["CardNumber2"] self.assertTrue(len(card_number_2) > 0) # make token based transaction transaction_id = str(uuid.uuid4()).replace("-", "")[0:16] amount = 10.00 response = self.pxpost.make_token_based_transaction( merchant_reference="MREF_POST", transaction_type=pxpost.TXN_PURCHASE, amount=amount, currency="NZD", card_number_2=card_number_2, transaction_id=transaction_id, billing_id=billing_id) self.assertIsInstance(parseXML(response["xml"]), Element) result = response["result"] labels = ["ResponseText", "Success", "DpsTxnRef", "TxnRef"] for label in labels: self.assertIn(label, result) self.assertEqual(int(result["Success"]), 1) self.assertEqual(result["ResponseText"], "APPROVED") self.assertEqual(result["TxnRef"], transaction_id) self.assertEqual(int(result["Transaction"]["Authorized"]), 1) self.assertEqual(float(result["Transaction"]["Amount"]), amount) self.assertEqual(result["Transaction"]["BillingId"], billing_id) self.assertEqual(result["Transaction"]["CardNumber2"], card_number_2)