Skip to content

Commit

Permalink
Added payment source validation to payment_method resource. (incentro…
Browse files Browse the repository at this point in the history
…-ecx#88)

* Added payment source validation to payment_method resource.

* Added unit tests for payment source validation.
  • Loading branch information
incentrotolgaakyazi authored Feb 1, 2023
1 parent 29d37f6 commit 132c128
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
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 {
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())
}

0 comments on commit 132c128

Please sign in to comment.