Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [SONIC-650] Added PayPal custom attributes management command #278

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions commerce_coordinator/apps/commercetools/catalog_info/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from commercetools.platform.models import (
CustomFieldStringType,
CustomObjectDraft,
FieldDefinition,
ResourceTypeId,
StateDraft,
Expand Down Expand Up @@ -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'}),
Expand Down Expand Up @@ -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": "[email protected]", # 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<string, "flex" | "text">
"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<string, any>
}

)
Original file line number Diff line number Diff line change
@@ -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())}")
shafqatfarhan marked this conversation as resolved.
Show resolved Hide resolved
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)
shafqatfarhan marked this conversation as resolved.
Show resolved Hide resolved
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())}")
Loading