using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using WebService.SDK.PayCommon;
namespace WebService.SDK.ALLinPay
{
///
/// 通联支付API
///
public class ALLinPayAPI
{
#region 扫码支付接口
///
/// 扫码支付接口
///
/// 接口配置
/// 平台商户信息
/// 用户付款授权码
/// 商户交易订单号
/// 交易金额(元)
///
public static Model.ScanResultModel ScanPay(ALLinPayConfig payConfig,
MobilePayConfig.PayMerchant merchantInfo, string authCode,
string merchantOrderCode, string payAmount)
{
if (payConfig == null)
{
return default(Model.ScanResultModel);
}
if (merchantInfo == null || string.IsNullOrWhiteSpace(merchantInfo.MerchantCode))
{
return default(Model.ScanResultModel);
}
if (!decimal.TryParse(payAmount, out decimal _PayAmount) || _PayAmount <= 0)
{
return default(Model.ScanResultModel);
}
Dictionary _ApiDictionary = new Dictionary
{
{ "orgid", payConfig.Organization },//通联平台机构号
{ "cusid", merchantInfo.MerchantCode },//通联平台商户号
{ "appid", payConfig.PlatformAPPID },//通联平台APPID
{ "randomstr", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() },//随机字符串
{ "trxamt", (_PayAmount*100).ToString("F0") }, //交易金额
{ "reqsn", merchantOrderCode },//商户订单号
{ "authcode", authCode }//用户付款授权码
};
_ApiDictionary.Add("sign", ApiParamSign(_ApiDictionary, payConfig.PlatformKey));
LogHelper.WriteReceiveLog("支付反扫-传第三方参数:" +
_ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()).
DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b));
return JsonConvert.DeserializeObject(CommonHelper.HttpUrlPost(
_ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()).
DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b), payConfig.ScanPayURL,
"application/x-www-form-urlencoded", payConfig.PlatformTimeout));
}
#endregion
#region 支付查询接口
///
/// 支付查询接口
///
/// 接口配置
/// 平台商户信息
/// 商户订单号
/// 注:与平台订单号必填一项
/// 平台订单号
/// 注:与商户订单号必填一项
/// 交易金额(元)
///
public static Model.QueryResultModel QueryPay(ALLinPayConfig payConfig,
MobilePayConfig.PayMerchant merchantInfo, string merchantOrderCode,
string platformOrderCode, string payAmount)
{
if (payConfig == null)
{
return default(Model.QueryResultModel);
}
if (merchantInfo == null || string.IsNullOrWhiteSpace(merchantInfo.MerchantCode))
{
return default(Model.QueryResultModel);
}
Dictionary _ApiDictionary = new Dictionary
{
{ "orgid", payConfig.Organization },//通联平台机构号
{ "cusid", merchantInfo.MerchantCode },//通联平台商户号
{ "appid", payConfig.PlatformAPPID },//通联平台APPID
{ "randomstr", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() },//随机字符串
{ "reqsn", merchantOrderCode }//商户订单号
};
if (!string.IsNullOrWhiteSpace(platformOrderCode) && platformOrderCode != merchantOrderCode)
{
_ApiDictionary.Add("trxid", platformOrderCode); //平台交易流水
}
_ApiDictionary.Add("sign", ApiParamSign(_ApiDictionary, payConfig.PlatformKey));
LogHelper.WriteReceiveLog("支付查询-传第三方参数:" +
_ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()).
DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b));
return JsonConvert.DeserializeObject(CommonHelper.HttpUrlPost(
_ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()).
DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b), payConfig.QueryURL,
"application/x-www-form-urlencoded", payConfig.PlatformTimeout));
}
#endregion
#region 退款接口
///
/// 退款接口
///
/// 接口配置
/// 平台商户信息
/// 平台订单号(原流水号)
/// 注:与商户订单号必填一项
/// 商户订单号(原流水号)
/// 注:与平台订单号必填一项
/// 退款订单号
/// 退款金额(元)
///
public static Model.RefundResultModel RefundPay(ALLinPayConfig payConfig,
MobilePayConfig.PayMerchant merchantInfo, string platformOrderCode,
string merchantOrderCode, string refundOrderCode, string payAmount)
{
if (payConfig == null)
{
return default(Model.RefundResultModel);
}
if (merchantInfo == null || string.IsNullOrWhiteSpace(merchantInfo.MerchantCode))
{
return default(Model.RefundResultModel);
}
if (!decimal.TryParse(payAmount, out decimal _PayAmount) || _PayAmount <= 0)
{
return default(Model.RefundResultModel);
}
Dictionary _ApiDictionary = new Dictionary
{
{ "orgid", payConfig.Organization },//通联平台机构号
{ "cusid", merchantInfo.MerchantCode },//通联平台商户号
{ "appid", payConfig.PlatformAPPID },//通联平台APPID
{ "randomstr", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() },//随机字符串
{ "trxamt", (_PayAmount*100).ToString("F0") }, //退款金额
{ "reqsn", refundOrderCode },//退款订单号
{ "oldtrxid", platformOrderCode } //商户退款码
};
if (!string.IsNullOrWhiteSpace(merchantOrderCode))
{
_ApiDictionary.Add("oldreqsn", merchantOrderCode);//原交易订单号
}
_ApiDictionary.Add("sign", ApiParamSign(_ApiDictionary, payConfig.PlatformKey));
LogHelper.WriteReceiveLog("支付退款-传第三方参数:" +
_ApiDictionary.Select(pair => pair.Key + "=" + pair.Value).
DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b));
return JsonConvert.DeserializeObject(CommonHelper.HttpUrlPost(
_ApiDictionary.Select(pair => pair.Key + "=" + pair.Value).
DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b), payConfig.RefundURL,
"application/x-www-form-urlencoded", payConfig.PlatformTimeout));
}
#endregion
///
/// 通联支付平台参数签名
///
/// 待签名参数集
/// 签名密钥
///
public static string ApiParamSign(Dictionary signParm, string MD5Key)
{
return BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(
new Dictionary(signParm) { { "key", MD5Key } }.OrderBy(p => p.Key)
.Select(pair => pair.Key + "=" + pair.Value.ToString()).DefaultIfEmpty("")
.Aggregate((a, b) => a + "&" + b)))).Replace("-", "").ToUpper();
}
}
}