Class GiftCardService
- Namespace
- Dynamicweb.Ecommerce.GiftCards
- Assembly
- Dynamicweb.Ecommerce.dll
Manages the whole life cycle of gift cards: issuing them when an order containing gift card lines is completed, applying and redeeming them against later orders, and reading their remaining balance.
public class GiftCardService
- Inheritance
-
GiftCardService
- Inherited Members
Remarks
A gift card's balance is not stored on the card. It is derived from the GiftCardTransaction records written against it - one crediting the card when it is issued, and one debiting it for each redemption - so GetGiftCardBalance(GiftCard) is always computed rather than read.
Redemption codes are held encrypted, both on the gift card and on the discount order line that carries a card, which is why lookups by code take a flag saying whether the caller already has the encrypted form.
Redemption is serialized across callers, so two concurrent checkouts cannot both spend the last of the same card.
Methods
CreateGiftCards(Order)
Issues one gift card per purchased gift card on the order.
public virtual void CreateGiftCards(Order order)
Parameters
orderOrderThe order whose gift card lines should be issued.
Remarks
Gift card order lines with a quantity above one are first split into separate lines of quantity one, so that each purchased gift card gets its own card and code. Failures for an individual card are logged and recorded against the order rather than aborting the remaining cards.
Exceptions
- ArgumentNullException
Thrown when
orderisnull.
DecryptCode(string)
Decrypts a gift card code back to the value a customer would recognise.
public virtual string DecryptCode(string input)
Parameters
inputstringThe encrypted code.
Returns
- string
The plain code.
Exceptions
- ArgumentException
Thrown when
inputis null or empty.
DeleteGiftCard(GiftCard)
Deletes a gift card.
public virtual void DeleteGiftCard(GiftCard giftCard)
Parameters
giftCardGiftCardThe gift card to delete.
Exceptions
- ArgumentNullException
Thrown when
giftCardisnull.
DeleteGiftCardTransaction(GiftCardTransaction)
Deletes a gift card transaction, returning the amount it held to the gift card's balance.
public virtual void DeleteGiftCardTransaction(GiftCardTransaction transaction)
Parameters
transactionGiftCardTransactionThe transaction to delete.
Exceptions
- ArgumentNullException
Thrown when
transactionisnull.
EncryptCode(string)
Encrypts a gift card code, as stored on the gift card and on discount order lines.
public virtual string EncryptCode(string input)
Parameters
inputstringThe plain code.
Returns
- string
The encrypted code.
Exceptions
- ArgumentException
Thrown when
inputis null or empty.
GetAllGiftCards()
Gets every gift card in the solution.
public virtual IEnumerable<GiftCard> GetAllGiftCards()
Returns
- IEnumerable<GiftCard>
All gift cards.
GetGiftCardBalance(GiftCard)
Gets the amount still available on a gift card, after every transaction recorded against it.
public virtual double GetGiftCardBalance(GiftCard giftCard)
Parameters
giftCardGiftCardThe gift card.
Returns
- double
The remaining balance, or
0whengiftCardisnull.
GetGiftCardByCode(string, bool)
Gets a gift card by its redemption code.
public virtual GiftCard? GetGiftCardByCode(string giftCardCode, bool encoded)
Parameters
giftCardCodestringThe gift card code.
encodedbooltruewhengiftCardCodeis already encrypted, as it is when read back off an order line;falsewhen it is the plain code a customer entered.
Returns
- GiftCard
The matching gift card, or
nullwhen none exists orgiftCardCodeis null or empty.
GetGiftCardById(string)
Gets a gift card by its identifier.
public virtual GiftCard? GetGiftCardById(string giftCardId)
Parameters
giftCardIdstringThe gift card identifier.
Returns
- GiftCard
The matching gift card, or
nullwhen none exists orgiftCardIdis null or empty.
GetGiftCardTransaction(int)
Gets a gift card transaction by its id.
public virtual GiftCardTransaction? GetGiftCardTransaction(int transactionId)
Parameters
transactionIdintThe transaction id.
Returns
- GiftCardTransaction
The matching transaction, or
nullwhen none exists or the id is not greater than zero.
GetGiftCardsForOrder(string)
Gets the gift cards issued by an order.
public virtual IEnumerable<GiftCard> GetGiftCardsForOrder(string orderId)
Parameters
orderIdstringThe order id.
Returns
- IEnumerable<GiftCard>
The gift cards issued by the order, or an empty sequence when
orderIdis null or empty.
GetGiftCardsForOrderLine(string)
Gets the gift card issued by a single order line.
public virtual GiftCard? GetGiftCardsForOrderLine(string orderLineId)
Parameters
orderLineIdstringThe order line id.
Returns
- GiftCard
The matching gift card, or
nullwhen none exists ororderLineIdis null or empty.
GetTransactionsForGiftCard(string)
Gets every transaction recorded against a gift card, which together determine its remaining balance.
public virtual IEnumerable<GiftCardTransaction> GetTransactionsForGiftCard(string giftCardId)
Parameters
giftCardIdstringThe gift card identifier.
Returns
- IEnumerable<GiftCardTransaction>
The transactions, or an empty sequence when
giftCardIdis null or empty.
SaveGiftCard(GiftCard)
Creates or updates a gift card in the database.
public virtual void SaveGiftCard(GiftCard giftCard)
Parameters
giftCardGiftCardThe gift card to save.
Exceptions
- ArgumentNullException
Thrown when
giftCardisnull.
SaveGiftCardTransaction(GiftCardTransaction)
Creates or updates a gift card transaction, the record that draws an amount down from a gift card.
public virtual void SaveGiftCardTransaction(GiftCardTransaction transaction)
Parameters
transactionGiftCardTransactionThe transaction to save.
Exceptions
- ArgumentNullException
Thrown when
transactionisnull.
SetCardAsExpired(GiftCard)
Expires a gift card immediately by setting its expiry date to now, and saves it.
public virtual void SetCardAsExpired(GiftCard giftCard)
Parameters
giftCardGiftCardThe gift card to expire.
Exceptions
- ArgumentNullException
Thrown when
giftCardisnull.
UseGiftCards(Order)
Redeems the gift cards applied to an order, writing one transaction per gift card discount line.
public virtual void UseGiftCards(Order order)
Parameters
orderOrderThe order to redeem the applied gift cards on.
Remarks
Each gift card is drawn down by at most the remaining order total, and a card is only charged when its balance covers the amount. If any card fails the order is flagged with GiftCardTransactionFailed; if a discount line had to be reduced the order is re-calculated. Redemption is serialized across callers.
Exceptions
- ArgumentNullException
Thrown when
orderisnull.