custom payment

mahendran s asked on January 24, 2019 11:23

this is the class i created for custom payment authorization process .

using System.Collections.Generic; using System.Net.Http; using CMS.Ecommerce; using CMS.Ecommerce.Web.UI; using CMS.Helpers; using CMSApp;

namespace CMSApp { public class CustomGatewayProvider : CMSPaymentGatewayProvider, IDirectPaymentGatewayProvider { /// public override string ValidateCustomData(IDictionary<string, object> paymentData) { // Gets the email address value from the submitted payment form string paymentAccountEmail = ValidationHelper.GetString(paymentData["PaymentAccountEmail"], "");

        // Checks whether the value is a valid email address
        if (!ValidationHelper.IsEmail(paymentAccountEmail))
        {
            return "Please enter a valid email address identifying your payment account.";
        }

        return string.Empty;
    }

    /// <summary>
    /// Processes the payment after a customer submits valid data through the payment form.
    /// </summary>
    public PaymentResultInfo ProcessPayment(IDictionary<string, object> paymentData)
    {
        // Prepares the URL of the external gateway's payment page
        string gatewayUrl = "https://www.custompayments.com/service/directpayment";

        // Adds query string parameters to the payment page URL with data for the gateway
        // Most parameter values are loaded from the related order, the payment account email is from the payment form
        gatewayUrl = URLHelper.AddParameterToUrl(gatewayUrl, "orderid", OrderId.ToString());
        gatewayUrl = URLHelper.AddParameterToUrl(gatewayUrl, "price", Order.OrderGrandTotal.ToString());
        gatewayUrl = URLHelper.AddParameterToUrl(gatewayUrl, "currency", CurrencyInfoProvider.GetCurrencyInfo(Order.OrderCurrencyID).CurrencyCode);
        gatewayUrl = URLHelper.AddParameterToUrl(gatewayUrl, "accountEmail", ValidationHelper.GetString(paymentData["PaymentAccountEmail"], ""));

        gatewayUrl = URLHelper.UrlEncodeQueryString(gatewayUrl);

        // Sets the external gateway URL into the payment results
        // Customers are redirected to this URL to finish the payment after they successfully submit the payment form
        PaymentResult.PaymentApprovalUrl = gatewayUrl;
        PaymentResult.PaymentDescription = "Customer payment approval pending";

        // Saves the payment result to the related order
        // Automatically sets the PaymentResult time stamp (date) and payment method properties
        UpdateOrderPaymentResult();

        // Returns the partially set payment results.
        // The customer finishes the payment on an external page, so the example assumes that a HTTP handler (IPN)
        // later processes the gateway's reply, and calls the provider's 'ProcessDirectPaymentReply' method.
        return PaymentResult;
    }

    /// <summary>
    /// Processes the payment results based on a reply from the payment gateway.
    /// For example, the method can be called from a custom IPN handler that accepts responses/notifications from
    /// the external gateway's responses/notifications.
    /// </summary>
    public void ProcessDirectPaymentReply(bool success, string transactionId)
    {
        if (success)
        {
            // Sets the payment result for successful transactions
            PaymentResult.PaymentIsCompleted = true;
            PaymentResult.PaymentDescription = "Payment complete";
            PaymentResult.PaymentTransactionID = transactionId;
        }
        else
        {
            // Sets the payment result for failed transactions
            PaymentResult.PaymentIsFailed = true;
            PaymentResult.PaymentDescription = "Payment failed";
        }

        // Saves the payment result to the related order
        // Moves the order to the status configured for successful or failed payments
        UpdateOrderPaymentResult();
    }
}
public class CustomDelayedCaptureGatewayProvider : CMSPaymentGatewayProvider, IDelayedPaymentGatewayProvider
{
    // Class used for JSON serialization of data posted during capture transactions
    private class CaptureData
    {
        public string TransactionId { get; set; }
    }

    /// <summary>
    /// Validates data submitted by the customer through the related payment form.
    /// </summary>
    public override string ValidateCustomData(IDictionary<string, object> paymentData)
    {
        // Gets the email address value from the submitted payment form
        string paymentAccountEmail = ValidationHelper.GetString(paymentData["PaymentAccountEmail"], "");

        // Checks whether the value is a valid email address
        if (!ValidationHelper.IsEmail(paymentAccountEmail))
        {
            return "Please enter a valid email address identifying your payment account.";
        }

        return string.Empty;
    }

