diff --git a/commerce_coordinator/apps/commercetools/catalog_info/constants.py b/commerce_coordinator/apps/commercetools/catalog_info/constants.py index 0e20da1e..70d67c60 100644 --- a/commerce_coordinator/apps/commercetools/catalog_info/constants.py +++ b/commerce_coordinator/apps/commercetools/catalog_info/constants.py @@ -15,6 +15,24 @@ class TwoUKeys: SUCCESS_FULFILMENT_STATE = '2u-fulfillment-success-state' FAILURE_FULFILMENT_STATE = '2u-fulfillment-failure-state' + # Payment Custom Type Keys for PayPal + PAYPAL_PAYMENT_TYPE = 'paypal-payment-type' + + # Payment Custom Fields Keys for PayPal + PAYPAL_ORDER_ID_FIELD_KEY = 'PayPalOrderId' + PAYPAL_CLIENT_TOKEN_REQUEST_FIELD_KEY = 'getClientTokenRequest' + PAYPAL_CLIENT_TOKEN_RESPONSE_FIELD_KEY = 'getClientTokenResponse' + PAYPAL_CREATE_ORDER_REQUEST_FIELD_KEY = 'createPayPalOrderRequest' + PAYPAL_CREATE_ORDER_RESPONSE_FIELD_KEY = 'createPayPalOrderResponse' + PAYPAL_CAPTURE_ORDER_REQUEST_FIELD_KEY = 'capturePayPalOrderRequest' + PAYPAL_CAPTURE_ORDER_RESPONSE_FIELD_KEY = 'capturePayPalOrderResponse' + PAYPAL_UPDATE_ORDER_REQUEST_FIELD_KEY = 'updatePayPalOrderRequest' + PAYPAL_UPDATE_ORDER_RESPONSE_FIELD_KEY = 'updatePayPalOrderResponse' + + # Custom Objects keys + PAYPAL_CONNECTOR_CONTAINER = 'paypal-commercetools-connector' + PAYPAL_CONNECTOR_SETTINGS_KEY = 'settings' + class EdXFieldNames: """edX Specific field names for use in Commercetools""" diff --git a/commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py b/commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py index 189da1f8..4613b204 100644 --- a/commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py +++ b/commerce_coordinator/apps/commercetools/catalog_info/foundational_types.py @@ -1,5 +1,6 @@ from commercetools.platform.models import ( CustomFieldStringType, + CustomObjectDraft, FieldDefinition, ResourceTypeId, StateDraft, @@ -85,6 +86,7 @@ class TwoUCustomStates: class TwoUCustomTypes: """Global 2U Custom Type Definitions in Commercetools""" + # 2U Cross System User Information for Customer Objects CUSTOMER_TYPE_DRAFT = TypeDraft( key=TwoUKeys.CROSS_SYS_USER_INFO_TYPE, name=ls({'en': '2U Cross System User Information'}), @@ -116,4 +118,123 @@ class TwoUCustomTypes: ) ] ) - """2U Cross System User Information for Customer Objects""" + + # Custom type for PayPal for Payment Objects + PAYMENT_TYPE_DRAFT = TypeDraft( + key=TwoUKeys.PAYPAL_PAYMENT_TYPE, + name=ls({'en': 'Custom type for PayPal'}), + resource_type_ids=[ResourceTypeId.PAYMENT], + # ^^^ this cannot be updated, the whole type has to be unassigned, removed and replaced. + field_definitions=[ + # Updating Field Definitions only supports: + # - basic field definitions changes, like label and input_hint, not type or + # - whether it is required or not. + # - It can add new ones. + # If you need something done that can't be, the whole type has to be unassigned, removed and replaced. + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_ORDER_ID_FIELD_KEY, + required=False, + label=ls({'en': 'Order ID for PayPal'}), + ), + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_CLIENT_TOKEN_REQUEST_FIELD_KEY, + required=False, + label=ls({'en': 'Client token for PayPal request'}), + ), + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_CLIENT_TOKEN_RESPONSE_FIELD_KEY, + required=False, + label=ls({'en': 'Client token for PayPal response'}), + ), + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_CREATE_ORDER_REQUEST_FIELD_KEY, + required=False, + label=ls({'en': 'Create order for PayPal request'}), + ), + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_CREATE_ORDER_RESPONSE_FIELD_KEY, + required=False, + label=ls({'en': 'Create order for PayPal response'}), + ), + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_CAPTURE_ORDER_REQUEST_FIELD_KEY, + required=False, + label=ls({'en': 'Capture order for PayPal request'}), + ), + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_CAPTURE_ORDER_RESPONSE_FIELD_KEY, + required=False, + label=ls({'en': 'Capture order for PayPal response'}), + ), + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_UPDATE_ORDER_REQUEST_FIELD_KEY, + required=False, + label=ls({'en': 'Update order for PayPal request'}), + ), + FieldDefinition( + type=CustomFieldStringType(), + name=TwoUKeys.PAYPAL_UPDATE_ORDER_RESPONSE_FIELD_KEY, + required=False, + label=ls({'en': 'Update order for PayPal response'}), + ) + ] + ) + + +class TwoUCustomObjects: + PAYPAL_CUSTOM_OBJECT_DRAFT = CustomObjectDraft( + container=TwoUKeys.PAYPAL_CONNECTOR_CONTAINER, + key=TwoUKeys.PAYPAL_CONNECTOR_SETTINGS_KEY, + value={ + "merchantId": "", # string + "email": "test@example.com", # string + "acceptPayPal": True, # boolean + "acceptPayLater": False, # boolean + "acceptVenmo": False, # boolean + "acceptLocal": False, # boolean + "acceptCredit": False, # boolean + "buttonPaymentPage": False, # boolean + "buttonCartPage": False, # boolean + "buttonDetailPage": False, # boolean + "buttonShippingPage": False, # boolean + "buttonShape": "pill", # "rect" | "pill" + "buttonTagline": False, # boolean + "payLaterMessagingType": {}, # Record + "payLaterMessageHomePage": False, # boolean + "payLaterMessageCategoryPage": False, # boolean + "payLaterMessageDetailsPage": False, # boolean + "payLaterMessageCartPage": False, # boolean + "payLaterMessagePaymentPage": False, # boolean + "payLaterMessageTextLogoType": "none", # "inline" | "primary" | "alternative" | "none" + "payLaterMessageTextLogoPosition": "left", # "left" | "right" | "top" + "payLaterMessageTextColor": "black", # "black" | "white" | "monochrome" | "grayscale" + "payLaterMessageTextSize": "10", # "10" | "11" | "12" | "13" | "14" | "15" | "16" + "payLaterMessageTextAlign": "left", # "left" | "center" | "right" + # "blue" | "black" | "white" | "white-no-border" | "gray" | "monochrome" | "grayscale" + "payLaterMessageFlexColor": "blue", + "payLaterMessageFlexRatio": "1x1", # "1x1" | "1x4" | "8x1" | "20x1" + "threeDSOption": "", # "" | "SCA_ALWAYS" | "SCA_WHEN_REQUIRED" + "payPalIntent": "Capture", # "Authorize" | "Capture" + "ratePayBrandName": {}, # CustomDataStringObject + "ratePayLogoUrl": {}, # CustomDataStringObject + "ratePayCustomerServiceInstructions": {}, # CustomDataStringObject + "paymentDescription": {}, # CustomDataStringObject + "storeInVaultOnSuccess": False, # boolean + "paypalButtonConfig": { # PayPal button configuration + "buttonColor": "blue", # "gold" | "blue" | "white" | "silver" | "black" + "buttonLabel": "buynow", # "paypal" | "checkout" | "buynow" | "pay" | "installment" + }, + "hostedFieldsPayButtonClasses": "", # string + "hostedFieldsInputFieldClasses": "", # string + "threeDSAction": {}, # Record + } + + ) diff --git a/commerce_coordinator/apps/commercetools/management/commands/create_paypal_custom_types.py b/commerce_coordinator/apps/commercetools/management/commands/create_paypal_custom_types.py new file mode 100644 index 00000000..2179d09e --- /dev/null +++ b/commerce_coordinator/apps/commercetools/management/commands/create_paypal_custom_types.py @@ -0,0 +1,32 @@ +import json + +from commercetools import CommercetoolsError + +from commerce_coordinator.apps.commercetools.catalog_info.foundational_types import TwoUCustomObjects, TwoUCustomTypes +from commerce_coordinator.apps.commercetools.management.commands._ct_api_client_command import ( + CommercetoolsAPIClientCommand +) + + +class Command(CommercetoolsAPIClientCommand): + help = "Create the Payment Custom Type/Fields for PayPal" + + def handle(self, *args, **options): + current_draft = TwoUCustomTypes.PAYMENT_TYPE_DRAFT + type_key = current_draft.key + custom_object_draft = TwoUCustomObjects.PAYPAL_CUSTOM_OBJECT_DRAFT + + try: + custom_object = self.ct_api_client.base_client.custom_objects.create_or_update(draft=custom_object_draft) + print(f"PayPal custom object already exists: {json.dumps(custom_object.serialize())}") + except CommercetoolsError as ex: + print(f"Error in creating/updating PayPal custom object: {str(ex)}") + + try: + ret = self.ct_api_client.base_client.types.get_by_key(type_key) + print(f"Payment custom type already exists: {json.dumps(ret.serialize())}") + except CommercetoolsError as _: + ret = self.ct_api_client.base_client.types.create( + TwoUCustomTypes.PAYMENT_TYPE_DRAFT + ) + print(f"Created new Payment custom type: {json.dumps(ret.serialize())}")