def test_payments_get_by_id(client): custom_type = client.types.create( types.TypeDraft( name=types.LocalizedString(en="myType"), key="payment-info", resource_type_ids=[types.ResourceTypeId.PAYMENT_INTERFACE_INTERACTION], field_definitions=[ types.FieldDefinition( type=types.CustomFieldStringType(), name="operations", label=types.LocalizedString(en="Operation"), required=False, ) ], ) ) payment = client.payments.create( types.PaymentDraft( key="test-payment", amount_planned=types.Money(cent_amount=2000, currency_code="GBP"), payment_method_info=types.PaymentMethodInfo( payment_interface="ADYEN", method="mc" ), transactions=[ types.TransactionDraft( type=types.TransactionType.CHARGE, amount=types.Money(cent_amount=2000, currency_code="GBP"), interaction_id="8525483242578266", state=types.TransactionState.SUCCESS, ) ], interface_interactions=[ types.CustomFieldsDraft( type=types.TypeResourceIdentifier(id=custom_type.id), fields=types.FieldContainer( { "operations": "CANCEL,CAPTURE,REFUND", "success": True, "psp_reference": "8525483242578266", "merchant_reference": "some reference", "reason": "82132:0005:10/2020", "amount": 2000, "payment_method": "mc", "event_date": "2019-01-24T11:04:17.000000Z", "currency_code": "GBP", "event_code": "AUTHORISATION", "merchant_account_code": "TestMerchant", } ), ) ], ) ) assert payment.id assert payment.key == "test-payment"
def test_products_create(client): custom_type = client.types.create( types.TypeDraft( name=types.LocalizedString(en="myType"), key="my-type", resource_type_ids=[types.ResourceTypeId.ASSET], field_definitions=[ types.FieldDefinition( name="foo", type=types.CustomFieldStringType(), label=types.LocalizedString(en="foo"), required=False, ) ], ) ) assert custom_type.id draft = types.ProductDraft( key="test-product", publish=True, name=types.LocalizedString(en=f"my-product"), slug=types.LocalizedString(en=f"my-product"), product_type=types.ProductTypeResourceIdentifier(key="dummy"), master_variant=types.ProductVariantDraft( assets=[ types.AssetDraft( sources=[], name=types.LocalizedString(en="something"), custom=types.CustomFieldsDraft( type=types.TypeResourceIdentifier(id=custom_type.id), fields=types.FieldContainer(foo="bar"), ), ) ], prices=[ types.PriceDraft( value=types.CentPrecisionMoneyDraft( cent_amount=1000, currency_code="EUR" ), country="NL", ) ], ), ) product = client.products.create(draft) assert product.id assert product.master_data.current.master_variant.assets assert product.master_data.current.master_variant.prices
def post_load(self, data, **kwargs): del data["name"] return types.CustomFieldStringType(**data)
def test_update_actions(client): custom_type = client.types.create( types.TypeDraft( name=types.LocalizedString(en="myType"), key="payment-info", resource_type_ids=[types.ResourceTypeId.PAYMENT_INTERFACE_INTERACTION], field_definitions=[ types.FieldDefinition( type=types.CustomFieldStringType(), name="operations", label=types.LocalizedString(en="Operation"), required=False, ) ], ) ) payment = client.payments.create( types.PaymentDraft( key="test-payment", amount_planned=types.Money(cent_amount=2000, currency_code="GBP"), payment_method_info=types.PaymentMethodInfo( payment_interface="ADYEN", method="mc" ), transactions=[ types.TransactionDraft( type=types.TransactionType.CHARGE, amount=types.Money(cent_amount=2000, currency_code="GBP"), state=types.TransactionState.PENDING, ) ], ) ) existing_transaction = payment.transactions[0] payment = client.payments.update_by_id( payment.id, payment.version, actions=[ types.PaymentAddInterfaceInteractionAction( type=types.TypeResourceIdentifier(id=custom_type.id), fields=types.FieldContainer({"pspRef": "1337"}), ), types.PaymentChangeTransactionInteractionIdAction( transaction_id=existing_transaction.id, interaction_id="1337" ), types.PaymentAddTransactionAction( transaction=types.TransactionDraft( type=types.TransactionType.CHARGE, amount=types.Money(currency_code="GBP", cent_amount=1000), interaction_id="123", state=types.TransactionState.INITIAL, ) ), types.PaymentChangeTransactionStateAction( transaction_id=existing_transaction.id, state=types.TransactionState.SUCCESS, ), ], ) assert payment.interface_interactions[0].fields == {"pspRef": "1337"} assert payment.transactions[0].interaction_id == "1337" assert len(payment.transactions) == 2 assert payment.transactions[0].state == types.TransactionState.SUCCESS
def test_update_actions(commercetools_api, client, cart_draft): cart = client.carts.create(cart_draft) payment_reference = types.PaymentReference(id=str(uuid.uuid4())) cart = client.carts.update_by_id( cart.id, cart.version, actions=[types.CartAddPaymentAction(payment=payment_reference)], ) type_draft = types.TypeDraft( key="foobar", resource_type_ids=[types.ResourceTypeId.ORDER], name={"en-US": "test"}, field_definitions=[ types.FieldDefinition( type=types.CustomFieldStringType(), name="foo1", label={"en-US": "foo-1"}, required=False, ), types.FieldDefinition( type=types.CustomFieldSetType(element_type=None), name="foo2", label={"en-US": "foo-2"}, required=False, ), types.FieldDefinition( type=types.CustomFieldBooleanType(), name="foo3", label={"en-US": "foo-3"}, required=False, ), ], ) custom_type = client.types.create(type_draft) assert custom_type.id assert cart.payment_info.payments[0] == payment_reference cart = client.carts.update_by_id( cart.id, cart.version, actions=[ types.CartSetCustomTypeAction(type=types.TypeResourceIdentifier( id=custom_type.id)) ], ) client.carts.update_by_id( cart.id, cart.version, actions=[ types.CartSetCustomFieldAction(name="foo1", value="bar"), types.CartSetCustomFieldAction(name="foo2", value=["bar"]), types.CartSetCustomFieldAction(name="foo3", value=False), ], ) cart = client.carts.get_by_id(cart.id) assert all(key in cart.custom.fields for key in ["foo1", "foo2", "foo3"]) assert cart.custom.fields["foo1"] == "bar" assert cart.custom.fields["foo2"] == ["bar"] assert cart.custom.fields["foo3"] is False