//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.nopCommerce.com/License.aspx.
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the License for the specific language governing rights and limitations under the License.
//
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
//
// Contributor(s):
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;
using NopSolutions.NopCommerce.BusinessLogic.Directory;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic.Payment;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.Common.Utils;
namespace NopSolutions.NopCommerce.Payment.Methods.Alipay{
/// <summary>
/// Represents an Alipay payment gateway
/// </summary>
public class AlipayPaymentProcessor : IPaymentMethod
{
#region Methods
/// <summary>
/// Process payment
/// </summary>
/// <param name="paymentInfo">Payment info required for an order processing</param>
/// <param name="customer">Customer</param>
/// <param name="orderGuid">Unique order identifier</param>
/// <param name="processPaymentResult">Process payment result</param>
public void ProcessPayment(PaymentInfo paymentInfo, Customer customer, Guid orderGuid, ref ProcessPaymentResult processPaymentResult)
{
processPaymentResult.PaymentStatus = PaymentStatusEnum.Pending;
}
/// <summary>
/// Post process payment (payment gateways that require redirecting)
/// </summary>
/// <param name="order">Order</param>
/// <returns>The error status, or String.Empty if no errors</returns>
public string PostProcessPayment(Order order)
{
string gateway = "https://www.alipay.com/cooperate/gateway.do?";
string service = "create_direct_pay_by_user";
string seller_email = SettingManager.GetSettingValue("PaymentMethod.Alipay.SellerEmail");
string sign_type = "MD5";
string key = SettingManager.GetSettingValue("PaymentMethod.Alipay.Key");
string partner = SettingManager.GetSettingValue("PaymentMethod.Alipay.Partner");
string input_charset = "utf-8";
string show_url = "http://www.alipay.com/";
string out_trade_no = order.OrderId.ToString();
string subject = SettingManager.StoreName;
string body = "Order from " + SettingManager.StoreName;
string total_fee = order.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture);
string notify_url = CommonHelper.GetStoreLocation() + "Alipay_Notify.aspx";
string return_url = CommonHelper.GetStoreLocation() + "Alipay_Return.aspx";
string[] para ={
"service="+service,
"partner=" + partner,
"seller_email=" + seller_email,
"out_trade_no=" + out_trade_no,
"subject=" + subject,
"body=" + body,
"total_fee=" + total_fee,
"show_url=" + show_url,
"payment_type=1",
"notify_url=" + notify_url,
"return_url=" + return_url,
"_input_charset=" + input_charset
};
string aliay_url = CreatUrl(
para,
input_charset,
key
);
RemotePost post = new RemotePost();
post.FormName = "alipaysubmit";
post.Url = "https://www.alipay.com/cooperate/gateway.do?_input_charset=utf-8";
post.Method = "POST";
post.Add("service", service);
post.Add("partner", partner);
post.Add("seller_email", seller_email);
post.Add("out_trade_no", out_trade_no);
post.Add("subject", subject);
post.Add("body", body);
post.Add("total_fee", total_fee);
post.Add("show_url", show_url);
post.Add("return_url", return_url);
post.Add("notify_url", notify_url);
post.Add("payment_type", "1");
post.Add("sign", aliay_url);
post.Add("sign_type", sign_type);
post.Post();
return String.Empty;
}
/// <summary>
/// Gets additional handling fee
/// </summary>
/// <returns>Additional handling fee</returns>
public decimal GetAdditionalHandlingFee()
{
return SettingManager.GetSettingValueDecimalNative("PaymentMethod.Alipay.AdditionalFee");
}
/// <summary>
/// Captures payment
/// </summary>
/// <param name="order">Order</param>
/// <param name="processPaymentResult">Process payment result</param>
public void Capture(Order order, ref ProcessPaymentResult processPaymentResult)
{
throw new NotImplementedException();
}
/// <summary>
/// Refunds payment
/// </summary>
/// <param name="order">Order</param>
/// <param name="cancelPaymentResult">Cancel payment result</param>
public void Refund(Order order, ref CancelPaymentResult cancelPaymentResult)
{
throw new NotImplementedException();
}
/// <summary>
/// Voids paymen
/// </summary>
/// <param name="order">Order</param>
/// <param name="cancelPaymentResult">Cancel payment result</param>
public void Void(Order order, ref CancelPaymentResult cancelPaymentResult)
{
throw new NotImplementedException();
}
/// <summary>
/// Process recurring payment
/// </summary>
/// <param name="paymentInfo">Payment info required for an order processing</param>
/// <param name="customer">Customer</param>
/// <param name="orderGuid">Unique order identifier</param>
/// <param name="processPaymentResult">Process payment result</param>
public void ProcessRecurringPayment(PaymentInfo paymentInfo, Customer customer, Guid orderGuid, ref ProcessPaymentResult processPaymentResult)
{
throw new NotImplementedException();
}
/// <summary>
/// Cancels recurring payment
/// </summary>
/// <param name="order">Order</param>
/// <param name="cancelPaymentResult">Cancel payment result</param>
public void CancelRecurringPayment(Order order, ref CancelPaymentResult cancelPaymentResult)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets MD5 hash
/// </summary>
/// <param name="Input">Input</param>
/// <param name="Input_charset">Input charset</param>
/// <returns>Result</returns>
public static string GetMD5(string Input, string Input_charset)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(Encoding.GetEncoding(Input_charset).GetBytes(Input));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
/// <summary>
/// Bubble sort
/// </summary>
/// <param name="Input">Input</param>
/// <returns>Result</returns>
public static string[] BubbleSort(string[] Input)
{
int i, j;
string temp;
bool exchange;
for (i = 0; i < Input.Length; i++)
{
exchange = false;
for (j = Input.Length - 2; j >= i; j--)
{
if (System.String.CompareOrdinal(Input[j + 1], Input[j]) < 0)
{
temp = Input[j + 1];
Input[j + 1] = Input[j];
Input[j] = temp;
exchange = true;
}
}
if (!exchange)
{
break;
}
}
return Input;
}
/// <summary>
/// Create URL
/// </summary>
/// <param name="Para">Para</param>
/// <param name="InputCharset">Input charset</param>
/// <param name="Key">Key</param>
/// <returns>Result</returns>
public static string CreatUrl(string[] Para, string InputCharset, string Key)
{
int i;
string[] Sortedstr = BubbleSort(Para);
StringBuilder prestr = new StringBuilder();
for (i = 0; i < Sortedstr.Length; i++)
{
if (i == Sortedstr.Length - 1)
{
prestr.Append(Sortedstr[i]);
}
else
{
prestr.Append(Sortedstr[i] + "&");
}
}
prestr.Append(Key);
string sign = GetMD5(prestr.ToString(), InputCharset);
return sign;
}
/// <summary>
/// Gets HTTP
/// </summary>
/// <param name="StrUrl">Url</param>
/// <param name="Timeout">Timeout</param>
/// <returns>Result</returns>
public static string Get_Http(string StrUrl, int Timeout)
{
string strResult = string.Empty;
try
{
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(StrUrl);
myReq.Timeout = Timeout;
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream();
StreamReader sr = new StreamReader(myStream, Encoding.Default);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine());
}
strResult = strBuilder.ToString();
}
catch (Exception exc)
{
strResult = "Error: " + exc.Message;
}
return strResult;
}
#endregion
#region Properties
/// <summary>
/// Gets a value indicating whether capture is supported
/// </summary>
public bool CanCapture
{
get
{
return false;
}
}
/// <summary>
/// Gets a value indicating whether refund is supported
/// </summary>
public bool CanRefund
{
get
{
return false;
}
}
/// <summary>
/// Gets a value indicating whether void is supported
/// </summary>
public bool CanVoid
{
get
{
return false;
}
}
/// <summary>
/// Gets a recurring payment type of payment method
/// </summary>
/// <returns>A recurring payment type of payment method</returns>
public RecurringPaymentTypeEnum SupportRecurringPayments
{
get
{
return RecurringPaymentTypeEnum.NotSupported;
}
}
/// <summary>
/// Gets a payment method type
/// </summary>
/// <returns>A payment method type</returns>
public PaymentMethodTypeEnum PaymentMethodType
{
get
{
return PaymentMethodTypeEnum.Standard;
}
}
#endregion
}
}
|