def test_append_klarna_data_tax_included( mocked_checkout_line_total, mocked_plugins_manager, dummy_payment_data, payment_dummy, checkout_ready_to_complete, ): # given net = Money(100, "USD") gross = Money(123, "USD") # tax 23 % mocked_checkout_line_total.return_value = quantize_price( TaxedMoney(net=net, gross=gross), "USD" ) manager = get_plugins_manager() unit_price = mock.MagicMock() unit_price.return_value = quantize_price(TaxedMoney(net=net, gross=gross), "USD") manager.calculate_checkout_line_unit_price = unit_price mocked_plugins_manager.return_value = manager country_code = checkout_ready_to_complete.get_country() checkout_ready_to_complete.payments.add(payment_dummy) line = checkout_ready_to_complete.lines.first() payment_data = { "reference": "test", } # when result = append_klarna_data(dummy_payment_data, payment_data) # then expected_result = { "reference": "test", "shopperLocale": get_shopper_locale_value(country_code), "shopperReference": dummy_payment_data.customer_email, "countryCode": country_code, "lineItems": [ { "description": f"{line.variant.product.name}, {line.variant.name}", "quantity": line.quantity, "id": line.variant.sku, "taxAmount": price_to_minor_unit((gross - net).amount, "USD"), "taxPercentage": 2300, "amountExcludingTax": price_to_minor_unit(net.amount, "USD"), "amountIncludingTax": price_to_minor_unit(gross.amount, "USD"), }, { "amountExcludingTax": "1000", "amountIncludingTax": "1000", "description": "Shipping - DHL", "id": f"Shipping:{checkout_ready_to_complete.shipping_method.id}", "quantity": 1, "taxAmount": "0", "taxPercentage": 0, }, ], } assert result == expected_result
def test_append_checkout_details_tax_included( mocked_calculate_checkout_line_unit_price, dummy_payment_data, payment_dummy, checkout_ready_to_complete, ): # given net = Money(100, "USD") gross = Money(123, "USD") # tax 23 % unit_price = quantize_price(TaxedMoney(net=net, gross=gross), "USD") country_code = checkout_ready_to_complete.get_country() mocked_calculate_checkout_line_unit_price.return_value = CheckoutTaxedPricesData( price_with_sale=unit_price, undiscounted_price=unit_price, price_with_discounts=unit_price, ) checkout_ready_to_complete.payments.add(payment_dummy) line = checkout_ready_to_complete.lines.first() payment_data = { "reference": "test", } # when result = append_checkout_details(dummy_payment_data, payment_data) # then expected_result = { "reference": "test", "shopperLocale": get_shopper_locale_value(country_code), "countryCode": country_code, "lineItems": [ { "description": f"{line.variant.product.name}, {line.variant.name}", "quantity": line.quantity, "id": line.variant.sku, "taxAmount": price_to_minor_unit((gross - net).amount, "USD"), "taxPercentage": 2300, "amountExcludingTax": price_to_minor_unit(net.amount, "USD"), "amountIncludingTax": price_to_minor_unit(gross.amount, "USD"), }, { "amountExcludingTax": "1000", "amountIncludingTax": "1000", "description": "Shipping - DHL", "id": f"Shipping:{checkout_ready_to_complete.shipping_method.id}", "quantity": 1, "taxAmount": "0", "taxPercentage": 0, }, ], } assert result == expected_result
def test_request_data_for_payment_channel_different_than_web(dummy_payment_data): # given return_url = "https://www.example.com" merchant_account = "MerchantTestAccount" data = {"is_valid": True, "paymentMethod": {"type": "scheme"}, "channel": "iOS"} dummy_payment_data.data = data native_3d_secure = True # when result = request_data_for_payment( dummy_payment_data, return_url, merchant_account, native_3d_secure ) # then assert result == { "amount": { "value": price_to_minor_unit( dummy_payment_data.amount, dummy_payment_data.currency ), "currency": dummy_payment_data.currency, }, "reference": dummy_payment_data.graphql_payment_id, "paymentMethod": {"type": "scheme"}, "returnUrl": return_url, "merchantAccount": merchant_account, "channel": "iOS", "additionalData": {"allow3DS2": "true"}, "shopperEmail": "*****@*****.**", }
def test_request_data_for_payment_without_shipping( dummy_payment_data, dummy_address_data ): # given return_url = "https://www.example.com" merchant_account = "MerchantTestAccount" origin_url = "https://www.example.com" data = { "is_valid": True, "riskData": {"clientData": "test_client_data"}, "paymentMethod": {"type": "scheme"}, "browserInfo": {"acceptHeader": "*/*", "colorDepth": 30, "language": "pl"}, "shopperIP": "123", "originUrl": origin_url, } dummy_payment_data.data = data dummy_payment_data.billing = dummy_address_data dummy_payment_data.shipping = None native_3d_secure = False # when result = request_data_for_payment( dummy_payment_data, return_url, merchant_account, native_3d_secure ) # then assert result == { "amount": { "value": price_to_minor_unit( dummy_payment_data.amount, dummy_payment_data.currency ), "currency": dummy_payment_data.currency, }, "reference": dummy_payment_data.graphql_payment_id, "paymentMethod": {"type": "scheme"}, "returnUrl": return_url, "merchantAccount": merchant_account, "origin": return_url, "shopperIP": data["shopperIP"], "browserInfo": data["browserInfo"], "channel": "web", "shopperEmail": "*****@*****.**", "shopperName": { "firstName": dummy_payment_data.billing.first_name, "lastName": dummy_payment_data.billing.last_name, }, "shopperReference": "*****@*****.**", "billingAddress": { "city": "WROCŁAW", "country": "PL", "houseNumberOrName": "Mirumee Software", "postalCode": "53-601", "stateOrProvince": "ZZ", "street": "Tęczowa 7", }, }
def test_request_data_for_payment_channel_different_than_web( dummy_payment_data, dummy_address_data ): # given return_url = "https://www.example.com" merchant_account = "MerchantTestAccount" data = {"is_valid": True, "paymentMethod": {"type": "scheme"}, "channel": "iOS"} dummy_payment_data.data = data dummy_payment_data.billing = dummy_address_data dummy_payment_data.shipping = dummy_address_data native_3d_secure = True # when result = request_data_for_payment( dummy_payment_data, return_url, merchant_account, native_3d_secure ) # then assert result == { "amount": { "value": price_to_minor_unit( dummy_payment_data.amount, dummy_payment_data.currency ), "currency": dummy_payment_data.currency, }, "reference": dummy_payment_data.graphql_payment_id, "paymentMethod": {"type": "scheme"}, "returnUrl": return_url, "merchantAccount": merchant_account, "channel": "iOS", "additionalData": {"allow3DS2": "true"}, "shopperEmail": "*****@*****.**", "shopperName": { "firstName": dummy_payment_data.billing.first_name, "lastName": dummy_payment_data.billing.last_name, }, "shopperReference": "*****@*****.**", "deliveryAddress": { "city": "WROCŁAW", "country": "PL", "houseNumberOrName": "Mirumee Software", "postalCode": "53-601", "stateOrProvince": "ZZ", "street": "Tęczowa 7", }, "billingAddress": { "city": "WROCŁAW", "country": "PL", "houseNumberOrName": "Mirumee Software", "postalCode": "53-601", "stateOrProvince": "ZZ", "street": "Tęczowa 7", }, }
def test_append_checkout_details_without_sku(dummy_payment_data, payment_dummy, checkout_ready_to_complete): # given checkout_ready_to_complete.payments.add(payment_dummy) channel_id = checkout_ready_to_complete.channel_id line = checkout_ready_to_complete.lines.first() line.variant.sku = None line.variant.save() payment_data = { "reference": "test", } country_code = checkout_ready_to_complete.get_country() # when result = append_checkout_details(dummy_payment_data, payment_data) # then variant_channel_listing = line.variant.channel_listings.get( channel_id=channel_id) variant_price = variant_channel_listing.price_amount variant_currency = variant_channel_listing.currency price = price_to_minor_unit(variant_price, variant_currency) assert result == { "reference": "test", "shopperLocale": get_shopper_locale_value(country_code), "countryCode": country_code, "lineItems": [ { "description": f"{line.variant.product.name}, {line.variant.name}", "quantity": line.quantity, "id": line.variant.get_global_id(), "taxAmount": "0", "taxPercentage": 0, "amountExcludingTax": price, "amountIncludingTax": price, }, { "amountExcludingTax": "1000", "amountIncludingTax": "1000", "description": "Shipping - DHL", "id": f"Shipping:{checkout_ready_to_complete.shipping_method.id}", "quantity": 1, "taxAmount": "0", "taxPercentage": 0, }, ], }
def test_refund_payment_intent(mocked_refund): api_key = "api_key" payment_intent_id = "id1234" amount = price_to_minor_unit(Decimal("10.0"), "USD") mocked_refund.create.return_value = StripeObject() intent, _ = refund_payment_intent(api_key=api_key, payment_intent_id=payment_intent_id, amount_to_refund=amount) mocked_refund.create.assert_called_with( payment_intent=payment_intent_id, amount=amount, api_key=api_key, stripe_version=STRIPE_API_VERSION, ) assert isinstance(intent, StripeObject)
def test_request_data_for_payment_append_klarna_data( append_klarna_data_mock, dummy_payment_data ): # given return_url = "https://www.example.com" merchant_account = "MerchantTestAccount" origin_url = "https://www.example.com" data = { "is_valid": True, "riskData": {"clientData": "test_client_data"}, "paymentMethod": {"type": "klarna"}, "browserInfo": {"acceptHeader": "*/*", "colorDepth": 30, "language": "pl"}, "billingAddress": {"address": "test_address"}, "shopperIP": "123", "originUrl": origin_url, } dummy_payment_data.data = data klarna_result = { "amount": { "value": price_to_minor_unit( dummy_payment_data.amount, dummy_payment_data.currency ), "currency": dummy_payment_data.currency, }, "reference": dummy_payment_data.graphql_payment_id, "paymentMethod": {"type": "scheme"}, "returnUrl": return_url, "merchantAccount": merchant_account, "origin": return_url, "shopperIP": data["shopperIP"], "billingAddress": data["billingAddress"], "browserInfo": data["browserInfo"], "shopperLocale": "test_shopper", } append_klarna_data_mock.return_value = klarna_result native_3d_secure = False # when result = request_data_for_payment( dummy_payment_data, return_url, merchant_account, native_3d_secure ) # then assert result == klarna_result
def test_refund_payment_intent_returns_error(mocked_refund): api_key = "api_key" payment_intent_id = "id1234" amount = price_to_minor_unit(Decimal("10.0"), "USD") expected_error = StripeError(message="stripe-error") mocked_refund.create.side_effect = expected_error _, error = refund_payment_intent(api_key=api_key, payment_intent_id=payment_intent_id, amount_to_refund=amount) mocked_refund.create.assert_called_with( payment_intent=payment_intent_id, amount=amount, api_key=api_key, stripe_version=STRIPE_API_VERSION, ) assert error == expected_error
def test_request_data_for_payment_native_3d_secure(dummy_payment_data): # given return_url = "https://www.example.com" merchant_account = "MerchantTestAccount" origin_url = "https://www.example.com" data = { "is_valid": True, "riskData": {"clientData": "test_client_data"}, "paymentMethod": {"type": "scheme"}, "browserInfo": {"acceptHeader": "*/*", "colorDepth": 30, "language": "pl"}, "billingAddress": {"address": "test_address"}, "shopperIP": "123", "originUrl": origin_url, } dummy_payment_data.data = data native_3d_secure = True # when result = request_data_for_payment( dummy_payment_data, return_url, merchant_account, native_3d_secure ) # then assert result == { "amount": { "value": price_to_minor_unit( dummy_payment_data.amount, dummy_payment_data.currency ), "currency": dummy_payment_data.currency, }, "reference": dummy_payment_data.graphql_payment_id, "paymentMethod": {"type": "scheme"}, "returnUrl": return_url, "merchantAccount": merchant_account, "origin": origin_url, "shopperIP": data["shopperIP"], "billingAddress": data["billingAddress"], "browserInfo": data["browserInfo"], "channel": "web", "additionalData": {"allow3DS2": "true"}, "shopperEmail": "*****@*****.**", }
def test_to_adyen_price(value, currency, expected_result): # when result = price_to_minor_unit(value, currency) # then assert result == expected_result