How to Add a Payment to an OrderForm

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

A Payment represents a method that the customer will use to pay for the items in a Basket. Because the customer could pay by using more than one payment method—for example, the customer could pay for half of the total cost by using a gift certificate, and pay for the other half by using a credit card—Payments are contained in a PaymentCollection. The PaymentCollection is contained indirectly in a Basket.

To add a Payment object to an OrderForm object

  1. In Visual Studio, create a new Commerce Server Web application.

  2. Add the following references:

    • Microsoft.CommerceServer.Runtime

    • Microsoft.CommerceServer.Runtime.Orders

  3. Add using directives for the following namespaces:

  4. Create or retrieve a Basket object.

  5. Create or retrieve an OrderForm object.

  6. Get the valid payment methods from the OrderContext instance.

  7. Extract each payment method from the dataset that contains the valid payment methods.

  8. Ask the customer how to pay for the purchase.

  9. Create a new Payment object for each way that the customer will pay for the purchase (a GiftCertificatePayment, CreditCardPayment, CashCardPayment, or PurchaseOrderPayment) and add each Payment to the PaymentCollection object in the OrderForm.

  10. Save the updated Basket.

Example

The following code example shows how to add a CreditCardPayment to a Basket.

This example builds on the example in the topic How to Add an OrderForm to a Basket.

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

public partial class Default_aspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // For this example, create a new GUID for the user's ID.
        // If this were part of a full Commerce Server site that
        // implemented logons, you would get the ID of the current
        // user from the CommerceContext object.

        Guid userID = Guid.NewGuid();

        // Create a Basket object and an OrderForm object.

        Basket myBasket = OrderContext.Current.GetBasket(userID, "primary");
        OrderForm orderForm = new OrderForm();

        // Get the valid payment methods.

        OrderContext orderContext = CommerceContext.Current.OrderSystem;
        DataSet validPaymentMethods = orderContext.GetPaymentMethods("en-US");

        // Create a PaymentCollection and two CreditCardPayments.
        // The first payment is for $10.00 and uses one payment
        // method. The second payment is for $25.00 and uses a
        // different payment method.

        PaymentCollection payments = new PaymentCollection(orderForm);
        DataRowCollection paymentMethodRows = validPaymentMethods.Tables[0].Rows;
        try
        {
            Guid paymentMethodId1 = (Guid)(paymentMethodRows[0]["PaymentMethodId"]);
            Guid paymentMethodId2 = (Guid)(paymentMethodRows[1]["PaymentMethodId"]);

            // In a full application, you would get the AddressId
            // from the user's profile. For this example, just
            // create a new GUID for the AddressId.

            string addressId1 = new Guid().ToString();
            string addressId2 = new Guid().ToString();

            CreditCardPayment payment1 = new CreditCardPayment(addressId1, paymentMethodId1);
            CreditCardPayment payment2 = new CreditCardPayment(addressId2, paymentMethodId2);
            payment1.Amount = 10;
            payment2.Amount = 25;
            payment1.CreditCardNumber = "1234123412341234";
            payment2.CreditCardNumber = "5678567856785678";
            payment1.CreditCardIdentifier = "001";
            payment2.CreditCardIdentifier = "002";
            orderForm.Payments.Add(payment1);
            orderForm.Payments.Add(payment2);
        }
        catch (IndexOutOfRangeException ex)
        {
            Response.Write("Fewer than two payment methods.<br>");
            return;
        }

        // Add the OrderForm to the OrderFormCollection in the
        // Basket.

        myBasket.OrderForms.Add(orderForm);

        // Validate that the payment methods were added by
        // getting the PaymentMethodId for each Payment, and
        // then displaying the PaymentMethodName that is 
        // associated with the PaymentMethodId.

        foreach (OrderForm of in myBasket.OrderForms)
        {
            foreach (Payment pmt in of.Payments)
            {
                try
                {
                    DataRow[] results = validPaymentMethods.Tables[0].Select("PaymentMethodId" + " = '" + pmt.PaymentMethodId.ToString() + "'");
                    Response.Write(" PaymentMethodName: " + results[0]["PaymentMethodName"].ToString() + "<br>");
                }
                catch (Exception ex)
                {
                    Response.Write("<br>No valid payment method with PaymentMethodId = " + pmt.PaymentMethodId.ToString() + ".<br>");
                    return;
                }
            }
        }

        // Save the Basket.

        myBasket.Save();
    }
}

To run this sample, you must have configured at least two payment methods for your Commerce Server application. For more information about how to configure payment methods, see How to Create a Payment Method.

By default, the PaymentMethodRouter pipeline component validates that the sum of the payment amounts equals the total cost of the order. For information about how to disable this check, see PaymentMethodRouter.

See Also

Other Resources

Working with Baskets

How to Add Items to a Basket

Orders Runtime Object Model