Payment Gateway - Update Order Status

Chamara Janaka asked on January 20, 2017 02:57

Hi,

I'm trying to update the order status once the payment has been completed. I'm using a third party payment gateway so the user is redirected to the payment gateway and redirected back to kentico after the payment is finished.

At this point I need to make the following changes in kentico.

  1. Mark the order as completed.
  2. Mark the payment is completed.
  3. Change order status to Payment Received.

Following is my code on the confirmation page.

    if (!Page.IsPostBack)
    {
        string orderID = Request.QueryString["orderid"];
        string receiptNo = Request.QueryString["receiptno"];

        if (isInt(orderID))
        {
            OrderInfo order = OrderInfoProvider.GetOrderInfo(Convert.ToInt32(orderID));

            if (order != null)
            {
                if (order.OrderPaymentResult != null)
                {
                    order.OrderPaymentResult.PaymentIsCompleted = true;
                    order.OrderPaymentResult.PaymentDescription = "Payment Successful";
                }

                order.OrderIsPaid = true;

                //var status = OrderStatusInfoProvider.GetOrderStatusInfo(order.OrderStatusID);
                //order.OrderStatusID = 3;

                OrderInfoProvider.SetOrderInfo(order);
            }

        }
    }

I think I'm setting all the values correctly but not sure how to update the order status. I don't wan't to hard code OrderStatusID in my code. Is there any other way to do it?

Correct Answer

Anton Grekhovodov answered on January 20, 2017 05:38

If you have the following order of statuses: Image Text

So when a customer is redirected back to your site after successful payment, you can set another enabled status like:

var status = OrderStatusInfoProvider.GetNextEnabledStatus(order.OrderStatusID)
// or
// var status = OrderStatusInfoProvider.GetOrderStatuses().WhereEquals("StatusOrderIsPaid", true)
order.OrderStatusID = status.StatusID;
1 votesVote for this answer Unmark Correct answer

Recent Answers


Peter M answered on January 20, 2017 03:43 (last edited on January 20, 2017 03:45)

You should look at the existing examples for instance Paypal (see below). You can find code CMS\CMSModules\Ecommerce\CMSPages\PayPalIPN.aspx.cs in default intallation (corporated site) You should use paymentresultinfo for you purpose. PayPalPaymentResultInfo is inhereted from PaymentResultInfo, but I guess if don't need any customization you can use directly PaymentResultInfo.

public partial class CMSModules_Ecommerce_CMSPages_PayPalIPN : CMSPage
{
    private int orderId = 0;
    private string transactionId = "";
    private string paymentStatus = "";
    private string orderCulture = null;


    protected void Page_Load(object sender, EventArgs e)
    {
        // Get order id
        orderId = ValidationHelper.GetInteger(Request.Form["invoice"], 0);

        // Get transaction id
        transactionId = ValidationHelper.GetString(Request.Form["txn_id"], "");

        // Get payment status
        paymentStatus = ValidationHelper.GetString(Request.Form["payment_status"], "");

    // Get notification culture stored in custom field
    orderCulture = ValidationHelper.GetString(Request.Form["custom"], "");

    CMSPayPalProvider payPalProvider;
    string errorMessage;

    // Get PayPal provider
    OrderInfo oi = OrderInfoProvider.GetOrderInfo(orderId);
    if (oi != null)
    {
        try
        {
            payPalProvider = (CMSPayPalProvider)CMSPaymentGatewayProvider.GetPaymentGatewayProvider(oi.OrderPaymentOptionID);
            payPalProvider.OrderId = orderId;
        }
        catch (Exception ex)
        {
            // Log exception
            errorMessage = EventLogProvider.GetExceptionLogMessage(ex);
            LogEvent(errorMessage, "Payment_Provider_Not_Found");
            return;
        }
    }
    else
    {
        // Order not found
        errorMessage = string.Format(GetString("PaymentGatewayProvider.ordernotfound"), orderId);
        LogEvent(errorMessage, "Order_Not_Found");
        return;
    }

    PayPalPaymentResultInfo paypalResult = (PayPalPaymentResultInfo)(payPalProvider.PaymentResult);

    // Confirm payment and get PayPal payment gateway response
    string response = payPalProvider.ConfirmPayment(Request.Form);

    // Check IPN validation result
    var verified = (response.ToLowerCSafe() == "verified");
    if (!verified)
    {
        LogEvent(GetString("com.ipninvalidnotification"), "IPNNotificationValidation");
        return;
    }

    // Set payment as verified
    paypalResult.PayPalPaymentVerified = true;

    // Set transaction ID
    paypalResult.PaymentTransactionID = transactionId;

    // Set payment status
    paypalResult.PayPalPaymentStatus = PayPalPaymentResultInfo.GetPayPalPaymentStatus(paymentStatus);

    // Set payment as completed / not completed
    switch (paypalResult.PayPalPaymentStatus)
    {
        case PayPalPaymentStatusEnum.CanceledReversal:
        case PayPalPaymentStatusEnum.Completed:
        case PayPalPaymentStatusEnum.Processed:
            paypalResult.PaymentIsCompleted = true;
            break;

        default:
            paypalResult.PaymentIsCompleted = false;
            break;
    }

    // Compare payment data to order data
    string error = payPalProvider.ComparePaymentDataToOrderData(Request.Form);
    if (error != "")
    {
        paypalResult.PaymentIsCompleted = false;
        paypalResult.PayPalPaymentVerified = false;
        paypalResult.PaymentDescription = error;
    }

    // Order culture
    payPalProvider.ShoppingCartInfoObj.ShoppingCartCulture = orderCulture;

    // Update order payment result in database
    errorMessage = payPalProvider.UpdateOrderPaymentResult();
    if (errorMessage != "")
    {
        // Log the event
        errorMessage += error;
        LogEvent(errorMessage, "Order_Payment_Result_Update");
    }
}

Hope this helps.

0 votesVote for this answer Mark as a Correct answer

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