def test_gateway_capture_partial_capture(
    mock_get_payment_gateway,
    mock_handle_fully_paid_order,
    payment_txn_preauth,
    gateway_params,
    settings,
    dummy_response,
):
    payment = payment_txn_preauth
    amount = payment.total * Decimal("0.5")
    txn = payment.transactions.first()
    txn.amount = amount
    txn.currency = settings.DEFAULT_CURRENCY

    dummy_response.kind = TransactionKind.CAPTURE
    dummy_response.amount = amount
    mock_capture = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (Mock(capture=mock_capture),
                                             gateway_params)

    gateway_capture(payment, amount)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.PARTIALLY_CHARGED
    assert payment.captured_amount == amount
    assert payment.currency == settings.DEFAULT_CURRENCY
    assert not mock_handle_fully_paid_order.called
def test_gateway_capture_failed(
    mock_get_payment_gateway,
    mock_handle_fully_paid_order,
    payment_txn_preauth,
    gateway_params,
    dummy_response,
):
    txn = payment_txn_preauth.transactions.first()
    txn.is_success = False

    payment = payment_txn_preauth
    amount = payment.total

    dummy_response.is_success = False
    dummy_response.kind = TransactionKind.CAPTURE
    mock_capture = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (Mock(capture=mock_capture),
                                             gateway_params)
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment, amount)
    assert exc.value.message == EXAMPLE_ERROR
    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.NOT_CHARGED
    assert not payment.captured_amount
    assert not mock_handle_fully_paid_order.called
Exemple #3
0
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)
Exemple #4
0
def test_gateway_charge_failed(
    mock_get_payment_gateway,
    mock_handle_fully_paid_order,
    payment_txn_preauth,
    gateway_config,
    transaction_token,
    dummy_response,
):
    txn = payment_txn_preauth.transactions.first()
    txn.is_success = False

    payment = payment_txn_preauth
    amount = payment.total

    dummy_response.is_success = False
    dummy_response.kind = TransactionKind.CAPTURE
    mock_charge = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (Mock(capture=mock_charge), gateway_config)
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment, amount)
    assert exc.value.message == EXAMPLE_ERROR
    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.NOT_CHARGED
    assert not payment.captured_amount
    assert not mock_handle_fully_paid_order.called
Exemple #5
0
def test_gateway_capture_partial_charge(
    mock_get_payment_gateway,
    mock_handle_fully_paid_order,
    payment_txn_preauth,
    gateway_config,
    transaction_token,
    settings,
    dummy_response,
):
    payment = payment_txn_preauth
    amount = payment.total * Decimal("0.5")
    txn = payment.transactions.first()
    txn.amount = amount
    txn.currency = settings.DEFAULT_CURRENCY

    dummy_response.kind = TransactionKind.CAPTURE
    dummy_response.amount = amount
    mock_charge = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (Mock(capture=mock_charge), gateway_config)

    gateway_capture(payment, amount)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.PARTIALLY_CHARGED
    assert payment.captured_amount == amount
    assert payment.currency == settings.DEFAULT_CURRENCY
    assert not mock_handle_fully_paid_order.called
def test_gateway_capture(
    mock_get_payment_gateway,
    mock_handle_fully_paid_order,
    payment_txn_preauth,
    gateway_params,
    dummy_response,
):
    payment = payment_txn_preauth
    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_params)

    payment_info = create_payment_information(payment, "", amount)
    gateway_capture(payment, amount)

    mock_get_payment_gateway.assert_called_once_with(payment.gateway)
    mock_capture.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)
Exemple #7
0
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
    assert payment.cc_brand == dummy_response.card_info.brand
    assert payment.cc_exp_month == dummy_response.card_info.exp_month
    assert payment.cc_exp_year == dummy_response.card_info.exp_year
    assert payment.cc_last_digits == dummy_response.card_info.last_4
    mock_handle_fully_paid_order.assert_called_once_with(payment.order)
Exemple #8
0
def test_capture_success(amount, payment_txn_preauth):
    txn = gateway_capture(payment=payment_txn_preauth, amount=Decimal(amount))
    assert txn.is_success
    assert txn.payment == payment_txn_preauth
    payment_txn_preauth.refresh_from_db()
    assert payment_txn_preauth.charge_status == ChargeStatus.CHARGED
    assert payment_txn_preauth.is_active
def test_capture_gateway_error(payment_txn_preauth, monkeypatch):
    monkeypatch.setattr("saleor.payment.gateways.dummy.dummy_success", lambda: False)
    with pytest.raises(PaymentError):
        txn = gateway_capture(payment=payment_txn_preauth, amount=80)
        assert txn.kind == TransactionKind.CAPTURE
        assert not txn.is_success
        assert txn.payment == payment_txn_preauth
Exemple #10
0
def test_capture_gateway_error(payment_txn_preauth, monkeypatch):
    monkeypatch.setattr("saleor.payment.gateways.dummy.dummy_success", lambda: False)
    with pytest.raises(PaymentError):
        txn = gateway_capture(payment=payment_txn_preauth, amount=80)
        assert txn.kind == TransactionKind.CAPTURE
        assert not txn.is_success
        assert txn.payment == payment_txn_preauth
Exemple #11
0
def test_capture_success(amount, charge_status, payment_txn_preauth):
    txn = gateway_capture(payment=payment_txn_preauth, amount=Decimal(amount))
    assert txn.is_success
    assert txn.payment == payment_txn_preauth
    payment_txn_preauth.refresh_from_db()
    assert payment_txn_preauth.charge_status == charge_status
    assert payment_txn_preauth.is_active