    /// <summary>
    /// Indicates whether the provider uses delayed capture of funds.
    /// You can parameterize the result (for example using a custom setting) for provider classes that allow both direct payments
    /// and delayed capture (i.e. implement both the IDirectPaymentGatewayProvider and IDelayedPaymentGatewayProvider interfaces).
    /// </summary>
    public override bool UseDelayedPayment()
    {
        return true;
    }

    /// <summary>
    /// Indicates whether the processed payment represents a transaction that was successfully authorized for later capture.
    /// Required to control the visibility of the 'Capture payment' button in the order editing interface.
    /// </summary>
    public bool IsPaymentAuthorized
    {
        get
        {
            return PaymentResult != null && PaymentResult.PaymentIsAuthorized;
        }
    }

    /// <summary>
    /// Processes the authorization transaction after a customer submits valid data through the payment form.
    /// </summary>
    public PaymentResultInfo AuthorizePayment(IDictionary<string, object> paymentData)
    {
        // Prepares the URL of the external gateway's payment/authorization page
        string gatewayUrl = "https://www.custompayments.com/service/authorize";

        // Adds query string parameters to the payment page URL with data for the gateway
        // Most parameter values are loaded from the related order, the payment account email is from the payment form
        gatewayUrl = URLHelper.AddParameterToUrl(gatewayUrl, "orderid", OrderId.ToString());
        gatewayUrl = URLHelper.AddParameterToUrl(gatewayUrl, "price", Order.OrderGrandTotal.ToString());
        gatewayUrl = URLHelper.AddParameterToUrl(gatewayUrl, "currency", CurrencyInfoProvider.GetCurrencyInfo(Order.OrderCurrencyID).CurrencyCode);
        gatewayUrl = URLHelper.AddParameterToUrl(gatewayUrl, "accountEmail", ValidationHelper.GetString(paymentData["PaymentAccountEmail"], ""));

        gatewayUrl = URLHelper.UrlEncodeQueryString(gatewayUrl);

        // Sets the external gateway URL into the payment results
        // Customers are redirected to this URL to finish the authorization after they successfully submit the payment form
        PaymentResult.PaymentApprovalUrl = gatewayUrl;
        PaymentResult.PaymentDescription = "Customer authorization approval pending";

        // Saves the payment result to the related order
        // Automatically sets the PaymentResult time stamp (date) and payment method properties
        UpdateOrderPaymentResult();

        // Returns the partially set payment results.
        // The customer finishes the payment on an external page, so the example assumes that a HTTP handler (IPN)
        // later processes the gateway's reply, and calls the provider's 'ProcessAuthorizationReply' method.
        return PaymentResult;
    }

    /// <summary>
    /// Processes the payment results for authorization transactions based on a reply from the payment gateway.
    /// For example, such methods can be called from a custom IPN handler that accepts the response/notification from
    /// the external payment gateway.
    /// </summary>
    public void ProcessAuthorizationReply(bool success, string transactionId)
    {
        if (success)
        {
            // Sets the payment result for successful authorization transactions
            PaymentResult.PaymentIsAuthorized = true;
            PaymentResult.PaymentDescription = "Payment authorized";
            PaymentResult.PaymentTransactionID = transactionId;
        }
        else
        {
            // Sets the payment result for failed authorization transactions
            PaymentResult.PaymentIsFailed = true;
            PaymentResult.PaymentDescription = "Payment authorization failed";
        }

        // Saves the payment result to the related order
        // Moves the order to the status configured for authorized or failed payments
        UpdateOrderPaymentResult();
    }

    /// <summary>
    /// Performs capture of funds for successfully authorized payments.
    /// Triggered when the 'Capture payment' button is clicked when editing an order using the corresponding payment method.
    /// </summary>
    public PaymentResultInfo CapturePayment()
    {
        // Prepares the URL of the external gateway's payment capture service
        string gatewayUrl = "https://www.custompayments.com/service/capture";

        HttpResponseMessage response = null;

        using (var client = new HttpClient())
        {
            // Prepares an object containing payment capture parameters for the gateway (suitable for JSON serialization)
            var captureData = new CaptureData
            {
                // Gets the transaction ID from the order's payment result data
                TransactionId = PaymentResult.PaymentTransactionID
            };

            // Sends a POST request to the payment capture service with the payment parameters
            response = client.PostAsJsonAsync(gatewayUrl, captureData).Result;
        }

        // Marks the payment result as finished if the fund capture is successful
        // The example only checks the status code of the HTTP response
        // For real payment gateways, you need to process the response content as required by the gateway's API
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            PaymentResult.PaymentIsCompleted = true;
            PaymentResult.PaymentDescription = "Payment captured";
        }
        else
        {
            PaymentResult.PaymentIsFailed = false;
            PaymentResult.PaymentDescription = "Payment capture failed";
        }

