|
<%@ WebHandler Language = "C#" Class="PayPalIPN" %> |
|
|
|
using System; |
|
using System.Web; |
|
using CMS.Ecommerce; |
|
using CMS.EcommerceProvider; |
|
using CMS.EventLog; |
|
using CMS.Helpers; |
|
using CMS.Localization; |
|
|
|
public class PayPalIPN : IHttpHandler |
|
{ |
|
|
|
private int mOrderId = 0; |
|
private string mTransactionId = ""; |
|
private string mPaymentStatus = ""; |
|
private string mOrderCulture = null; |
|
|
|
public void ProcessRequest(HttpContext context) |
|
{ |
|
var request = context.Request; |
|
|
|
// Get order id |
|
mOrderId = ValidationHelper.GetInteger(request.Form["invoice"], 0); |
|
|
|
// Get transaction id |
|
mTransactionId = ValidationHelper.GetString(request.Form["txn_id"], ""); |
|
|
|
// Get payment status |
|
mPaymentStatus = ValidationHelper.GetString(request.Form["payment_status"], ""); |
|
|
|
// Get notification culture stored in custom field |
|
mOrderCulture = ValidationHelper.GetString(request.Form["custom"], ""); |
|
|
|
CMSPayPalProvider payPalProvider; |
|
string errorMessage; |
|
|
|
// Get PayPal provider |
|
OrderInfo oi = OrderInfoProvider.GetOrderInfo(mOrderId); |
|
if (oi != null) |
|
{ |
|
try |
|
{ |
|
payPalProvider = (CMSPayPalProvider)CMSPaymentGatewayProvider.GetPaymentGatewayProvider(oi.OrderPaymentOptionID); |
|
payPalProvider.OrderId = mOrderId; |
|
} |
|
catch (Exception ex) |
|
{ |
|
// Log exception |
|
errorMessage = EventLogProvider.GetExceptionLogMessage(ex); |
|
LogEvent(errorMessage, "Payment_Provider_Not_Found"); |
|
return; |
|
} |
|
} |
|
else |
|
{ |
|
// Order not found |
|
errorMessage = string.Format(LocalizationHelper.GetString("PaymentGatewayProvider.ordernotfound"), mOrderId); |
|
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).ToLowerCSafe(); |
|
|
|
// Check IPN validation result |
|
var verified = (response == "verified"); |
|
if (!verified) |
|
{ |
|
LogEvent(LocalizationHelper.GetString("com.ipninvalidnotification"), "IPNNotificationValidation"); |
|
return; |
|
} |
|
|
|
// Set payment as verified |
|
paypalResult.PayPalPaymentVerified = true; |
|
|
|
// Set transaction ID |
|
paypalResult.PaymentTransactionID = mTransactionId; |
|
|
|
// Set payment status |
|
paypalResult.PayPalPaymentStatus = PayPalPaymentResultInfo.GetPayPalPaymentStatus(mPaymentStatus); |
|
|
|
// 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 = mOrderCulture; |
|
|
|
// Update order payment result in database |
|
errorMessage = payPalProvider.UpdateOrderPaymentResult(); |
|
if (errorMessage != "") |
|
{ |
|
// Log the event |
|
errorMessage += error; |
|
LogEvent(errorMessage, "Order_Payment_Result_Update"); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// Logs error. |
|
/// </summary> |
|
/// <param name="message">Error message</param> |
|
/// <param name="eventCode">Event code</param> |
|
protected void LogEvent(string message, string eventCode) |
|
{ |
|
try |
|
{ |
|
// Add some additional information to the error message |
|
string info = "ORDER ID: " + mOrderId + "\r\n"; |
|
info += "TRANSACTION ID: " + mTransactionId + "\r\n"; |
|
info += "PAYMENT STATUS: " + mPaymentStatus + "\r\n"; |
|
|
|
message = info + message; |
|
|
|
EventLogProvider.LogEvent(EventType.ERROR, "PayPal IPN", eventCode, message); |
|
} |
|
catch |
|
{ |
|
// Unable to log the event |
|
} |
|
} |
|
|
|
public bool IsReusable |
|
{ |
|
get |
|
{ |
|
return false; |
|
} |
|
} |
|
|
|
} |