Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Added payment source validation to payment_method resource. #88

Merged
merged 2 commits into from
Feb 1, 2023
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
11 changes: 6 additions & 5 deletions commercelayer/resource_payment_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ func resourcePaymentMethod() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"payment_source_type": {
Description: "The payment source type, can be one of: 'AdyenPayment', 'BraintreePayment', " +
"'CheckoutComPayment', 'CreditCard', 'ExternalPayment', 'KlarnaPayment', 'PaypalPayment', " +
"'StripePayment', or 'WireTransfer'.",
Type: schema.TypeString,
Required: true,
Description: "The payment source type, can be one of: AdyenPayment, BraintreePayment, " +
"CheckoutComPayment, CreditCard, ExternalPayment, KlarnaPayment, PaypalPayment, " +
"StripePayment or WireTransfer",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: paymentSourceValidation,
},
"currency_code": {
Description: "The international 3-letter currency code as defined by the ISO 4217 standard. " +
Expand Down
24 changes: 24 additions & 0 deletions commercelayer/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,27 @@ var inventoryModelStrategyValidation = func(i interface{}, path cty.Path) diag.D
return diag.Errorf("Invalid inventory model strategy provided: %s. Must be one of %s",
i.(string), strings.Join(getInventoryModelStrategies(), ", "))
}

func getPaymentSources() []string {
return []string{
"AdyenPayment",
"BraintreePayment",
"CheckoutComPayment",
"CreditCard",
"ExternalPayment",
"KlarnaPayment",
"PaypalPayment",
"StripePayment",
"WireTransfer",
}
}

var paymentSourceValidation = func(i interface{}, path cty.Path) diag.Diagnostics {
demeyerthom marked this conversation as resolved.
Show resolved Hide resolved
for _, s := range getPaymentSources() {
if s == i.(string) {
return nil
}
}
return diag.Errorf("Invalid payment source provided: %s. Must be one of %s",
i.(string), strings.Join(getPaymentSources(), ", "))
}
10 changes: 10 additions & 0 deletions commercelayer/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ func TestCurrencyCodeValidationOK(t *testing.T) {
diag := currencyCodeValidation("EUR", nil)
assert.False(t, diag.HasError())
}

func TestPaymentSourceValidationError(t *testing.T) {
diag := paymentSourceValidation("Adyen", nil)
assert.True(t, diag.HasError())
}

func TestPaymentSourceValidationOK(t *testing.T) {
diag := paymentSourceValidation("BraintreePayment", nil)
assert.False(t, diag.HasError())
}