예제 #1
0
def test_gateway_capture_partial_charge(
    mock_get_payment_gateway,
    mock_handle_fully_paid_order,
    payment_txn_preauth,
    gateway_params,
    transaction_token,
    settings,
    dummy_response,
):
    payment_token = transaction_token
    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(charge=mock_charge),
                                             gateway_params)

    gateway_charge(payment, payment_token, 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
예제 #2
0
def test_gateway_charge_failed(
    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()
    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(charge=mock_charge),
                                             gateway_params)
    with pytest.raises(PaymentError) as exc:
        gateway_charge(payment, payment_token, 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
예제 #3
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.order)
예제 #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
    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)
예제 #5
0
def test_charge_success(payment_dummy):
    gateway_charge(payment=payment_dummy, payment_token='fake-token')
    capture_txn = payment_dummy.transactions.last()

    assert capture_txn.is_success
    assert capture_txn.kind == TransactionKind.CAPTURE
    assert capture_txn.payment == payment_dummy
    assert capture_txn.amount == payment_dummy.total

    payment_dummy.refresh_from_db()
    assert payment_dummy.charge_status == ChargeStatus.FULLY_CHARGED
    assert payment_dummy.captured_amount == payment_dummy.total
    assert payment_dummy.is_active
예제 #6
0
def test_charge_success(payment_dummy):
    gateway_charge(payment=payment_dummy, payment_token='fake-token')
    capture_txn = payment_dummy.transactions.last()

    assert capture_txn.is_success
    assert capture_txn.kind == TransactionKind.CAPTURE
    assert capture_txn.payment == payment_dummy
    assert capture_txn.amount == payment_dummy.total

    payment_dummy.refresh_from_db()
    assert payment_dummy.charge_status == ChargeStatus.CHARGED
    assert payment_dummy.captured_amount == payment_dummy.total
    assert payment_dummy.is_active
예제 #7
0
def test_charge_failed(is_active, charge_status, payment_dummy):
    payment = payment_dummy
    payment.is_active = is_active
    payment.charge_status = charge_status
    payment.save()
    with pytest.raises(PaymentError):
        txn = gateway_charge(payment=payment, payment_token='Fake')
        assert txn is None
예제 #8
0
def test_charge_failed(is_active, charge_status, payment_dummy):
    payment = payment_dummy
    payment.is_active = is_active
    payment.charge_status = charge_status
    payment.save()
    with pytest.raises(PaymentError):
        txn = gateway_charge(payment=payment, payment_token='Fake')
        assert txn is None
예제 #9
0
def test_charge_gateway_error(payment_dummy, monkeypatch):
    monkeypatch.setattr(
        'saleor.payment.gateways.dummy.dummy_success', lambda: False)
    with pytest.raises(PaymentError):
        txn = gateway_charge(
            payment=payment_dummy, payment_token='Fake',
            amount=payment_dummy.total)
        assert txn.kind == TransactionKind.CHARGE
        assert not txn.is_success
        assert txn.payment == payment_dummy
예제 #10
0
def test_charge_gateway_error(payment_dummy, monkeypatch):
    monkeypatch.setattr('saleor.payment.gateways.dummy.dummy_success',
                        lambda: False)
    with pytest.raises(PaymentError):
        txn = gateway_charge(payment=payment_dummy,
                             payment_token='Fake',
                             amount=payment_dummy.total)
        assert txn.kind == TransactionKind.CHARGE
        assert not txn.is_success
        assert txn.payment == payment_dummy
예제 #11
0
def test_gateway_charge_failed(
        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()
    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(charge=mock_charge), gateway_params)
    with pytest.raises(PaymentError) as exc:
        gateway_charge(payment, payment_token, 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
예제 #12
0
def test_gateway_capture_partial_charge(mock_get_payment_gateway,
                                        mock_handle_fully_paid_order,
                                        payment_txn_preauth, gateway_params,
                                        transaction_token, settings):
    payment_token = transaction_token
    payment = payment_txn_preauth
    amount = payment.total * Decimal('0.5')
    txn = payment.transactions.first()
    txn.amount = amount
    txn.currency = settings.DEFAULT_CURRENCY

    mock_charge = Mock(return_value=(txn, ''))
    mock_get_payment_gateway.return_value = (Mock(charge=mock_charge),
                                             gateway_params)

    gateway_charge(payment, payment_token, 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
예제 #13
0
def test_gateway_capture_partial_charge(
        mock_get_payment_gateway, mock_handle_fully_paid_order, payment_txn_preauth,
        gateway_params, transaction_token, settings, dummy_response):
    payment_token = transaction_token
    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(charge=mock_charge), gateway_params)

    gateway_charge(payment, payment_token, 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
예제 #14
0
def test_gateway_charge(mock_get_payment_gateway, mock_handle_fully_paid_order,
                        payment_txn_preauth, gateway_params,
                        transaction_token):
    payment_token = transaction_token
    txn = payment_txn_preauth.transactions.first()
    payment = payment_txn_preauth
    assert not payment.captured_amount
    amount = payment.total

    mock_charge = Mock(return_value=(txn, ''))
    mock_get_payment_gateway.return_value = (Mock(charge=mock_charge),
                                             gateway_params)

    gateway_charge(payment, payment_token, amount)
    mock_get_payment_gateway.assert_called_once_with(payment.gateway)
    mock_charge.assert_called_once_with(payment=payment,
                                        payment_token=payment_token,
                                        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)
예제 #15
0
def test_gateway_charge_errors(payment_dummy, transaction_token):
    payment = payment_dummy
    payment_token = transaction_token

    with pytest.raises(PaymentError) as exc:
        gateway_charge(payment, payment_token, 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_charge(payment, payment_token, Decimal("10"))
    assert exc.value.message == "This payment cannot be charged."

    payment.charge_status = ChargeStatus.NOT_CHARGED
    with pytest.raises(PaymentError) as exc:
        gateway_charge(payment, payment_token, Decimal("1000000"))
    assert exc.value.message == (
        "Unable to charge more than un-captured amount.")
예제 #16
0
def test_gateway_charge_errors(payment_dummy, transaction_token):
    payment = payment_dummy
    payment_token = transaction_token

    with pytest.raises(PaymentError) as exc:
        gateway_charge(payment, payment_token, 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_charge(payment, payment_token, Decimal('10'))
    assert exc.value.message == 'This payment cannot be charged.'

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