Example #1
0
def test_refund_gateway_error(payment_txn_captured, monkeypatch):
    monkeypatch.setattr("saleor.payment.gateways.dummy.dummy_success", lambda: False)
    payment = payment_txn_captured
    payment.charge_status = ChargeStatus.FULLY_CHARGED
    payment.captured_amount = Decimal("80.00")
    payment.save()
    with pytest.raises(PaymentError):
        gateway_refund(payment=payment, amount=Decimal("80.00"))

    payment.refresh_from_db()
    txn = payment.transactions.last()
    assert txn.kind == TransactionKind.REFUND
    assert not txn.is_success
    assert txn.payment == payment
    assert payment.charge_status == ChargeStatus.FULLY_CHARGED
    assert payment.captured_amount == Decimal("80.00")
Example #2
0
def test_refund_gateway_error(payment_txn_captured, monkeypatch):
    monkeypatch.setattr("saleor.payment.gateways.dummy.dummy_success", lambda: False)
    payment = payment_txn_captured
    payment.charge_status = ChargeStatus.FULLY_CHARGED
    payment.captured_amount = Decimal("80.00")
    payment.save()
    with pytest.raises(PaymentError):
        gateway_refund(payment=payment, amount=Decimal("80.00"))

    payment.refresh_from_db()
    txn = payment.transactions.last()
    assert txn.kind == TransactionKind.REFUND
    assert not txn.is_success
    assert txn.payment == payment
    assert payment.charge_status == ChargeStatus.FULLY_CHARGED
    assert payment.captured_amount == Decimal("80.00")
Example #3
0
def test_gateway_refund_failed(
        mock_get_payment_gateway, payment_txn_captured, gateway_params, settings):
    txn = payment_txn_captured.transactions.first()
    payment = payment_txn_captured
    captured_before = payment.captured_amount
    txn.is_success = False

    mock_refund = Mock(return_value=(txn, EXAMPLE_ERROR))
    mock_get_payment_gateway.return_value = (
        Mock(refund=mock_refund), gateway_params)

    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal('10.00'))
    exc.value.message == EXAMPLE_ERROR
    payment.refresh_from_db()
    assert payment.captured_amount == captured_before
Example #4
0
def test_gateway_refund_partial_refund(
        mock_get_payment_gateway, payment_txn_captured, gateway_params, settings):
    payment = payment_txn_captured
    amount = payment.total * Decimal('0.5')
    txn = payment_txn_captured.transactions.first()
    txn.amount = amount
    txn.currency = settings.DEFAULT_CURRENCY

    mock_refund = Mock(return_value=(txn, ''))
    mock_get_payment_gateway.return_value = (
        Mock(refund=mock_refund), gateway_params)

    gateway_refund(payment, amount)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.CHARGED
    assert payment.captured_amount == payment.total - amount
Example #5
0
def test_gateway_refund(mock_get_payment_gateway, payment_txn_captured,
                        gateway_params):
    txn = payment_txn_captured.transactions.first()
    payment = payment_txn_captured
    amount = payment.total

    mock_refund = Mock(return_value=(txn, ''))
    mock_get_payment_gateway.return_value = (Mock(refund=mock_refund),
                                             gateway_params)

    gateway_refund(payment, amount)
    mock_get_payment_gateway.assert_called_once_with(payment.gateway)
    mock_refund.assert_called_once_with(payment, amount, **gateway_params)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.FULLY_REFUNDED
    assert not payment.captured_amount
Example #6
0
def test_refund_failed(initial_captured_amount, refund_amount,
                       initial_charge_status, payment_dummy):
    payment = payment_dummy
    payment.charge_status = initial_charge_status
    payment.captured_amount = Decimal(initial_captured_amount)
    payment.save()
    with pytest.raises(PaymentError):
        txn = gateway_refund(payment=payment, amount=Decimal(refund_amount))
        assert txn is None
Example #7
0
def test_gateway_refund_failed(
        mock_get_payment_gateway, payment_txn_captured, gateway_params,
        settings, dummy_response):
    txn = payment_txn_captured.transactions.first()
    payment = payment_txn_captured
    captured_before = payment.captured_amount
    txn.is_success = False

    dummy_response['kind'] = TransactionKind.REFUND
    dummy_response['is_success'] = False
    mock_refund = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (
        Mock(refund=mock_refund), gateway_params)

    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal('10.00'))
    exc.value.message == EXAMPLE_ERROR
    payment.refresh_from_db()
    assert payment.captured_amount == captured_before
Example #8
0
def test_refund_failed(
        initial_captured_amount, refund_amount, initial_charge_status,
        payment_dummy):
    payment = payment_dummy
    payment.charge_status = initial_charge_status
    payment.captured_amount = Decimal(initial_captured_amount)
    payment.save()
    with pytest.raises(PaymentError):
        txn = gateway_refund(payment=payment, amount=Decimal(refund_amount))
        assert txn is None
Example #9
0
def test_gateway_refund_partial_refund(
        mock_get_payment_gateway, payment_txn_captured, gateway_params,
        settings, dummy_response):
    payment = payment_txn_captured
    amount = payment.total * Decimal('0.5')
    txn = payment_txn_captured.transactions.first()
    txn.amount = amount
    txn.currency = settings.DEFAULT_CURRENCY

    dummy_response.kind = TransactionKind.REFUND
    dummy_response.amount = amount
    mock_refund = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (
        Mock(refund=mock_refund), gateway_params)

    gateway_refund(payment, amount)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.PARTIALLY_REFUNDED
    assert payment.captured_amount == payment.total - amount
