github.com/seeker-insurance/kit@v0.0.13/payments/stripe/client.go (about)

     1  package stripe
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/spf13/viper"
     8  	stripe "github.com/stripe/stripe-go"
     9  	"github.com/stripe/stripe-go/card"
    10  	"github.com/stripe/stripe-go/charge"
    11  	"github.com/stripe/stripe-go/customer"
    12  )
    13  
    14  // Client stripe client data structure
    15  type Client struct {
    16  	Parent Parent
    17  }
    18  
    19  // PaymentSource payment source model interface
    20  type PaymentSource interface {
    21  	SetSource(string, interface{}) error
    22  }
    23  
    24  // ChargeData charge data structure
    25  type ChargeData struct {
    26  	SourceID    *string
    27  	Amount      float64
    28  	Description string
    29  	Currency    string
    30  }
    31  
    32  // Setup init stripe setup
    33  func Setup(key string) error {
    34  	if key == "" {
    35  		return errors.New("Stripe secret key is not set")
    36  	}
    37  	stripe.Key = key
    38  	return nil
    39  }
    40  
    41  // NewClient init new client
    42  func NewClient(parent Parent) (*Client, error) {
    43  	if err := Setup(viper.GetString("stripe_secret_key")); err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return &Client{Parent: parent}, nil
    48  }
    49  
    50  // FetchCustomer find customer record
    51  func (i *Client) FetchCustomer() (*stripe.Customer, error) {
    52  	customerID := i.Parent.GetStripeCustomerID()
    53  	if customerID == nil {
    54  		return nil, nil
    55  	}
    56  	c, err := customer.Get(*customerID, nil)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return c, nil
    62  }
    63  
    64  // EnsureCustomer find or create a customer record
    65  func (i *Client) EnsureCustomer() error {
    66  	c, err := i.FetchCustomer()
    67  	if err != nil || c != nil {
    68  		return err
    69  	}
    70  
    71  	params := &stripe.CustomerParams{
    72  		Description: i.Parent.StripeCustomerDescription(),
    73  	}
    74  
    75  	c, err = customer.New(params)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	customerID := c.ID
    80  
    81  	return i.Parent.SetStripeCustomerID(&customerID)
    82  }
    83  
    84  // CreatePaymentSource create payment source
    85  func (i *Client) CreatePaymentSource(paymentType string, token string, ps PaymentSource) error {
    86  	if paymentType == "credit_card" {
    87  		return i.CreateCreditCard(&token, ps)
    88  	}
    89  
    90  	return fmt.Errorf("Payment type '%s' is not supported", paymentType)
    91  }
    92  
    93  // CreateCreditCard create credit card payment source
    94  func (i *Client) CreateCreditCard(token *string, ps PaymentSource) error {
    95  	c, err := card.New(&stripe.CardParams{
    96  		Customer: i.Parent.GetStripeCustomerID(),
    97  		Token:    token,
    98  	})
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	return ps.SetSource(c.ID, c)
   104  }
   105  
   106  // Charge charge provided source with charge params
   107  func (i *Client) Charge(data *ChargeData) (*stripe.Charge, error) {
   108  	customerID := i.Parent.GetStripeCustomerID()
   109  	if customerID == nil {
   110  		return nil, errors.New("Stripe customer ID is missing")
   111  	}
   112  	if data.SourceID == nil {
   113  		return nil, errors.New("Stripe source ID is missing")
   114  	}
   115  	chargeParams := &stripe.ChargeParams{
   116  		Customer:    customerID,
   117  		Amount:      stripe.Int64(int64(data.Amount * 100)), // has to be in cents
   118  		Description: stripe.String(data.Description),
   119  	}
   120  	if data.Currency == "" {
   121  		chargeParams.Currency = stripe.String(string(stripe.CurrencyUSD))
   122  	} else {
   123  		chargeParams.Currency = stripe.String(data.Currency)
   124  	}
   125  	chargeParams.SetSource(*(data.SourceID))
   126  
   127  	return charge.New(chargeParams)
   128  }