diff options
Diffstat (limited to 'client_options.go')
| -rw-r--r-- | client_options.go | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/client_options.go b/client_options.go index b365ac0..fa6ad1c 100644 --- a/client_options.go +++ b/client_options.go @@ -18,30 +18,12 @@ type ClientOptions struct { // The hostname extracted from ServerUrl. host string - // The merchant code is the project code obtained from the Duitku - // merchant page. - // This code is useful as an identifier of your project in each - // request using the /merchant/* APIs. - // You can get this code on every project you register on the - // [merchant portal]. - // - // [merchant portal]: https://passport.duitku.com/merchant/Project - MerchantCode string `ini:"duitku::merchant_code"` + // DefaultMerchant default merchant to be used for payment. + DefaultMerchant Merchant `ini:"default-merchant"` - // MerchantApiKey The API key for signing merchant related request. - MerchantApiKey string `ini:"duitku::merchant_api_key"` - - // MerchantCallbackUrl The URL that will be used by Duitku to - // confirm payments made by your customers. - MerchantCallbackUrl string `ini:"duitku::merchant_callback_url"` - - // MerchantReturnUrl The URL that Duitku will direct the customer - // after the transaction is successful or canceled. - MerchantReturnUrl string `ini:"duitku::merchant_return_url"` - - // Merchant code and API key for payment through Indomaret. - IndomaretMerchantCode string `ini:"duitku::indomaret_merchant_code"` - IndomaretApiKey string `ini:"duitku::indomaret_api_key"` + // PaymentMerchant specific merchant to be used based on payment + // method. + PaymentMerchant map[string]Merchant `ini:"merchant"` // DisburseApiKey API key for signing disbursement request. DisburseApiKey string `ini:"duitku::disburse_api_key"` @@ -74,8 +56,23 @@ func LoadClientOptions(file string) (opts *ClientOptions, err error) { return opts, nil } -// validate each field values. -func (opts *ClientOptions) validate() (err error) { +// Merchant return the PaymentMerchant based on paymentMethod. +// If no key found, it will return DefaultMerchant. +func (opts *ClientOptions) Merchant(paymentMethod string) (merchant Merchant) { + var ( + found bool + ) + + merchant, found = opts.PaymentMerchant[paymentMethod] + if !found { + merchant = opts.DefaultMerchant + } + + return merchant +} + +// initAndValidate each field values. +func (opts *ClientOptions) initAndValidate() (err error) { var ( urlServer *url.URL ) @@ -96,5 +93,19 @@ func (opts *ClientOptions) validate() (err error) { return fmt.Errorf(`invalid or empty DisburseApiKey: %s`, opts.DisburseApiKey) } + var ( + method string + merchant Merchant + ) + for method, merchant = range opts.PaymentMerchant { + if len(merchant.CallbackUrl) == 0 { + merchant.CallbackUrl = opts.DefaultMerchant.CallbackUrl + } + if len(merchant.ReturnUrl) == 0 { + merchant.ReturnUrl = opts.DefaultMerchant.ReturnUrl + } + opts.PaymentMerchant[method] = merchant + } + return nil } |