Example #10
0
def test_gateway_refund_partial_refund(
        mock_get_payment_gateway, payment_txn_captured, gateway_params,
        settings, dummy_response):
    payment = payment_txn_captured
    amount = payment.total * Decimal('0.5')
    txn = payment_txn_captured.transactions.first()
    txn.amount = amount
    txn.currency = settings.DEFAULT_CURRENCY

    dummy_response['kind'] = TransactionKind.REFUND
    dummy_response['amount'] = amount
    mock_refund = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (
        Mock(refund=mock_refund), gateway_params)

    gateway_refund(payment, amount)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.PARTIALLY_REFUNDED
    assert payment.captured_amount == payment.total - amount
Example #11
0
def test_gateway_refund(mock_get_payment_gateway, payment_txn_captured,
                        gateway_params, dummy_response):
    txn = payment_txn_captured.transactions.first()
    payment = payment_txn_captured
    amount = payment.total

    dummy_response.kind = TransactionKind.REFUND
    mock_refund = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (Mock(refund=mock_refund),
                                             gateway_params)

    payment_info = create_payment_information(payment, txn.token, amount)
    gateway_refund(payment, amount)

    mock_get_payment_gateway.assert_called_once_with(payment.gateway)
    mock_refund.assert_called_once_with(payment_information=payment_info,
                                        connection_params=gateway_params)

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.FULLY_REFUNDED
    assert not payment.captured_amount
Example #12
0
def test_gateway_refund(
    mock_get_payment_gateway, payment_txn_captured, gateway_config, dummy_response
):
    txn = payment_txn_captured.transactions.first()
    payment = payment_txn_captured
    amount = payment.total

    dummy_response.kind = TransactionKind.REFUND
    mock_refund = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (Mock(refund=mock_refund), gateway_config)

    payment_info = create_payment_information(payment, txn.token, amount)
    gateway_refund(payment, amount)

    mock_get_payment_gateway.assert_called_once_with(payment.gateway)
    mock_refund.assert_called_once_with(
        payment_information=payment_info, config=gateway_config
    )

    payment.refresh_from_db()
    assert payment.charge_status == ChargeStatus.FULLY_REFUNDED
    assert not payment.captured_amount
Example #13
0
def test_gateway_refund_failed(
    mock_get_payment_gateway,
    payment_txn_captured,
    gateway_config,
    settings,
    dummy_response,
):
    txn = payment_txn_captured.transactions.first()
    payment = payment_txn_captured
    captured_before = payment.captured_amount
    txn.is_success = False

    dummy_response.kind = TransactionKind.REFUND
    dummy_response.is_success = False
    mock_refund = Mock(return_value=dummy_response)
    mock_get_payment_gateway.return_value = (Mock(refund=mock_refund), gateway_config)

    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal("10.00"))
    assert exc.value.message == EXAMPLE_ERROR
    payment.refresh_from_db()
    assert payment.captured_amount == captured_before
Example #14
0
def test_refund_success(
        initial_captured_amount, refund_amount, final_captured_amount,
        final_charge_status, active_after, payment_txn_captured):
    payment = payment_txn_captured
    payment.charge_status = ChargeStatus.FULLY_CHARGED
    payment.captured_amount = initial_captured_amount
    payment.save()
    txn = gateway_refund(payment=payment, amount=Decimal(refund_amount))
    assert txn.kind == TransactionKind.REFUND
    assert txn.is_success
    assert txn.payment == payment
    assert payment.charge_status == final_charge_status
    assert payment.captured_amount == final_captured_amount
    assert payment.is_active == active_after
Example #15
0
def test_refund_success(initial_captured_amount, refund_amount,
                        final_captured_amount, final_charge_status,
                        active_after, payment_txn_captured):
    payment = payment_txn_captured
    payment.charge_status = ChargeStatus.CHARGED
    payment.captured_amount = initial_captured_amount
    payment.save()
    txn = gateway_refund(payment=payment, amount=Decimal(refund_amount))
    assert txn.kind == TransactionKind.REFUND
    assert txn.is_success
    assert txn.payment == payment
    assert payment.charge_status == final_charge_status
    assert payment.captured_amount == final_captured_amount
    assert payment.is_active == active_after
Example #16
0
def test_gateway_refund_errors(payment_txn_captured):
    payment = payment_txn_captured
    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal("1000000"))
    assert exc.value.message == "Cannot refund more than captured"

    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal("0"))
    assert exc.value.message == "Amount should be a positive number."

    payment.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal("1"))
    assert exc.value.message == "This payment cannot be refunded."
Example #17
0
def test_gateway_refund_errors(payment_txn_captured):
    payment = payment_txn_captured
    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal("1000000"))
    assert exc.value.message == "Cannot refund more than captured"

    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal("0"))
    assert exc.value.message == "Amount should be a positive number."

    payment.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_refund(payment, Decimal("1"))
    assert exc.value.message == "This payment cannot be refunded."