flamingo.me/flamingo-commerce/v3@v3.11.0/cart/domain/placeorder/placeorder.go (about)

     1  package placeorder
     2  
     3  import (
     4  	"context"
     5  
     6  	"flamingo.me/flamingo/v3/core/auth"
     7  
     8  	"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
     9  	price "flamingo.me/flamingo-commerce/v3/price/domain"
    10  )
    11  
    12  type (
    13  	// Service  interface - Secondary PORT
    14  	Service interface {
    15  		PlaceGuestCart(ctx context.Context, cart *cart.Cart, payment *Payment) (PlacedOrderInfos, error)
    16  		PlaceCustomerCart(ctx context.Context, identity auth.Identity, cart *cart.Cart, payment *Payment) (PlacedOrderInfos, error)
    17  		ReserveOrderID(ctx context.Context, cart *cart.Cart) (string, error)
    18  		// CancelGuestOrder cancels a previously placed guest order and returns the used cart
    19  		CancelGuestOrder(ctx context.Context, orderInfos PlacedOrderInfos) error
    20  		// CancelCustomerOrder cancels a previously placed guest order and returns the used cart
    21  		CancelCustomerOrder(ctx context.Context, orderInfos PlacedOrderInfos, identity auth.Identity) error
    22  	}
    23  	// Payment represents all payments done for the cart and which items have been purchased by what method
    24  	Payment struct {
    25  		// The name of the Gateway that has returned the Payment for the cart
    26  		Gateway string
    27  		// Transactions is the list of individual transactions -  most cases only one Transaction might be part of the payment
    28  		Transactions []Transaction
    29  		// RawTransactionData can be used to store any additional stuff (specific for Gateway)
    30  		RawTransactionData interface{}
    31  		// PaymentID is a optional reference of the Payment (that contains the Transactions)
    32  		PaymentID string
    33  	}
    34  
    35  	// Transaction representing the transaction
    36  	Transaction struct {
    37  		// PaymentProvider - optional - the underling processor of this transaction (e.g. "paymark")
    38  		PaymentProvider string
    39  		// Method like "paymark_cc" , "paypal",
    40  		Method string
    41  		// Status - Method specific status e.g. Auth, Captured, Open, ...
    42  		Status string
    43  		// TransactionID - The main reference of the payment that was done
    44  		TransactionID string
    45  		// AdditionalData - room for AdditionalData - specific to the payment
    46  		AdditionalData map[string]string
    47  		// AmountPayed the amount that have been paid - eventually in a different currency
    48  		AmountPayed price.Price
    49  		// ValuedPayed the value of the AmountPayed in the cart default currency
    50  		ValuedAmountPayed price.Price
    51  		// CreditCardInfo Optional
    52  		CreditCardInfo *CreditCardInfo
    53  		// Title - speaking title - optional may describe the payment and may be shown to the customer
    54  		Title string
    55  		// RawTransactionData - place to store any additional stuff (specific for Gateway)
    56  		RawTransactionData interface{}
    57  		// ChargeAssignments - optional the assignment of this transaction to charges - this might be required for payments that are really only done for a certain item
    58  		ChargeByItem *ChargeByItem
    59  	}
    60  
    61  	// ChargeByItem - the Charge that is paid for the individual items
    62  	ChargeByItem struct {
    63  		cartItems     map[string]price.Charge
    64  		shippingItems map[string]price.Charge
    65  		totalItems    map[string]price.Charge
    66  	}
    67  
    68  	// CreditCardInfo contains the necessary data
    69  	CreditCardInfo struct {
    70  		AnonymizedCardNumber string
    71  		Type                 string
    72  		CardHolder           string
    73  		Expire               string
    74  	}
    75  
    76  	// PlacedOrderInfos represents a slice of PlacedOrderInfo
    77  	PlacedOrderInfos []PlacedOrderInfo
    78  
    79  	// PlacedOrderInfo defines the additional info struct for placed orders
    80  	PlacedOrderInfo struct {
    81  		OrderNumber  string
    82  		DeliveryCode string
    83  	}
    84  )
    85  
    86  const (
    87  	// PaymentStatusCaptured a payment which has been captured
    88  	PaymentStatusCaptured = "CAPTURED"
    89  	// PaymentStatusAuthorized a payment which has been AUTHORIZED
    90  	PaymentStatusAuthorized = "AUTHORIZED"
    91  	// PaymentStatusOpen payment is still open
    92  	PaymentStatusOpen = "OPEN"
    93  )
    94  
    95  // AddTransaction for a paymentInfo with items
    96  func (cp *Payment) AddTransaction(transaction Transaction) {
    97  	cp.Transactions = append(cp.Transactions, transaction)
    98  }
    99  
   100  // TotalValue returns the Total Valued Price
   101  func (cp *Payment) TotalValue() (price.Price, error) {
   102  	var prices []price.Price
   103  
   104  	for _, transaction := range cp.Transactions {
   105  		prices = append(prices, transaction.ValuedAmountPayed)
   106  	}
   107  
   108  	return price.SumAll(prices...)
   109  }
   110  
   111  // GetOrderNumberForDeliveryCode returns the order number for a delivery code
   112  func (poi PlacedOrderInfos) GetOrderNumberForDeliveryCode(deliveryCode string) string {
   113  	for _, v := range poi {
   114  		if v.DeliveryCode == deliveryCode {
   115  			return v.OrderNumber
   116  		}
   117  	}
   118  
   119  	return ""
   120  }
   121  
   122  // CartItems return CartItems
   123  func (c ChargeByItem) CartItems() map[string]price.Charge {
   124  	return c.cartItems
   125  }
   126  
   127  // ChargeForCartItem returns Charge for a cart item by id
   128  func (c ChargeByItem) ChargeForCartItem(itemid string) (*price.Charge, bool) {
   129  	if charge, ok := c.cartItems[itemid]; ok {
   130  		return &charge, true
   131  	}
   132  	return nil, false
   133  }
   134  
   135  // ChargeForDeliveryCode returns Charge for a shipping item by delivery code
   136  func (c ChargeByItem) ChargeForDeliveryCode(itemid string) (*price.Charge, bool) {
   137  	if charge, ok := c.shippingItems[itemid]; ok {
   138  		return &charge, true
   139  	}
   140  	return nil, false
   141  }
   142  
   143  // ChargeForTotal returns Charge for a total item by code
   144  func (c ChargeByItem) ChargeForTotal(itemid string) (*price.Charge, bool) {
   145  	if charge, ok := c.totalItems[itemid]; ok {
   146  		return &charge, true
   147  	}
   148  	return nil, false
   149  }
   150  
   151  // TotalItems returns totalItems
   152  func (c ChargeByItem) TotalItems() map[string]price.Charge {
   153  	return c.totalItems
   154  }
   155  
   156  // ShippingItems returns ShippingItems
   157  func (c ChargeByItem) ShippingItems() map[string]price.Charge {
   158  	return c.shippingItems
   159  }
   160  
   161  // AddCartItem modifies the current instance and adds a charge for a cart item
   162  func (c ChargeByItem) AddCartItem(id string, charge price.Charge) ChargeByItem {
   163  	if c.cartItems == nil {
   164  		c.cartItems = make(map[string]price.Charge)
   165  	}
   166  	c.cartItems[id] = charge
   167  	return c
   168  }
   169  
   170  // AddTotalItem modifies the current instance and adds a charge for a total item
   171  func (c ChargeByItem) AddTotalItem(id string, charge price.Charge) ChargeByItem {
   172  	if c.totalItems == nil {
   173  		c.totalItems = make(map[string]price.Charge)
   174  	}
   175  	c.totalItems[id] = charge
   176  	return c
   177  }
   178  
   179  // AddShippingItems modifies the current instance and adds a charge for a shipping item
   180  func (c ChargeByItem) AddShippingItems(id string, charge price.Charge) ChargeByItem {
   181  	if c.shippingItems == nil {
   182  		c.shippingItems = make(map[string]price.Charge)
   183  	}
   184  	c.shippingItems[id] = charge
   185  	return c
   186  }