Skip to content

Commit

Permalink
Merge pull request #67 from glendaesutanto/story/API-2362/direct-debi…
Browse files Browse the repository at this point in the history
…t-api

Direct Debit API
  • Loading branch information
ErvanAdetya authored Aug 4, 2021
2 parents 39407cc + 25da1f9 commit 462238b
Show file tree
Hide file tree
Showing 19 changed files with 2,746 additions and 1 deletion.
140 changes: 140 additions & 0 deletions directdebit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package xendit

// ChannelCodeEnum constants are the available channel code
type ChannelCodeEnum string

// AccountType constants are the available account type
type AccountTypeEnum string

// This consists the values that ChannelCodeEnum can take
const (
DC_BRI ChannelCodeEnum = "DC_BRI"
BA_BPI ChannelCodeEnum = "BA_BPI"
BA_UBP ChannelCodeEnum = "BA_UBP"
)

// This consists the values that AccountTypeEnum can take
const (
DEBIT_CARD AccountTypeEnum = "DEBIT_CARD"
BANK_ACCOUNT AccountTypeEnum = "BANK_ACCOUNT"
)

// InitializedLinkedAccount contains data from Xendit's API response of initialize linked account related requests.
// For more details see https://xendit.github.io/apireference/?bash#initialize-linked-account-tokenization.
// For documentation of subpackage linked account, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebit/linkedaccount/
type InitializedLinkedAccount struct {
ID string `json:"id"`
CustomerID string `json:"customer_id"`
ChannelCode ChannelCodeEnum `json:"channel_code"`
AuthorizerURL string `json:"authorizer_url,omitempty"`
Status string `json:"status,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

// ValidatedLinkedAccount contains data from Xendit's API response of validate linked account related requests.
// For more details see https://xendit.github.io/apireference/?bash#validate-otp-for-linked-account-token.
// For documentation of subpackage linked account, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebit/linkedaccount/
type ValidatedLinkedAccount struct {
ID string `json:"id"`
CustomerID string `json:"customer_id"`
ChannelCode ChannelCodeEnum `json:"channel_code"`
Status string `json:"status"`
}

// AccessibleLinkedAccount contains data from Xendit's API response of get accessible linked account related requests.
// For more details see https://xendit.github.io/apireference/?bash#retrieve-accessible-accounts-by-linked-account-token.
// For documentation of subpackage linked account, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebit/linkedaccount/
type AccessibleLinkedAccount struct {
ID string `json:"id"`
ChannelCode ChannelCodeEnum `json:"channel_code"`
AccountType AccountTypeEnum `json:"type"`
Properties map[string]interface{} `json:"properties"`
}

// UnbindedLinkedAccount contains data from Xendit's API response of unbind linked account token related requests.
// For more details see https://xendit.github.io/apireference/?bash#unbind-a-linked-account-token.
// For documentation of subpackage linked account, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebit/linkedaccount/
type UnbindedLinkedAccount struct {
ID string `json:"id"`
IsDeleted bool `json:"is_deleted"`
}

// PaymentMethod contains data from Xendit's API response of payment method related requests.
// For more details see https://xendit.github.io/apireference/?bash#create-payment-method.
// For documentation of subpackage payment method, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebit/paymentmethod/
type PaymentMethod struct {
ID string `json:"id"`
Type AccountTypeEnum `json:"type"`
Properties map[string]interface{} `json:"properties"`
CustomerID string `json:"customer_id"`
Status string `json:"status"`
Created string `json:"created"`
Updated string `json:"updated"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

// DirectDebitBasketItem contains data from Xendit's API response of direct debit's basket requests.
// For more details see https://xendit.github.io/apireference/?bash#create-direct-debit-payment.
// For documentation of subpackage direct debit payment, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebitpayment/
type DirectDebitBasketItem struct {
ReferenceID string `json:"reference_id"`
Name string `json:"name"`
Market string `json:"market"`
Type string `json:"type"`
Description string `json:"description,omitempty"`
Category string `json:"category,omitempty"`
SubCategory string `json:"sub_category,omitempty"`
Price float64 `json:"price,omitempty"`
URL string `json:"url,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Quantity int `json:"quantity,omitempty"`
}

// DirectDebitDevice contains data from Xendit's API response of direct debit's device requests.
// For more details see https://xendit.github.io/apireference/?bash#create-direct-debit-payment.
// For documentation of subpackage direct debit payment, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebitpayment/
type DirectDebitDevice struct {
ID string `json:"id"`
IPAddress string `json:"ip_address"`
UserAgent string `json:"user_agent"`
ADID string `json:"ad_id,omitempty"`
Imei string `json:"imei,omitempty"`
}

// DirectDebitRefunds contains data from Xendit's API response of direct debit's refunds requests.
// For more details see https://xendit.github.io/apireference/?bash#create-direct-debit-payment.
// For documentation of subpackage direct debit payment, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebitpayment/
type DirectDebitRefunds struct {
Data []string `json:"data"`
HasMore bool `json:"has_more"`
URL string `json:"url"`
}

// DirectDebitPayment contains data from Xendit's API response of direct debit payment requests.
// For more details see https://xendit.github.io/apireference/?bash#create-direct-debit-payment.
// For documentation of subpackage direct debit payment, checkout https://pkg.go.dev/github.com/xendit/xendit-go/directdebitpayment/
type DirectDebitPayment struct {
ID string `json:"id"`
ReferenceID string `json:"reference_id"`
ChannelCode ChannelCodeEnum `json:"channel_code"`
PaymentMethodID string `json:"payment_method_id"`
Currency string `json:"currency"`
Amount float64 `json:"amount"`
Description string `json:"description"`
Status string `json:"status"`
FailureCode string `json:"failure_code"`
IsOTPRequired bool `json:"is_otp_required"`
OTPMobileNumber string `json:"otp_mobile_number"`
OTPExpirationTimestamp string `json:"otp_expiration_timestamp"`
Created string `json:"created"`
Updated string `json:"updated"`
Basket []DirectDebitBasketItem `json:"basket"`
Metadata map[string]interface{} `json:"metadata"`
Device DirectDebitDevice `json:"device"`
RefundedAmount float64 `json:"refunded_amount"`
Refunds DirectDebitRefunds `json:"refunds"`
SuccessRedirectURL string `json:"success_redirect_url"`
CheckoutURL string `json:"checkout_url"`
FailureRedirectURL string `json:"failure_redirect_url"`
RequiredAction string `json:"required_action"`
}
198 changes: 198 additions & 0 deletions directdebit/directdebitpayment/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package directdebitpayment

import (
"context"
"fmt"
"net/http"

"github.com/xendit/xendit-go"
"github.com/xendit/xendit-go/utils/validator"
)

// Client is the client used to invoke direct debit (linked account) API.
type Client struct {
Opt *xendit.Option
APIRequester xendit.APIRequester
}

type DirectDebitPaymentResponse struct {
ID string `json:"id"`
ReferenceID string `json:"reference_id"`
ChannelCode xendit.ChannelCodeEnum `json:"channel_code"`
PaymentMethodID string `json:"payment_method_id"`
Currency string `json:"currency"`
Amount float64 `json:"amount"`
Description string `json:"description"`
Status string `json:"status"`
FailureCode string `json:"failure_code"`
IsOTPRequired bool `json:"is_otp_required"`
OTPMobileNumber string `json:"otp_mobile_number"`
OTPExpirationTimestamp string `json:"otp_expiration_timestamp"`
Created string `json:"created"`
Updated string `json:"updated"`
Basket []xendit.DirectDebitBasketItem `json:"basket"`
Metadata map[string]interface{} `json:"metadata"`
Device xendit.DirectDebitDevice `json:"device"`
RefundedAmount float64 `json:"refunded_amount"`
Refunds xendit.DirectDebitRefunds `json:"refunds"`
SuccessRedirectURL string `json:"success_redirect_url"`
CheckoutURL string `json:"checkout_url"`
FailureRedirectURL string `json:"failure_redirect_url"`
RequiredAction string `json:"required_action"`
}

func (r *DirectDebitPaymentResponse) toDirectDebitPaymentResponse() *xendit.DirectDebitPayment {
return &xendit.DirectDebitPayment{
ID: r.ID,
ReferenceID: r.ReferenceID,
ChannelCode: r.ChannelCode,
PaymentMethodID: r.PaymentMethodID,
Currency: r.Currency,
Amount: r.Amount,
Description: r.Description,
Status: r.Status,
FailureCode: r.FailureCode,
IsOTPRequired: r.IsOTPRequired,
OTPMobileNumber: r.OTPMobileNumber,
OTPExpirationTimestamp: r.OTPExpirationTimestamp,
Created: r.Created,
Updated: r.Updated,
Basket: r.Basket,
Metadata: r.Metadata,
Device: r.Device,
RefundedAmount: r.RefundedAmount,
Refunds: r.Refunds,
SuccessRedirectURL: r.SuccessRedirectURL,
CheckoutURL: r.CheckoutURL,
FailureRedirectURL: r.FailureRedirectURL,
RequiredAction: r.RequiredAction,
}
}

// CreateDirectDebitPayment created new direct debit payment
func (c *Client) CreateDirectDebitPayment(data *CreateDirectDebitPaymentParams) (*xendit.DirectDebitPayment, *xendit.Error) {
return c.CreateDirectDebitPaymentWithContext(context.Background(), data)
}

// CreateDirectDebitPaymentWithContext created new direct debit payment
func (c *Client) CreateDirectDebitPaymentWithContext(ctx context.Context, data *CreateDirectDebitPaymentParams) (*xendit.DirectDebitPayment, *xendit.Error) {
if err := validator.ValidateRequired(ctx, data); err != nil {
return nil, validator.APIValidatorErr(err)
}

response := &xendit.DirectDebitPayment{}
header := &http.Header{}

if data.ForUserID != "" {
header.Add("for-user-id", data.ForUserID)
}

header.Add("Idempotency-key", data.IdempotencyKey)

err := c.APIRequester.Call(
ctx,
"POST",
fmt.Sprintf("%s/direct_debits", c.Opt.XenditURL),
c.Opt.SecretKey,
header,
data,
response,
)
if err != nil {
return nil, err
}

return response, nil
}

// ValidateOTPForDirectDebitPayment validate OTP for direct debit payment
func (c *Client) ValidateOTPForDirectDebitPayment(data *ValidateOTPForDirectDebitPaymentParams) (*xendit.DirectDebitPayment, *xendit.Error) {
return c.ValidateOTPForDirectDebitPaymentWithContext(context.Background(), data)
}

// ValidateOTPForDirectDebitPayment validate OTP for direct debit payment
func (c *Client) ValidateOTPForDirectDebitPaymentWithContext(ctx context.Context, data *ValidateOTPForDirectDebitPaymentParams) (*xendit.DirectDebitPayment, *xendit.Error) {
if err := validator.ValidateRequired(ctx, data); err != nil {
return nil, validator.APIValidatorErr(err)
}

response := &xendit.DirectDebitPayment{}
header := &http.Header{}

if data.ForUserID != "" {
header.Add("for-user-id", data.ForUserID)
}

err := c.APIRequester.Call(
ctx,
"POST",
fmt.Sprintf("%s/direct_debits/%s/validate_otp/", c.Opt.XenditURL, data.DirectDebitID),
c.Opt.SecretKey,
header,
data,
response,
)
if err != nil {
return nil, err
}

return response, nil
}

// GetDirectDebitPaymentStatusByID gets direct debit payment status by ID
func (c *Client) GetDirectDebitPaymentStatusByID(data *GetDirectDebitPaymentStatusByIDParams) (*xendit.DirectDebitPayment, *xendit.Error) {
return c.GetDirectDebitPaymentStatusByIDWithContext(context.Background(), data)
}

// GetDirectDebitPaymentStatusByIDWithContext gets direct debit payment status by ID
func (c *Client) GetDirectDebitPaymentStatusByIDWithContext(ctx context.Context, data *GetDirectDebitPaymentStatusByIDParams) (*xendit.DirectDebitPayment, *xendit.Error) {
if err := validator.ValidateRequired(ctx, data); err != nil {
return nil, validator.APIValidatorErr(err)
}
tempResponse := &DirectDebitPaymentResponse{}

err := c.APIRequester.Call(
ctx,
"GET",
fmt.Sprintf("%s/direct_debits/%s/", c.Opt.XenditURL, data.ID),
c.Opt.SecretKey,
nil,
nil,
tempResponse,
)
if err != nil {
return nil, err
}

response := tempResponse.toDirectDebitPaymentResponse()

return response, nil
}

// GetDirectDebitPaymentStatusByReferenceID gets direct debit payment status by reference ID
func (c *Client) GetDirectDebitPaymentStatusByReferenceID(data *GetDirectDebitPaymentStatusByReferenceIDParams) ([]xendit.DirectDebitPayment, *xendit.Error) {
return c.GetDirectDebitPaymentStatusByReferenceIDWithContext(context.Background(), data)
}

// GetDirectDebitPaymentStatusByReferenceIDWithContext gets direct debit payment status by reference ID
func (c *Client) GetDirectDebitPaymentStatusByReferenceIDWithContext(ctx context.Context, data *GetDirectDebitPaymentStatusByReferenceIDParams) ([]xendit.DirectDebitPayment, *xendit.Error) {
if err := validator.ValidateRequired(ctx, data); err != nil {
return nil, validator.APIValidatorErr(err)
}
response := []xendit.DirectDebitPayment{}

err := c.APIRequester.Call(
ctx,
"GET",
fmt.Sprintf("%s/direct_debits?%s", c.Opt.XenditURL, data.QueryString()),
c.Opt.SecretKey,
nil,
nil,
&response,
)
if err != nil {
return nil, err
}

return response, nil
}
Loading

0 comments on commit 462238b

Please sign in to comment.