Partager via


How to Iterate through Multiple Types of Payment

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

You can configure a Commerce Server site to accept multiple types of payment. These include credit cards, cash cards, gift certificates, and purchase orders. You can also implement custom payment types.

However, iterating through payments involves one complication. Suppose that you want to display all payments to the shopper and ask for confirmation before processing the payments. Some properties of the payment that you might want to display are properties of the sub-class, not properties of the Payment class itself. When you iterate through multiple payment objects, use a switch statement to compare the PaymentType property of the payment object to the members of the PaymentMethodTypes enumeration. Then cast the object to the appropriate sub-class.

To iterate through multiple types of payment

  1. Get the PaymentCollection object from the Payments property of the OrderForm object.

  2. Iterate through the Payment objects in the PaymentCollection object.

  3. Use a switch statement to take a different action based on the value of the PaymentType attribute of the Payment object.

  4. Add a case statement for each type of payment that your site supports. The values of the case expressions will be properties of the PaymentMethodTypes enumeration.

    Note

    The Custom, Custom1, Custom2, Custom3, Custom4, and Custom5 properties of the PaymentMethodTypes enumeration are used for custom payment types that you implement. For more information about how to create and use custom payment types, see Creating a Custom Payment Method.

  5. Within the branch for a given payment type, cast the Payment object into an object of the appropriate Payment sub-class.

  6. Access the methods and properties of the object as needed.

Example

The following example displays a brief description of each payment that is associated with a Basket object.

using System;
using System.Collections;
using System.Data;
using System.Web;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Orders;
using Microsoft.CommerceServer.Orders;

void DisplayPayments(Guid userID)
{
    Basket myBasket = OrderContext.Current.GetBasket(userID);

    foreach (OrderForm orderForm in myBasket.OrderForms)
    {
        foreach (Payment pmt in orderForm.Payments)
        {
            switch (pmt.PaymentType)
            {
                case PaymentMethodTypes.CashCard:
                    CashCardPayment cashCardPayment = (CashCardPayment)pmt;
                    Response.Write("$" + cashCardPayment.Amount.ToString() 
                        + " paid with cash card number " 
                        + cashCardPayment.CashCardNumber.ToString() + ".<br>");
                    break;
                case PaymentMethodTypes.CreditCard:
                    CreditCardPayment creditCardPayment = (CreditCardPayment)pmt;
                    Response.Write("$" + creditCardPayment.Amount.ToString() 
                        + " paid with " + creditCardPayment.CardType + " number ************" 
                        + creditCardPayment.CreditCardIdentifier + ".<br>");
                    break;
                case PaymentMethodTypes.PurchaseOrder:
                    PurchaseOrderPayment purchaseOrderPayment = (PurchaseOrderPayment)pmt;
                    Response.Write("$" + purchaseOrderPayment.Amount.ToString() 
                        + " paid with purchase order number " 
                        + purchaseOrderPayment.PurchaseOrderPaymentNumber.ToString() + ".<br>");
                    break;
                case PaymentMethodTypes.GiftCertificate:
                    GiftCertificatePayment giftCertificatePayment = (GiftCertificatePayment)pmt;
                    Response.Write("$" + giftCertificatePayment.Amount.ToString() 
                        + " paid with gift certificate number " 
                        + giftCertificatePayment.GiftCertificateNumber.ToString() + ".<br>");
                    break;
                default:
                    Response.Write("Unknown payment type " + pmt.GetType() + ".<br>");
                    break;
            }
        }
    }
}

To use this sample you must call the DisplayPayments method with the user ID.

Compiling the Code

To build a Commerce Server Web project that includes the sample code, add references to the following assemblies:

  • Microsoft.CommerceServer.Runtime

  • Microsoft.CommerceServer.Orders.CrossTierTypes

See Also

Other Resources

Creating a Custom Payment Method

Working with Baskets