        // Saves the payment result to the related order
        UpdateOrderPaymentResult();

        return PaymentResult;
    }
    [assembly: RegisterImplementation(typeof(IPaymentGatewayFormFactory), typeof(CustomPaymentGatewayFormFactory))]

    public class CustomPaymentGatewayFormFactory : PaymentGatewayFormFactory
    {
        protected override string GetPath(CMSPaymentGatewayProvider provider)
        {
            // Maps the path to the correct payment gateway form for the custom gateway providers
            if ((provider is CustomGatewayProvider) || (provider is CustomDelayedCaptureGatewayProvider))
            {
                return "~/DancingGoat/CustomGatewayForm.ascx";
            }
            else
            {
                // Calls the base method to map the paths of the default payment gateways
                return base.GetPath(provider);
            }
        }
    }
}

}

class test { public static void main(string[] args) { // Gets data from the payment gateway's notification request // This example uses URL parameters (the implementation depends on the gateway's API) string transactionId = URLHelper.GetUrlParameter(RequestContext.CurrentURL, "transactionid"); int orderId = ValidationHelper.GetInteger(URLHelper.GetUrlParameter(RequestContext.CurrentURL, "orderid"), 0);

    // Gets the Kentico order object related to the payment
    OrderInfo order = OrderInfoProvider.GetOrderInfo(orderId);

    if (order != null)
    {
        // Prepares an instance of the payment gateway provider used by the order's payment method
        CustomGatewayProvider customProvider = CMSPaymentGatewayProvider.GetPaymentGatewayProvider<CustomGatewayProvider>(order.OrderPaymentOptionID);
        customProvider.OrderId = orderId;

        // Calls the provider's method for processing payment results
        customProvider.ProcessDirectPaymentReply(true, transactionId);
    }
}

}

this is the email form in ascx file under CMSApp project

<%@ Control Language="C#" AutoEventWireup="true" Codebehind="novalnet.ascx.cs" Inherits="sample" %>

Payment account details

<asp:Label ID="lblError" runat="server" EnableViewState="false" CssClass="ErrorLabel" Visible="false" /> <asp:Label ID="lblPaymentAccountEmail" EnableViewState="false" runat="server" Text="Account email address:" /> <asp:TextBox ID="txtPaymentAccountEmail" runat="server" Height="16px" Width="127px" OnTextChanged="txtPaymentAccountEmail_TextChanged" />

this is my sample.ascx.cs file

using System; using System.Collections.Generic; using CMS.Ecommerce.Web.UI; using CMS.Ecommerce;

public partial class sample: CMSPaymentGatewayForm { /// protected override void OnInitialized() { // Attempts to get a default email address value from the Kentico customer object string customerEmail = CustomerInfoProvider.GetCustomerInfo(PaymentProvider.Order.OrderCustomerID)?.CustomerEmail; if (!String.IsNullOrEmpty(customerEmail)) { txtPaymentAccountEmail.Text = customerEmail; } }

/// <summary>
/// Loads the customer's input from the payment form.
/// </summary>
public override IDictionary<string, object> GetPaymentGatewayData()
{
    var paymentData = new Dictionary<string, object>();
    paymentData.Add("PaymentAccountEmail", txtPaymentAccountEmail.Text.Trim());

    return paymentData;
}

/// <summary>
/// Used for validation of the payment form's inputs.
/// </summary>
public override string ValidateData()
{
    // The 'ValidateData' method of the base class delegates the validation logic to the related payment provider
    // First gets the form's data using the 'GetPaymentGatewayData()' method
    // and then validates it by calling the provider's 'ValidateCustomData' method
    string error = base.ValidateData();

    if (!String.IsNullOrEmpty(error))
    {
        lblError.Visible = true;
        lblError.Text = error;
    }

    return error;
}

protected void txtPaymentAccountEmail_TextChanged(object sender, EventArgs e)
{

}

}

//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------

this is my designer.cs file

public partial class sample{

/// <summary>
/// lblError control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblError;

/// <summary>
/// lblPaymentAccountEmail control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblPaymentAccountEmail;

/// <summary>
/// txtPaymentAccountEmail control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox txtPaymentAccountEmail;

}

please help me to create custom payment creation

   Please, sign in to be able to submit a new answer.