IPaymentArgsCreator interface
This article explains how to implement the IPaymentArgsCreator interface, which converts the CheckoutFlowInfo into ExecutePaymentArgs.
The checkout flow, when calling the payment provider, uses the CheckoutFlowInfo that has information about the payment collected from the user. This information are one time settings that do not need to be saved into the database, but they need to be used in the current process of making the payment. The most common are the customer's social security number and host IP address.
First we create a PaymentArgs class that extend the ExecutePaymentArgs class, which will hold the payment provider specific information. For our example, we will not have any additional information.
using Litium.Foundation.Modules.ECommerce.Plugins.Payments;
namespace Litium.Studio.AddOns.Samples.TestPaymentsProvider
{
/// <summary>
/// Default implementation to pass information to <see cref="TestPaymentsProvider.ExecutePayment"/> method.
/// </summary>
public class PaymentArgs:ExecutePaymentArgs
{
}
}
Then we create the PaymentArgsCreator class, implement the IPaymentArgsCreator interface and mark it with the Plugins attribute.
using System;
using System.Web;
using Litium.Foundation.Modules.ECommerce.Plugins.Checkout;
using Litium.Foundation.Modules.ECommerce.Plugins.Payments;
using Litium.Owin.InversionOfControl;
namespace Litium.Studio.AddOns.Samples.TestPaymentsProvider
{
/// <summary>
/// Converts the <see cref="CheckoutFlowInfo"/> into <see cref="ExecutePaymentArgs"/>
/// </summary>
[Plugin(TestPaymentsProvider.ProviderName)]
public class PaymentArgsCreator:IPaymentArgsCreator
{
public ExecutePaymentArgs CreatePaymentArgs(CheckoutFlowInfo checkoutFlowInfo)
{
var args = new PaymentArgs();
//store end-users IP address
var currentContext = HttpContext.Current;
if (currentContext != null)
{
args.UserHostAddress = HttpContext.Current.Request.UserHostAddress;
}
//set the payment mode if its being set, otherwise leave it at its default value.
if (checkoutFlowInfo.ExecutePaymentMode != ExecutePaymentMode.Unset)
{
args.PaymentMode = checkoutFlowInfo.ExecutePaymentMode;
}
return args;
}
/// <summary>
/// Gets the name of the payment provider to which this Payment Args factory supports creationg payment args.
/// </summary>
/// <value>
/// The name of the payment provider.
/// </value>
public Type PaymentProviderType
{
get { return typeof(TestPaymentsProvider); }
}
}
}