Exemple #12
0
def test_capture_failed(amount, captured_amount, charge_status, is_active,
                        payment_dummy):
    payment = payment_dummy
    payment.is_active = is_active
    payment.captured_amount = captured_amount
    payment.charge_status = charge_status
    payment.save()
    with pytest.raises(PaymentError):
        txn = gateway_capture(payment=payment, amount=amount)
        assert txn is None
Exemple #13
0
def test_gateway_capture_partial_capture(
        mock_get_payment_gateway, mock_handle_fully_paid_order, payment_txn_preauth,
        gateway_params, settings):
    payment = payment_txn_preauth
    amount = payment.total * Decimal('0.5')
    txn = payment.transactions.first()
    txn.amount = amount
    txn.currency = settings.DEFAULT_CURRENCY

    mock_capture = Mock(return_value=(txn, ''))
    mock_get_payment_gateway.return_value = (
        Mock(capture=mock_capture), gateway_params)

    gateway_capture(payment, amount)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.CHARGED
    assert payment.captured_amount == amount
    assert payment.currency == settings.DEFAULT_CURRENCY
    assert not mock_handle_fully_paid_order.called
Exemple #14
0
def test_capture_failed(
        amount, captured_amount, charge_status, is_active,
        payment_dummy):
    payment = payment_dummy
    payment.is_active = is_active
    payment.captured_amount = captured_amount
    payment.charge_status = charge_status
    payment.save()
    with pytest.raises(PaymentError):
        txn = gateway_capture(payment=payment, amount=amount)
        assert txn is None
Exemple #15
0
def test_gateway_capture(
        mock_get_payment_gateway, mock_handle_fully_paid_order, payment_txn_preauth,
        gateway_params):
    txn = payment_txn_preauth.transactions.first()
    payment = payment_txn_preauth
    assert not payment.captured_amount
    amount = payment.total

    mock_capture = Mock(return_value=(txn, ''))
    mock_get_payment_gateway.return_value = (
        Mock(capture=mock_capture), gateway_params)

    gateway_capture(payment, amount)
    mock_get_payment_gateway.assert_called_once_with(payment.gateway)
    mock_capture.assert_called_once_with(
        payment=payment, amount=amount, **gateway_params)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.CHARGED
    assert payment.captured_amount == payment.total
    mock_handle_fully_paid_order.assert_called_once_with(payment.order)
Exemple #16
0
def test_gateway_capture_errors(payment_txn_preauth):
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal("0"))
    assert exc.value.message == "Amount should be a positive number."

    payment_txn_preauth.charge_status = ChargeStatus.FULLY_REFUNDED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal("10"))
    assert exc.value.message == "This payment cannot be captured."

    payment_txn_preauth.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal("1000000"))
    assert exc.value.message == ("Unable to charge more than un-captured amount.")
Exemple #17
0
def test_gateway_capture_errors(payment_txn_preauth):
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal("0"))
    assert exc.value.message == "Amount should be a positive number."

    payment_txn_preauth.charge_status = ChargeStatus.FULLY_REFUNDED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal("10"))
    assert exc.value.message == "This payment cannot be captured."

    payment_txn_preauth.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal("1000000"))
    assert exc.value.message == ("Unable to charge more than un-captured amount.")
def test_gateway_capture_errors(payment_txn_preauth):
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal('0'))
    assert exc.value.message == 'Amount should be a positive number.'

    payment_txn_preauth.charge_status = ChargeStatus.FULLY_REFUNDED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal('10'))
    assert exc.value.message == 'This payment cannot be captured.'

    payment_txn_preauth.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_txn_preauth, Decimal('1000000'))
    assert exc.value.message == (
        'Unable to capture more than authorized amount.')
Exemple #19
0
def test_gateway_capture_errors(payment_dummy):
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_dummy, Decimal('0'))
    assert exc.value.message == 'Amount should be a positive number.'

    payment_dummy.charge_status = ChargeStatus.FULLY_REFUNDED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_dummy, Decimal('10'))
    assert exc.value.message == 'This payment cannot be captured.'

    payment_dummy.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment_dummy, Decimal('1000000'))
    assert exc.value.message == (
        'Unable to capture more than authorized amount.')
Exemple #20
0
def test_gateway_charge_errors(payment_dummy, transaction_token, settings):
    payment = payment_dummy
    gateway_authorize(payment, transaction_token)
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment, Decimal("0"))
    assert exc.value.message == "Amount should be a positive number."

    payment.charge_status = ChargeStatus.FULLY_REFUNDED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment, Decimal("10"))
    assert exc.value.message == "This payment cannot be captured."

    payment.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment, Decimal("1000000"))
    assert exc.value.message == ("Unable to charge more than un-captured amount.")
Exemple #21
0
def test_gateway_charge_errors(payment_dummy, transaction_token, settings):
    payment = payment_dummy
    gateway_authorize(payment, transaction_token)
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment, Decimal("0"))
    assert exc.value.message == "Amount should be a positive number."

    payment.charge_status = ChargeStatus.FULLY_REFUNDED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment, Decimal("10"))
    assert exc.value.message == "This payment cannot be captured."

    payment.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_capture(payment, Decimal("1000000"))
    assert exc.value.message == ("Unable to charge more than un-captured amount.")