def test_validate_ap_transaction_without_enums(self): try: Utils.validate_ap_transaction('lpp_14', 'pay_tok', {}) except Exception: self.fail( 'Utils.validate_ap_transaction raised an exception unexpectedly when not using enums' )
def test_luhn_check(self): self.assertEqual(Utils.luhn_check(self.VALID_PAN), True, 'PAN unexpected invalid') self.assertEqual(Utils.luhn_check(self.INVALID_PAN), False, 'PAN unexpected valid') self.assertEqual(Utils.luhn_check(None), False, 'None type unexpectedly valid')
def alternative_payment_request(self, apm_id, payment_token, user_data=None, customer=None, **kwargs): Utils.validate_ap_transaction( apm_id=apm_id, payment_token=payment_token, user_data=user_data) Utils.validate_customer(customer=customer) request = { 'localPayment': { 'lppId': apm_id if not isinstance(apm_id, sdk.AlternativePaymentMethodId) else apm_id.value, 'userData': user_data }, 'paymentToken': payment_token } if Utils.is_email(customer): request['email'] = customer else: request['customerId'] = customer request.update(kwargs) return AlternativePaymentResponse( self._send_http_request('charges/localpayment', HttpMethod.POST, request))
def test_validate_payment_source_card(self): try: Utils.validate_payment_source(self.VALID_ID) except Exception: self.fail( 'Utils.validate_payment_source raised an exception unexpectedly for a valid card source.' )
def get(self, id): self._log_info('Get {}'.format(id)) Utils.validate_payment_id(id) return PaymentProcessed( self._send_http_request('charges/{}'.format(id), HttpMethod.GET))
def test_validate_payment_source_card(self): try: Utils.validate_payment_source(self.CARD_ID) except errors.BadRequestError: self.fail( 'Utils.validate_payment_source raised BadRequestError unexpectedly for a valid card id' )
def test_payments_client_full_card_auth_request(self): payment = self.auth_card() self.assertEqual(payment.http_response.status, 200) # test payment self.assertTrue(Utils.is_id(payment.id, short_id=True)) self.assertTrue(payment.approved) self.assertEqual(payment.value, 100) self.assertEqual(payment.currency, 'USD') self.assertEqual(payment.track_id, 'ORDER-001-002') # test card self.assertTrue(Utils.is_id(payment.card.id), 'card') self.assertEqual(int(payment.card.expiryMonth), 6) self.assertEqual(int(payment.card.expiryYear), 2025) self.assertEqual(payment.card.last4, '4242') self.assertEqual(payment.card.name, 'Joe Smith') # test customer self.assertTrue(Utils.is_id(payment.customer.id, 'cust')) self.assertEqual(payment.customer.email, '*****@*****.**') # test other content from the http body body = payment.http_response.body self.assertEqual(body['card']['billingDetails']['city'], 'London') self.assertEqual( body['transactionIndicator'], sdk.PaymentType.Recurring.value) # pylint: disable = no-member self.assertEqual(body['udf1'], 'udf1') # below is also a test of snake >> camel casing. self.assertEqual(body['customerIp'], '8.8.8.8') self.assertEqual(body['products'][0]['price'], 2000)
def test_validate_transaction_without_enums(self): try: Utils.validate_transaction(100, 'eur', 2, 1) except Exception: self.fail( 'Utils.validate_transaction raised an exception unexpectedly when not using enums' )
def test_validate_payment_source_token(self): try: Utils.validate_payment_source(None, self.TOKEN_ID) except errors.BadRequestError: self.fail( 'Utils.validate_payment_source raised BadRequestError unexpectedly for a valid token' )
def test_validate_payment_source_token(self): try: Utils.validate_payment_source(None, self.VALID_ID) except Exception: self.fail( 'Utils.validate_payment_source raised an exception unexpectedly for a valid token.' )
def test_validate_transaction_without_enums(self): try: Utils.validate_transaction(100, 'eur', 2) except errors.BadRequestError: self.fail( 'Utils.validate_transaction raised BadRequestError unexpectedly when not using enums' )
def test_validate_payment_id(self): try: Utils.validate_payment_id(self.PAYMENT_ID) except errors.BadRequestError: self.fail( 'Utils.validate_payment_id raised BadRequestError unexpectedly' )
def test_validate_transaction(self): try: Utils.validate_transaction(100, sdk.Currency.USD, sdk.PaymentType.Recurring) except errors.BadRequestError: self.fail( 'Utils.validate_transaction raised BadRequestError unexpectedly with enums' )
def test_validate_customer(self): try: Utils.validate_customer(self.CUSTOMER_ID) Utils.validate_customer(self.EMAIL) except errors.BadRequestError: self.fail( 'Utils.validate_customer raised BadRequestError unexpectedly for a valid customer id' )
def test_validate_ap_transaction(self): try: Utils.validate_ap_transaction(sdk.AlternativePaymentMethodId.IDEAL, 'pay_tok', {}) except Exception: self.fail( 'Utils.validate_ap_transaction raised an exception unexpectedly when using enums' )
def test_validate_payment_source_full_card_fails(self): with self.assertRaises(errors.BadRequestError): Utils.validate_payment_source({ 'number': '4242424242424241', 'expiryMonth': 6, 'expiryYear': 2025, 'cvv': '100' })
def test_validate_transaction(self): try: Utils.validate_transaction(100, sdk.Currency.USD, sdk.PaymentType.Recurring, sdk.ChargeMode.NonThreeD) except Exception: self.fail( 'Utils.validate_transaction raised an exception unexpectedly when using enums.' )
def test_payments_client_void_request(self): payment = self.auth_card() # void the previous auth request response = self.client.void(payment.id, track_id='ORDER-001-002-VOID') self.assertEqual(response.http_response.status, 200) # test payment self.assertTrue(Utils.is_id(response.id, short_id=True)) self.assertTrue(Utils.is_id(response.original_id, short_id=True)) self.assertEqual(response.track_id, 'ORDER-001-002-VOID') self.assertTrue(response.approved)
def test_is_id(self): self.assertEqual(Utils.is_id(self.CARD_ID, 'card', False), True, 'Card Id unexpectedly invalid') self.assertEqual(Utils.is_id(self.CUSTOMER_ID, 'cust', False), True, 'Customer Id unexpectedly invalid') self.assertEqual(Utils.is_id(self.PAYMENT_ID, 'charge_test', True), True, 'Charge Id unexpectedly invalid') self.assertEqual(Utils.is_id(self.CARD_ID, '...', False), False, 'Card Id unexpectedly valid') self.assertEqual(Utils.is_id(self.CARD_ID, 'card', True), False, 'Card Id unexpectedly valid')
def test_payments_client_capture_partial_amount_request(self): payment = self.auth_card(value=150) # capture the previous auth request response = self.client.capture(payment.id, value=100, track_id='ORDER-001-002-CAPTURE') self.assertEqual(response.http_response.status, 200) # test payment self.assertTrue(Utils.is_id(response.id, short_id=True)) self.assertTrue(Utils.is_id(response.original_id, short_id=True)) self.assertEqual(response.track_id, 'ORDER-001-002-CAPTURE') self.assertEqual(response.value, 100) self.assertTrue(response.approved)
def request_payment_token( self, # Transaction value=0, currency=sdk.default_currency, track_id=None, # Auto Capture auto_capture=sdk.default_auto_capture, auto_capture_delay=sdk.default_auto_capture_delay, # Urls success_url=None, fail_url=None, **kwargs): self._log_info('Payment token {}{} - {}'.format( value, currency, track_id if track_id else '<no track id>')) Utils.validate_transaction(value=value, currency=currency) request = { 'value': value, 'currency': currency if not isinstance(currency, sdk.Currency) else currency.value, 'trackId': track_id, 'chargeMode': sdk.ChargeMode.APM.value, # pylint: disable = no-member 'autoCapture': 'Y' if auto_capture else 'N', 'autoCapTime': auto_capture_delay, 'successUrl': success_url, 'failUrl': fail_url } # add remaining properties request.update(kwargs) http_response = self._send_http_request('tokens/payment', HttpMethod.POST, request) return PaymentTokenResponse(http_response)
def test_payments_client_refund_full_amount_request(self): payment = self.auth_card(value=150) # capture the previous auth request capture = self.client.capture(payment.id, track_id='ORDER-001-002-CAPTURE') self.assertEqual(payment.id, capture.original_id) # refund the capture response = self.client.refund(capture.id, track_id='ORDER-001-002-REFUND') self.assertEqual(response.http_response.status, 200) # test payment self.assertTrue(Utils.is_id(response.id, short_id=True)) self.assertTrue(Utils.is_id(response.original_id, short_id=True)) self.assertEqual(capture.id, response.original_id) self.assertEqual(response.track_id, 'ORDER-001-002-REFUND') self.assertEqual(response.value, 150) self.assertTrue(response.approved)
def test_verify_redirect_flow_ap(self): http_response = HttpResponse(200, None, { 'id': 'pay_tok_1', 'localPayment': { 'paymentUrl': 'http://www' } }, 0) self.assertTrue(Utils.verify_redirect_flow(http_response))
def _getPaymentActionResponse(self, id, action, value, track_id, **kwargs): self._log_info('{} - {} - {}'.format( action.capitalize(), id, track_id if track_id else '<no track id>')) Utils.validate_payment_id(id) request = {'trackId': track_id} if value is not None: Utils.validate_transaction(value=value) request['value'] = value # add remaining properties request.update(kwargs) return self._send_http_request('charges/{}/{}'.format(id, action), HttpMethod.POST, request)
def test_is_number(self): self.assertEqual(Utils.is_number(5, 1, 10), True, 'Number unexpctedly out of range') self.assertEqual(Utils.is_number(5, 5, 5), True, 'Number unexpctedly out of range') self.assertEqual(Utils.is_number('5', 5, 5), True, 'Number unexpctedly out of range') self.assertEqual(Utils.is_number(10, 11, 15), False, 'Number unexpctedly in range') self.assertEqual(Utils.is_number(15, 11), True, 'Number unexpctedly out of range') self.assertEqual(Utils.is_number('ABC'), False, '"ABC" is not a number') self.assertEqual(Utils.is_number(None), False, 'None type is not a number')
def request(self, card=None, token=None, value=0, currency=sdk.default_currency, payment_type=sdk.default_payment_type, customer=None, track_id=None, auto_capture=sdk.default_auto_capture, auto_capture_delay=sdk.default_auto_capture_delay, **kwargs): payment_source = card or token # card can be a dictionary and need the JSON case converted, if needed, before being validated if isinstance(card, dict): card = self._convert_json_case(card) # change payment source to a masked PAN payment_source = Utils.mask_pan(card['number']) self._log_info('Auth {} - {}{} - {}'.format( payment_source, value, currency, track_id if track_id else '<no track id>')) Utils.validate_payment_source(card=card, token=token) Utils.validate_transaction(value=value, currency=currency, payment_type=payment_type) Utils.validate_customer(customer=customer) request = { 'value': value, 'currency': currency if not isinstance(currency, sdk.Currency) else currency.value, 'trackId': track_id, 'transactionIndicator': payment_type if not isinstance(payment_type, sdk.PaymentType) else payment_type.value, 'autoCapture': 'Y' if auto_capture else 'N', 'autoCapTime': auto_capture_delay } if card: if isinstance(card, dict): request['card'] = card else: request['cardId'] = card else: request['cardToken'] = token if Utils.is_id(customer): request['customerId'] = customer else: request['email'] = customer # add remaining properties request.update(kwargs) return PaymentProcessed( self._send_http_request( 'charges/card' if card is not None else 'charges/token', HttpMethod.POST, request))
def test_validate_payment_source_token_fails(self): with self.assertRaises(errors.BadRequestError): Utils.validate_payment_source(None, self.INVALID_ID)
def test_validate_transaction_fails_with_bad_payment_type(self): with self.assertRaises(errors.BadRequestError): Utils.validate_transaction(100, 'usd', 5)
def test_validate_transaction_fails_with_bad_currency(self): with self.assertRaises(errors.BadRequestError): Utils.validate_transaction(100, 'xxx')
def test_validate_transaction_fails_with_bad_value(self): with self.assertRaises(errors.BadRequestError): Utils.validate_transaction(-5)