def test_widget_with_additional_attr(stripe_payment, gateway_params):
    payment_info = create_payment_information(stripe_payment)

    widget = StripeCheckoutWidget(payment_info,
                                  gateway_params,
                                  attrs={'data-custom': 'custom-data'})
    assert 'data-custom="custom-data"' in widget.render()
Example #2
0
def test_get_customer_data(payment_dummy):
    payment = payment_dummy
    payment_info = create_payment_information(payment)
    result = get_customer_data(payment_info)
    expected_result = {
        'task_id': payment.task_id,
        'billing': {
            'first_name': payment.billing_first_name,
            'last_name': payment.billing_last_name,
            'company': payment.billing_company_name,
            'postal_code': payment.billing_postal_code,
            'street_address': payment.billing_address_1[:255],
            'extended_address': payment.billing_address_2[:255],
            'locality': payment.billing_city,
            'region': payment.billing_country_area,
            'country_code_alpha2': payment.billing_country_code
        },
        'risk_data': {
            'customer_ip': payment.customer_ip_address or ''
        },
        'customer': {
            'email': payment.billing_email
        }
    }
    assert result == expected_result
Example #3
0
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)
    })
Example #4
0
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.task)
Example #5
0
def test_charge_invalid_request(
        mocked_gateway,
        mocked_logger,
        razorpay_payment,
        gateway_params):

    # Data to be passed
    payment_token = '123'

    # Assign the side effect to the gateway's `charge()` 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 = charge(payment_info, gateway_params)

    # 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.CHARGE
    assert response['transaction_id'] == payment_token

    # Ensure the HTTP error was logged
    assert mocked_logger.call_count == 1
Example #6
0
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']
Example #7
0
def test_refund_invalid_data(
        mocked_gateway,
        mocked_logger,
        charged_payment,
        razorpay_success_response,
        gateway_params):

    # 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_params)

    # 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
Example #8
0
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']
Example #9
0
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)
Example #10
0
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
Example #11
0
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_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()
Example #14
0
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
Example #15
0
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_widget_with_enable_delivery_address_option(stripe_payment,
                                                    gateway_params):
    payment_info = create_payment_information(stripe_payment, FAKE_TOKEN)

    gateway_params['enable_delivery_address'] = True
    widget = StripeCheckoutWidget(payment_info, gateway_params)
    assert 'data-delivery-address="true"' in widget.render()

    gateway_params['enable_delivery_address'] = False
    widget = StripeCheckoutWidget(payment_info, gateway_params)
    assert 'data-delivery-address="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-delivery-address="false" '
        'data-zip-code="false" src="https://checkout.stripe.com/checkout.js">'
        '</script>')
Example #18
0
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>')
Example #19
0
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
    }
Example #20
0
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>')
Example #21
0
def test_capture_incorrect_token(mock_gateway, payment_txn_preauth,
                                 braintree_not_found_error, gateway_config):
    payment = payment_txn_preauth
    amount = Decimal('10.00')
    mock_response = Mock(side_effect=braintree_not_found_error)
    mock_gateway.return_value = Mock(transaction=Mock(
        submit_for_settlement=mock_response))

    payment_info = create_payment_information(payment)
    with pytest.raises(BraintreeException) as e:
        capture(payment_info, gateway_config)
    assert str(e.value) == DEFAULT_ERROR_MESSAGE
Example #22
0
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()
Example #23
0
def test_void_error_response(mock_gateway, payment_txn_preauth,
                             braintree_error_response, gateway_config):
    payment = payment_txn_preauth
    mock_response = Mock(return_value=braintree_error_response)
    mock_gateway.return_value = Mock(transaction=Mock(void=mock_response))

    payment_info = create_payment_information(payment)
    response = void(payment_info, gateway_config)

    assert response['raw_response'] == extract_gateway_response(
        braintree_error_response)
    assert not response['is_success']
    assert response['error'] == DEFAULT_ERROR
def test_stripe_payment_form(stripe_payment, gateway_params):
    payment_info = create_payment_information(stripe_payment, FAKE_TOKEN)
    form = create_form(None,
                       payment_information=payment_info,
                       connection_params=gateway_params)
    assert isinstance(form, StripePaymentModalForm)
    assert not form.is_valid()

    form = create_form(data={'stripeToken': FAKE_TOKEN},
                       payment_information=payment_info,
                       connection_params=gateway_params)
    assert isinstance(form, StripePaymentModalForm)
    assert form.is_valid()
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_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_dummy_payment_form(kind, charge_status, payment_dummy):
    payment = payment_dummy
    data = {'charge_status': charge_status}
    payment_gateway, gateway_params = get_payment_gateway(payment.gateway)
    payment_info = create_payment_information(payment)

    form = payment_gateway.create_form(data=data,
                                       payment_information=payment_info,
                                       connection_params=gateway_params)
    assert form.is_valid()
    gateway_process_payment(payment=payment,
                            payment_token=form.get_payment_token())
    payment.refresh_from_db()
    assert payment.transactions.last().kind == kind
Example #28
0
def test_authorize_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 = authorize(payment_info, gateway_config)

    assert response['raw_response'] == extract_gateway_response(
        braintree_error_response)
    assert not response['is_success']
    assert response['error'] == DEFAULT_ERROR
Example #29
0
def test_refund_error_response(mock_gateway, payment_txn_captured,
                               braintree_error_response, gateway_config):
    payment = payment_txn_captured
    amount = Decimal('10.00')
    mock_response = Mock(return_value=braintree_error_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 response['raw_response'] == extract_gateway_response(
        braintree_error_response)
    assert not response['is_success']
    assert response['error'] == DEFAULT_ERROR
Example #30
0
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)