def test_refund_invalid_data( mocked_gateway, mocked_logger, charged_payment, razorpay_success_response, gateway_config, ): # Assign the side effect to the gateway's `refund()` method, # that should trigger the expected error. mocked_gateway.return_value.payment.refund.side_effect = ServerError() # Attempt charging payment_info = create_payment_information( charged_payment, amount=TRANSACTION_AMOUNT ) response = refund(payment_info, gateway_config) # Ensure a error was returned assert response.error == errors.SERVER_ERROR assert not response.is_success # Ensure the transaction is correctly set assert response.kind == TransactionKind.REFUND # Ensure the HTTP error was logged assert mocked_logger.call_count == 1
def test_charge_invalid_request( mocked_gateway, mocked_logger, razorpay_payment, gateway_config ): # Data to be passed payment_token = "123" # Assign the side effect to the gateway's `capture()` method, # that should trigger the expected error. mocked_gateway.return_value.payment.capture.side_effect = BadRequestError() payment_info = create_payment_information( razorpay_payment, payment_token=payment_token, amount=TRANSACTION_AMOUNT ) # Attempt charging response = capture(payment_info, gateway_config) # Ensure an error was returned assert response.error == errors.INVALID_REQUEST assert not response.is_success # Ensure the response is correctly set assert response.kind == TransactionKind.CAPTURE assert response.transaction_id == payment_token # Ensure the HTTP error was logged assert mocked_logger.call_count == 1
def transaction_data(payment_dummy, gateway_response): return { 'payment': payment_dummy, 'payment_information': create_payment_information( payment_dummy, 'payment-token'), 'kind': TransactionKind.CAPTURE, 'gateway_response': gateway_response}
def test_gateway_charge( mock_get_payment_gateway, mock_handle_fully_paid_order, payment_txn_preauth, gateway_params, transaction_token, dummy_response): payment_token = transaction_token txn = payment_txn_preauth.transactions.first() payment = payment_txn_preauth assert not payment.captured_amount amount = payment.total dummy_response['kind'] = TransactionKind.CHARGE mock_charge = Mock(return_value=dummy_response) mock_get_payment_gateway.return_value = ( Mock(charge=mock_charge), gateway_params) payment_info = create_payment_information(payment, payment_token, amount) gateway_charge(payment, payment_token, amount) mock_get_payment_gateway.assert_called_once_with(payment.gateway) mock_charge.assert_called_once_with( payment_information=payment_info, connection_params=gateway_params) payment.refresh_from_db() assert payment.charge_status == ChargeStatus.FULLY_CHARGED assert payment.captured_amount == payment.total mock_handle_fully_paid_order.assert_called_once_with(payment.order)
def test_authorize( mock_gateway, payment_dummy, braintree_success_response, gateway_config ): payment = payment_dummy mock_response = Mock(return_value=braintree_success_response) mock_gateway.return_value = Mock(transaction=Mock(sale=mock_response)) payment_info = create_payment_information(payment, "auth-token") response = authorize(payment_info, gateway_config) assert not response.error assert response.kind == TransactionKind.AUTH assert response.amount == braintree_success_response.transaction.amount assert response.currency == braintree_success_response.transaction.currency_iso_code assert response.transaction_id == braintree_success_response.transaction.id assert response.is_success == braintree_success_response.is_success mock_response.assert_called_once_with( { "amount": str(payment.total), "payment_method_nonce": "auth-token", "options": { "submit_for_settlement": CONFIRM_MANUALLY, "three_d_secure": {"required": THREE_D_SECURE_REQUIRED}, }, **get_customer_data(payment_info), } )
def test_refund( mocked_gateway, charged_payment, razorpay_success_response, gateway_params): # Mock the gateway response to a success response mocked_gateway.return_value.payment.refund.return_value = ( razorpay_success_response) payment_info = create_payment_information( charged_payment, amount=TRANSACTION_AMOUNT) # Attempt charging response = refund(payment_info, gateway_params) # Ensure the was no error returned assert not response['error'] assert response['is_success'] assert response['kind'] == TransactionKind.REFUND assert response['amount'] == TRANSACTION_AMOUNT assert response['currency'] == razorpay_success_response['currency'] assert response['raw_response'] == razorpay_success_response assert response['transaction_id'] == razorpay_success_response['id']
def test_gateway_capture( mock_get_payment_gateway, mock_handle_fully_paid_order, payment_txn_preauth, gateway_config, dummy_response, ): payment = payment_txn_preauth gateway_config.auto_capture = True assert not payment.captured_amount amount = payment.total dummy_response.kind = TransactionKind.CAPTURE mock_capture = Mock(return_value=dummy_response) mock_get_payment_gateway.return_value = (Mock(capture=mock_capture), gateway_config) payment_info = create_payment_information(payment, "", amount) gateway_capture(payment, amount) mock_capture.assert_called_once_with( payment_information=payment_info, config=gateway_config ) payment.refresh_from_db() assert payment.charge_status == ChargeStatus.FULLY_CHARGED assert payment.captured_amount == payment.total mock_handle_fully_paid_order.assert_called_once_with(payment.order)
def test_authorize( mock_gateway, payment_dummy, braintree_success_response, gateway_config): payment = payment_dummy mock_response = Mock(return_value=braintree_success_response) mock_gateway.return_value = Mock(transaction=Mock(sale=mock_response)) payment_info = create_payment_information(payment, 'auth-token') response = authorize(payment_info, gateway_config) assert not response['error'] assert response['kind'] == TransactionKind.AUTH assert response['amount'] == braintree_success_response.transaction.amount assert response['currency'] == braintree_success_response.transaction.currency_iso_code assert response['transaction_id'] == braintree_success_response.transaction.id assert response['is_success'] == braintree_success_response.is_success mock_response.assert_called_once_with({ 'amount': str(payment.total), 'payment_method_nonce': 'auth-token', 'options': { 'submit_for_settlement': CONFIRM_MANUALLY, 'three_d_secure': { 'required': THREE_D_SECURE_REQUIRED}}, **get_customer_data(payment_info)})
def test_charge( mocked_gateway, razorpay_payment, razorpay_success_response, gateway_config ): # Data to be passed payment_token = "123" # Mock the gateway response to a success response mocked_gateway.return_value.payment.capture.return_value = razorpay_success_response payment_info = create_payment_information( razorpay_payment, payment_token=payment_token, amount=TRANSACTION_AMOUNT ) # Attempt charging response = capture(payment_info, gateway_config) # Ensure the was no error returned assert not response.error assert response.is_success assert response.kind == TransactionKind.CAPTURE assert response.amount == TRANSACTION_AMOUNT assert response.currency == razorpay_success_response["currency"] assert response.raw_response == razorpay_success_response assert response.transaction_id == razorpay_success_response["id"]
def test_charge( mocked_gateway, razorpay_payment, razorpay_success_response, gateway_params): # Data to be passed payment_token = '123' # Mock the gateway response to a success response mocked_gateway.return_value.payment.capture.return_value = ( razorpay_success_response) payment_info = create_payment_information( razorpay_payment, payment_token=payment_token, amount=TRANSACTION_AMOUNT) # Attempt charging response = charge(payment_info, gateway_params) # Ensure the was no error returned assert not response['error'] assert response['is_success'] assert response['kind'] == TransactionKind.CHARGE assert response['amount'] == TRANSACTION_AMOUNT assert response['currency'] == razorpay_success_response['currency'] assert response['raw_response'] == razorpay_success_response assert response['transaction_id'] == razorpay_success_response['id']
def test_get_payment_billing_fullname(payment_dummy): payment_info = create_payment_information(payment_dummy) expected_fullname = "%s %s" % ( payment_dummy.billing_last_name, payment_dummy.billing_first_name, ) assert get_payment_billing_fullname(payment_info) == expected_fullname
def test_widget_with_additional_attr(stripe_payment, gateway_config): payment_info = create_payment_information(stripe_payment) widget = StripeCheckoutWidget( payment_info, gateway_config.connection_params, attrs={"data-custom": "custom-data"}, ) assert 'data-custom="custom-data"' in widget.render()
def test_checkout_form(razorpay_payment, gateway_params): payment_info = create_payment_information(razorpay_payment) form = create_form( data={'razorpay_payment_id': '123'}, payment_information=payment_info, connection_params=gateway_params) assert isinstance(form, RazorPaymentForm) assert form.is_valid()
def test_payment_gateway_form_exists(gateway_name, payment_dummy): """Test if for each payment gateway there's a corresponding form for the old checkout. An error will be raised if it's missing. """ payment_gateway, gateway_params = get_payment_gateway(gateway_name) payment_info = create_payment_information(payment_dummy) payment_gateway.create_form(None, payment_info, gateway_params)
def test_braintree_payment_form_incorrect_amount(payment_dummy): amount = Decimal('0.01') data = {'amount': amount, 'payment_method_nonce': 'fake-nonce'} assert amount != payment_dummy.total payment_info = create_payment_information(payment_dummy) form = BraintreePaymentForm(data=data, payment_information=payment_info) assert not form.is_valid() assert form.non_field_errors
def transaction_data(payment_dummy, gateway_response): return { "payment": payment_dummy, "payment_information": create_payment_information( payment_dummy, "payment-token" ), "kind": TransactionKind.CAPTURE, "gateway_response": gateway_response, }
def test_widget_with_prefill_option(stripe_payment, gateway_config): payment_info = create_payment_information(stripe_payment) connection_params = gateway_config.connection_params connection_params["prefill"] = True widget = StripeCheckoutWidget(payment_info, connection_params) assert 'data-email="*****@*****.**"' in widget.render() connection_params["prefill"] = False widget = StripeCheckoutWidget(payment_info, connection_params) assert 'data-email="*****@*****.**"' not in widget.render()
def test_checkout_form(razorpay_payment, gateway_config): payment_info = create_payment_information(razorpay_payment) form = create_form( data={"razorpay_payment_id": "123"}, payment_information=payment_info, connection_params=gateway_config.connection_params, ) assert isinstance(form, RazorPaymentForm) assert form.is_valid()
def test_widget_with_enable_shipping_address_option(stripe_payment, gateway_params): payment_info = create_payment_information(stripe_payment, FAKE_TOKEN) gateway_params['enable_shipping_address'] = True widget = StripeCheckoutWidget(payment_info, gateway_params) assert 'data-shipping-address="true"' in widget.render() gateway_params['enable_shipping_address'] = False widget = StripeCheckoutWidget(payment_info, gateway_params) assert 'data-shipping-address="false"' in widget.render()
def test_widget_with_remember_me_option(stripe_payment, gateway_params): payment_info = create_payment_information(stripe_payment) gateway_params['remember_me'] = True widget = StripeCheckoutWidget(payment_info, gateway_params) assert 'data-allow-remember-me="true"' in widget.render() gateway_params['remember_me'] = False widget = StripeCheckoutWidget(payment_info, gateway_params) assert 'data-allow-remember-me="false"' in widget.render()
def test_widget_with_prefill_option(stripe_payment, gateway_params): payment_info = create_payment_information(stripe_payment) gateway_params['prefill'] = True widget = StripeCheckoutWidget(payment_info, gateway_params) assert 'data-email="*****@*****.**"' in widget.render() gateway_params['prefill'] = False widget = StripeCheckoutWidget(payment_info, gateway_params) assert 'data-email="*****@*****.**"' not in widget.render()
def test_widget_with_remember_me_option(stripe_payment, gateway_config): payment_info = create_payment_information(stripe_payment) connection_params = gateway_config.connection_params connection_params["remember_me"] = True widget = StripeCheckoutWidget(payment_info, connection_params) assert 'data-allow-remember-me="true"' in widget.render() connection_params["remember_me"] = False widget = StripeCheckoutWidget(payment_info, connection_params) assert 'data-allow-remember-me="false"' in widget.render()
def test_widget_with_default_options(stripe_payment, gateway_params): payment_info = create_payment_information(stripe_payment) widget = StripeCheckoutWidget(payment_info, gateway_params) assert widget.render() == ( '<script class="stripe-button" data-allow-remember-me="true" ' 'data-amount="4242" data-billing-address="false" data-currency="USD" ' 'data-description="Total payment" data-email="*****@*****.**" ' 'data-image="image.gif" data-key="public" data-locale="auto" ' 'data-name="Saleor" data-shipping-address="false" ' 'data-zip-code="false" src="https://checkout.stripe.com/checkout.js">' '</script>')
def test_checkout_widget_render_with_prefill(razorpay_payment, gateway_params): payment_info = create_payment_information(razorpay_payment) widget = RazorPayCheckoutWidget( payment_information=payment_info, **gateway_params) assert widget.render() == ( '<script data-amount="8000" data-buttontext="Pay now with Razorpay" ' 'data-currency="INR" data-description="Total payment" ' 'data-image="image.png" data-key="public" data-name="Saleor" ' 'data-prefill.email="*****@*****.**" ' 'data-prefill.name="Doe John" ' 'src="https://checkout.razorpay.com/v1/checkout.js"></script>')
def test_widget_with_enable_shipping_address_option(stripe_payment, gateway_config): payment_info = create_payment_information(stripe_payment, FAKE_TOKEN) connection_params = gateway_config.connection_params connection_params["enable_shipping_address"] = True widget = StripeCheckoutWidget(payment_info, connection_params) assert 'data-shipping-address="true"' in widget.render() connection_params["enable_shipping_address"] = False widget = StripeCheckoutWidget(payment_info, connection_params) assert 'data-shipping-address="false"' in widget.render()
def test_void_incorrect_token( mock_gateway, payment_txn_preauth, braintree_not_found_error, gateway_config): payment = payment_txn_preauth mock_response = Mock(side_effect=braintree_not_found_error) mock_gateway.return_value = Mock(transaction=Mock(void=mock_response)) payment_info = create_payment_information(payment) with pytest.raises(BraintreeException) as e: void(payment_info, gateway_config) assert str(e.value) == DEFAULT_ERROR_MESSAGE
def test_authorize_incorrect_token( mock_gateway, payment_dummy, braintree_not_found_error, gateway_config): payment = payment_dummy payment_token = 'payment-token' mock_response = Mock(side_effect=braintree_not_found_error) mock_gateway.return_value = Mock(transaction=Mock(sale=mock_response)) payment_info = create_payment_information(payment, payment_token) with pytest.raises(BraintreeException) as e: authorize(payment_info, gateway_config) assert str(e.value) == DEFAULT_ERROR_MESSAGE
def test_checkout_widget_render_without_prefill(razorpay_payment, gateway_params): gateway_params['prefill'] = False payment_info = create_payment_information(razorpay_payment) widget = RazorPayCheckoutWidget( payment_information=payment_info, attrs={'data-custom': '123'}, **gateway_params) assert widget.render() == ( '<script data-amount="8000" data-buttontext="Pay now with Razorpay" ' 'data-currency="INR" data-custom="123" ' 'data-description="Total payment" ' 'data-image="image.png" data-key="public" data-name="Saleor" ' 'src="https://checkout.razorpay.com/v1/checkout.js"></script>')
def test_braintree_payment_form(payment_dummy): payment = payment_dummy data = {"amount": payment.total, "payment_method_nonce": "fake-nonce"} payment_info = create_payment_information(payment) form = create_form( data=data, payment_information=payment_info, connection_params={"secret": "123"} ) assert isinstance(form, BraintreePaymentForm) assert form.is_valid()
def test_braintree_payment_form(payment_dummy): payment = payment_dummy data = {'amount': payment.total, 'payment_method_nonce': 'fake-nonce'} payment_info = create_payment_information(payment) form = create_form( data=data, payment_information=payment_info, connection_params={'secret': '123'}) assert isinstance(form, BraintreePaymentForm) assert form.is_valid()
def test_gateway_authorize(mock_get_payment_gateway, payment_txn_preauth, gateway_params, transaction_token, dummy_response): payment = payment_txn_preauth payment_token = transaction_token mock_authorize = Mock(return_value=dummy_response) mock_get_payment_gateway.return_value = (Mock(authorize=mock_authorize), gateway_params) payment_info = create_payment_information(payment, payment_token) gateway_authorize(payment, payment_token) mock_get_payment_gateway.assert_called_once_with(payment.gateway) mock_authorize.assert_called_once_with(payment_information=payment_info, connection_params=gateway_params)
def test_refund(stripe_paid_payment, sandbox_gateway_config): # Get id from sandbox for succeeded payment REFUND_AMOUNT = Decimal(10.0) # partial refund INTENT_ID = "pi_1F5BsRIUmJaD6Oqvz2XMKZCD" payment_info = create_payment_information( stripe_paid_payment, amount=REFUND_AMOUNT, payment_token=INTENT_ID ) response = refund(payment_info, sandbox_gateway_config) assert not response.error assert response.transaction_id == INTENT_ID assert response.kind == TransactionKind.REFUND assert response.is_success assert isclose(response.amount, REFUND_AMOUNT) assert response.currency == TRANSACTION_CURRENCY
def test_authorize_payment(mock_payment_interface, payment_dummy): PAYMENT_DATA = create_payment_information( payment=payment_dummy, payment_token=TOKEN ) mock_payment_interface.authorize_payment.return_value = AUTHORIZE_RESPONSE transaction = gateway.authorize(payment=payment_dummy, token=TOKEN) mock_payment_interface.authorize_payment.assert_called_once_with( USED_GATEWAY, PAYMENT_DATA ) assert transaction.amount == AUTHORIZE_RESPONSE.amount assert transaction.kind == TransactionKind.AUTH assert transaction.currency == "usd" assert transaction.gateway_response == RAW_RESPONSE
def test_process_payment(mock_payment_interface, payment_txn_preauth): PAYMENT_DATA = create_payment_information( payment=payment_txn_preauth, payment_token=TOKEN ) mock_payment_interface.process_payment.return_value = PROCESS_PAYMENT_RESPONSE transaction = gateway.process_payment(payment=payment_txn_preauth, token=TOKEN) mock_payment_interface.process_payment.assert_called_once_with( USED_GATEWAY, PAYMENT_DATA ) assert transaction.amount == PROCESS_PAYMENT_RESPONSE.amount assert transaction.kind == TransactionKind.CAPTURE assert transaction.currency == "usd" assert transaction.gateway_response == RAW_RESPONSE
def test_gateway_process_payment( mock_get_payment_gateway, payment_txn_preauth, gateway_params, transaction_token, dummy_response): payment_token = transaction_token payment = payment_txn_preauth mock_process_payment = Mock(return_value=[dummy_response, dummy_response]) mock_get_payment_gateway.return_value = ( Mock(process_payment=mock_process_payment), gateway_params) payment_info = create_payment_information(payment, payment_token) gateway_process_payment(payment, payment_token) mock_get_payment_gateway.assert_called_with(payment.gateway) mock_process_payment.assert_called_once_with( payment_information=payment_info, connection_params=gateway_params)
def test_capture(stripe_authorized_payment, sandbox_gateway_config): # Get id from sandbox for intent not yet captured INTENT_ID = "pi_1F5BsRIUmJaD6Oqvz2XMKZCD" payment_info = create_payment_information( stripe_authorized_payment, payment_token=INTENT_ID ) response = capture(payment_info, sandbox_gateway_config) assert not response.error assert response.transaction_id == INTENT_ID assert response.kind == TransactionKind.CAPTURE assert response.is_success assert isclose(response.amount, TRANSACTION_AMOUNT) assert response.currency == TRANSACTION_CURRENCY assert response.card_info == CARD_SIMPLE_DETAILS
def test_checkout_widget_render_without_prefill(razorpay_payment, gateway_config): gateway_config.connection_params["prefill"] = False payment_info = create_payment_information(razorpay_payment) widget = RazorPayCheckoutWidget( payment_information=payment_info, attrs={"data-custom": "123"}, **gateway_config.connection_params, ) assert widget.render() == ( '<script data-amount="8000" data-buttontext="Pay now with Razorpay" ' 'data-currency="INR" data-custom="123" ' 'data-description="Total payment" ' 'data-image="image.png" data-key="public" data-name="Saleor" ' 'src="https://checkout.razorpay.com/v1/checkout.js"></script>' )
def test_create_transaction_with_refund_success_response( stripe_payment, stripe_refund_success_response): payment_info = create_payment_information(stripe_payment) response = _create_response( payment_information=payment_info, kind="ANYKIND", response=stripe_refund_success_response, error=None, ) assert response.transaction_id == TRANSACTION_TOKEN assert response.is_success is True assert isclose(response.amount, TRANSACTION_REFUND_AMOUNT) assert response.currency == TRANSACTION_CURRENCY
def test_void(mock_gateway, payment_txn_preauth, braintree_success_response, gateway_config): payment = payment_txn_preauth mock_response = Mock(return_value=braintree_success_response) mock_gateway.return_value = Mock(transaction=Mock(void=mock_response)) payment_info = create_payment_information(payment, 'token') response = void(payment_info, gateway_config) assert not response.error assert response.kind == TransactionKind.VOID assert response.amount == braintree_success_response.transaction.amount assert response.currency == braintree_success_response.transaction.currency_iso_code assert response.transaction_id == braintree_success_response.transaction.id assert response.is_success == braintree_success_response.is_success mock_response.assert_called_once_with(transaction_id=payment_info.token)
def test_process_payment_error_response(mock_gateway, payment_dummy, braintree_error_response, gateway_config): payment = payment_dummy payment_token = 'payment-token' mock_response = Mock(return_value=braintree_error_response) mock_gateway.return_value = Mock(transaction=Mock(sale=mock_response)) payment_info = create_payment_information(payment, payment_token) response = process_payment(payment_info, gateway_config) assert isinstance(response, list) auth_resp, void_resp = response assert auth_resp['kind'] == TransactionKind.AUTH assert void_resp['kind'] == TransactionKind.VOID
def test_create_response_with_error_response(stripe_payment): payment = stripe_payment payment_info = create_payment_information(payment, FAKE_TOKEN) stripe_error_response = {} response = _create_response( payment_information=payment_info, kind="ANYKIND", response=stripe_error_response, error=None, ) assert response.transaction_id == FAKE_TOKEN assert response.is_success is False assert response.amount == payment.total assert response.currency == payment.currency
def test_authorize(mock_charge_create, stripe_payment, gateway_params, stripe_charge_success_response): payment = stripe_payment payment_info = create_payment_information(payment, FAKE_TOKEN) response = stripe_charge_success_response mock_charge_create.return_value = response response = authorize(payment_info, gateway_params) assert not response.error assert response.transaction_id == TRANSACTION_TOKEN assert response.kind == TransactionKind.AUTH assert response.is_success assert isclose(response.amount, TRANSACTION_AMOUNT) assert response.currency == TRANSACTION_CURRENCY assert response.raw_response == stripe_charge_success_response
def test_capture_3d_secure(stripe_payment, sandbox_gateway_config): PAYMENT_INTENT = "pi_1F6YmgIUmJaD6Oqv77HUh6qq" ERROR = ( "This PaymentIntent could not be captured because it" " has a status of requires_action." " Only a PaymentIntent with one of the following " "statuses may be captured: requires_capture." ) payment_info = create_payment_information(stripe_payment, PAYMENT_INTENT) response = capture(payment_info, sandbox_gateway_config) assert response.error == ERROR assert response.kind == TransactionKind.CAPTURE assert isclose(response.amount, TRANSACTION_AMOUNT) assert response.currency == TRANSACTION_CURRENCY assert not response.is_success assert response.action_required
def test_get_stripe_charge_payload_without_shipping(stripe_payment): stripe_payment.order.address = None payment_info = create_payment_information(stripe_payment, FAKE_TOKEN) billing_name = get_payment_billing_fullname(payment_info) expected_payload = { "capture": True, "amount": get_amount_for_stripe(TRANSACTION_AMOUNT, TRANSACTION_CURRENCY), "currency": get_currency_for_stripe(TRANSACTION_CURRENCY), "source": FAKE_TOKEN, "description": billing_name, } charge_payload = _get_stripe_charge_payload(payment_info, True) assert charge_payload == expected_payload
def test_get_stripe_charge_payload_without_shipping(stripe_payment): stripe_payment.order.shipping_address = None payment_info = create_payment_information(stripe_payment, FAKE_TOKEN) billing_name = get_payment_billing_fullname(payment_info) expected_payload = { 'capture': True, 'amount': get_amount_for_stripe(TRANSACTION_AMOUNT, TRANSACTION_CURRENCY), 'currency': get_currency_for_stripe(TRANSACTION_CURRENCY), 'source': FAKE_TOKEN, 'description': billing_name } charge_payload = _get_stripe_charge_payload(payment_info, True) assert charge_payload == expected_payload
def test_store_source_when_processing_payment( mock_payment_interface, payment_txn_preauth ): PAYMENT_DATA = create_payment_information( payment=payment_txn_preauth, payment_token=TOKEN, store_source=True ) mock_payment_interface.process_payment.return_value = PROCESS_PAYMENT_RESPONSE transaction = gateway.process_payment( payment=payment_txn_preauth, token=TOKEN, store_source=True ) mock_payment_interface.process_payment.assert_called_once_with( USED_GATEWAY, PAYMENT_DATA ) assert transaction.customer_id == PROCESS_PAYMENT_RESPONSE.customer_id
def test_charge(mock_charge_create, stripe_payment, gateway_params, stripe_charge_success_response): payment = stripe_payment payment_info = create_payment_information(payment, FAKE_TOKEN, TRANSACTION_AMOUNT) response = stripe_charge_success_response mock_charge_create.return_value = response response = charge(payment_info, gateway_params) assert not response['error'] assert response['transaction_id'] == TRANSACTION_TOKEN assert response['kind'] == TransactionKind.CHARGE assert response['is_success'] assert isclose(response['amount'], TRANSACTION_AMOUNT) assert response['currency'] == TRANSACTION_CURRENCY assert response['raw_response'] == stripe_charge_success_response
def test_refund_unsupported_currency(razorpay_payment, charged_payment, gateway_config): # Set the payment currency to an unsupported currency razorpay_payment.currency = "MXN" payment_info = create_payment_information( razorpay_payment, amount=TRANSACTION_AMOUNT ) # Attempt charging response = refund(payment_info, gateway_config) # Ensure a error was returned assert response.error == (errors.UNSUPPORTED_CURRENCY % {"currency": "MXN"}) assert not response.is_success # Ensure the kind is correctly set assert response.kind == TransactionKind.REFUND
def test_stripe_payment_form(stripe_payment, gateway_config): payment_info = create_payment_information(stripe_payment, FAKE_TOKEN) form = create_form( None, payment_information=payment_info, connection_params=gateway_config.connection_params, ) assert isinstance(form, StripePaymentModalForm) assert not form.is_valid() form = create_form( data={"stripeToken": FAKE_TOKEN}, payment_information=payment_info, connection_params=gateway_config.connection_params, ) assert isinstance(form, StripePaymentModalForm) assert form.is_valid()
def test_refund_success(paypal_paid_payment, sandbox_gateway_config): # Copy from cassettes/test_capture in response > body > payments > captures > id paypal_capture_id = "5DH34141WG814093W" payment_info = create_payment_information( paypal_paid_payment, amount=TRANSACTION_REFUND_AMOUNT, payment_token=paypal_capture_id, ) response = refund(payment_info, sandbox_gateway_config) assert not response.error assert response.kind == TransactionKind.REFUND assert response.is_success assert isclose(response.amount, TRANSACTION_REFUND_AMOUNT) assert response.currency == TRANSACTION_CURRENCY
def test_confirm_payment(mock_payment_interface, payment_txn_to_confirm): auth_transaction = payment_txn_to_confirm.transactions.get() PAYMENT_DATA = create_payment_information( payment=payment_txn_to_confirm, payment_token=auth_transaction.token, amount=CONFIRM_AMOUNT, ) mock_payment_interface.confirm_payment.return_value = CONFIRM_RESPONSE transaction = gateway.confirm(payment=payment_txn_to_confirm) mock_payment_interface.confirm_payment.assert_called_once_with( USED_GATEWAY, PAYMENT_DATA) assert transaction.amount == CONFIRM_RESPONSE.amount assert transaction.kind == TransactionKind.CONFIRM assert transaction.currency == "usd" assert transaction.gateway_response == RAW_RESPONSE
def test_authorize_error_response(mock_charge_create, stripe_payment, gateway_params): payment = stripe_payment payment_info = create_payment_information(payment, FAKE_TOKEN) stripe_error = stripe.error.InvalidRequestError(message=ERROR_MESSAGE, param=None) mock_charge_create.side_effect = stripe_error response = authorize(payment_info, gateway_params) assert response.error == ERROR_MESSAGE assert response.transaction_id == FAKE_TOKEN assert response.kind == TransactionKind.AUTH assert not response.is_success assert response.amount == payment.total assert response.currency == payment.currency assert response.raw_response == _get_error_response_from_exc(stripe_error)
def test_refund_unsupported_currency( razorpay_payment, charged_payment, gateway_params): # Set the payment currency to an unsupported currency razorpay_payment.currency = 'USD' payment_info = create_payment_information( razorpay_payment, amount=TRANSACTION_AMOUNT) # Attempt charging response = refund(payment_info, gateway_params) # Ensure a error was returned assert response['error'] == ( errors.UNSUPPORTED_CURRENCY % {'currency': 'USD'}) assert not response['is_success'] # Ensure the kind is correctly set assert response['kind'] == TransactionKind.REFUND
def test_full_refund_payment(mock_payment_interface, payment_txn_captured): capture_transaction = payment_txn_captured.transactions.get() PAYMENT_DATA = create_payment_information( payment=payment_txn_captured, amount=FULL_REFUND_AMOUNT, payment_token=capture_transaction.token, ) mock_payment_interface.refund_payment.return_value = FULL_REFUND_RESPONSE transaction = gateway.refund(payment=payment_txn_captured) mock_payment_interface.refund_payment.assert_called_once_with( USED_GATEWAY, PAYMENT_DATA) payment_txn_captured.refresh_from_db() assert payment_txn_captured.charge_status == ChargeStatus.FULLY_REFUNDED assert transaction.amount == FULL_REFUND_AMOUNT assert transaction.kind == TransactionKind.REFUND assert transaction.currency == "usd" assert transaction.gateway_response == RAW_RESPONSE
def test_void_error_response(mock_charge_retrieve, mock_refund_create, stripe_authorized_payment, gateway_params): payment = stripe_authorized_payment payment_info = create_payment_information(payment, TRANSACTION_TOKEN) mock_charge_retrieve.return_value = Mock(id='') stripe_error = stripe.error.InvalidRequestError(message=ERROR_MESSAGE, param=None) mock_refund_create.side_effect = stripe_error response = void(payment_info, gateway_params) assert response.error == ERROR_MESSAGE assert response.transaction_id == TRANSACTION_TOKEN assert response.kind == TransactionKind.VOID assert not response.is_success assert response.amount == payment.total assert response.currency == TRANSACTION_CURRENCY assert response.raw_response == {}
def test_void(mock_charge_retrieve, mock_refund_create, stripe_authorized_payment, gateway_params, stripe_refund_success_response): payment = stripe_authorized_payment payment_info = create_payment_information(payment, TRANSACTION_TOKEN) response = stripe_refund_success_response mock_charge_retrieve.return_value = Mock(id='') mock_refund_create.return_value = response response = void(payment_info, gateway_params) assert not response.error assert response.transaction_id == TRANSACTION_TOKEN assert response.kind == TransactionKind.VOID assert response.is_success assert isclose(response.amount, TRANSACTION_REFUND_AMOUNT) assert response.currency == TRANSACTION_CURRENCY assert response.raw_response == stripe_refund_success_response
def test_capture(mock_charge_retrieve, stripe_authorized_payment, gateway_params, stripe_charge_success_response): payment = stripe_authorized_payment payment_info = create_payment_information(payment, amount=TRANSACTION_AMOUNT) response = stripe_charge_success_response mock_charge_retrieve.return_value = Mock(capture=Mock( return_value=response)) response = capture(payment_info, gateway_params) assert not response.error assert response.transaction_id == TRANSACTION_TOKEN assert response.kind == TransactionKind.CAPTURE assert response.is_success assert isclose(response.amount, TRANSACTION_AMOUNT) assert response.currency == TRANSACTION_CURRENCY assert response.raw_response == stripe_charge_success_response
def test_refund(mock_gateway, payment_txn_captured, braintree_success_response, gateway_config): payment = payment_txn_captured amount = Decimal('10.00') mock_response = Mock(return_value=braintree_success_response) mock_gateway.return_value = Mock(transaction=Mock(refund=mock_response)) payment_info = create_payment_information(payment, 'token', amount) response = refund(payment_info, gateway_config) assert not response.error assert response.kind == TransactionKind.REFUND assert response.amount == braintree_success_response.transaction.amount assert response.currency == braintree_success_response.transaction.currency_iso_code assert response.transaction_id == braintree_success_response.transaction.id assert response.is_success == braintree_success_response.is_success mock_response.assert_called_once_with(amount_or_options=str(amount), transaction_id=payment_info.token)
def test_void_payment(mock_payment_interface, payment_txn_preauth): auth_transaction = payment_txn_preauth.transactions.get() PAYMENT_DATA = create_payment_information( payment=payment_txn_preauth, payment_token=auth_transaction.token, amount=VOID_AMOUNT, ) mock_payment_interface.void_payment.return_value = VOID_RESPONSE transaction = gateway.void(payment=payment_txn_preauth) mock_payment_interface.void_payment.assert_called_once_with( USED_GATEWAY, PAYMENT_DATA) payment_txn_preauth.refresh_from_db() assert not payment_txn_preauth.is_active assert transaction.amount == VOID_RESPONSE.amount assert transaction.kind == TransactionKind.VOID assert transaction.currency == "usd" assert transaction.gateway_response == RAW_RESPONSE
def test_capture_error_response(mock_charge_retrieve, stripe_authorized_payment, gateway_params): payment = stripe_authorized_payment payment_info = create_payment_information(payment, TRANSACTION_TOKEN, amount=TRANSACTION_AMOUNT) stripe_error = stripe.error.InvalidRequestError(message=ERROR_MESSAGE, param=None) mock_charge_retrieve.side_effect = stripe_error response = capture(payment_info, gateway_params) assert response.error == ERROR_MESSAGE assert response.transaction_id == TRANSACTION_TOKEN assert response.kind == TransactionKind.CAPTURE assert not response.is_success assert response.amount == payment.total assert response.currency == payment.currency assert response.raw_response == _get_error_response_from_exc(stripe_error)