前言

第三方短信接口,支付接口广泛应用于金融领域与电商领域,这是我在14年工作时的一些经验总结,现在分享出来,希望能对一部分朋友有所帮助!

内容

一:对接短信接口

对接短信接口大体分为两个步骤:

1. 根据接口文档发送短信类容到短信网关;

2. 回调结果并获取短信剩余条数

考虑到一个网站可能不止一个短信网关,我使用了工厂设计模式,下面附上代码

短信网关接口

    /// <summary>/// 短信网关接口/// </summary>public interface ISMS{/// <summary>/// 短信商/// </summary>SMSProvider Provider { get; }/// <summary>/// 发送短信/// </summary>/// <param name="destnumbers">要发送到的手机号码</param>/// <param name="msg">要发送的内容</param>/// <param name="sendTime">发送时间 为null表示立刻发送</param>int Send(string destnumbers, string msg, out SMSStatus status, DateTime? sendTime = null);/// <summary>/// 获取余额/// </summary>int Balance();/// <summary>/// 获取上行短信内容/// </summary>/// <returns>返回null表示没有获取到上行短信</returns>List<SMSReply> GetReply();}

短信工厂类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.ComponentModel;using SP.Studio.IO;
using SP.Studio.Core;namespace SP.Studio.Gateway.SMS
{/// <summary>/// 短信工厂/// </summary>public class SMSFactory{public static ISMS CreateSMS(SMSConfig config){ISMS sms = null;sms = (ISMS)Activator.CreateInstance(Type.GetType("SP.Studio.Gateway.SMS." + config.Provider),config.UserName,config.Password);return sms;}}/// <summary>/// 短信提供商/// </summary>public enum SMSProvider : byte{/// <summary>/// 无供应商/// </summary>
        None,/// <summary>/// 单元科技/// </summary>[Description("单元科技")]CCell,/// <summary>/// 单元科技 接口2/// </summary>[Description("单元科技")]CCell2,/// <summary>/// 亿美科技/// </summary>[Description("亿美科技")]EMay,/// <summary>/// /// <summary>/// 中国网建/// </summary>[Description("中国网建")]WebChinese,/// <summary>/// 国都/// </summary>[Description("国都")]Guodu,/// <summary>/// 企信通(鑫阳创投)/// </summary>[Description("企信通")]YDQXT,/// <summary>/// http://sdk.entinfo.cn:8061//// </summary>[Description("DXC")]DXC,/// <summary>/// 凌凯科技 http://www.zhongguowuxian.com/Item/list.asp?id=1460/// </summary>[Description("凌凯科技")]LingKai,/// <summary>/// 玄武科技/// </summary>[Description("玄武科技")]MT,/// <summary>/// 单元科技新接口/// </summary>[Description("单元科技-3")]CCell3,/// <summary>/// 使用短信猫发送/// </summary>[Description("短信猫")]Modem = 100}/// <summary>/// 短信息配置类/// </summary>public class SMSConfig{public SMSProvider Provider { get; set; }public string UserName { get; set; }public string Password { get; set; }}
}

短信工厂类

实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;using SP.Studio.Net;
using SP.Studio.Text;using SP.Studio.Security;using SP.Studio.Web;
namespace SP.Studio.Gateway.SMS
{public class LingKai : SMSBase, ISMS{public LingKai(string userName, string password): base(userName, password){}public SMSProvider Provider{get { return SMSProvider.LingKai; }}public int Send(string destnumbers, string msg, out SMSStatus status, DateTime? sendTime = null){if (!WebAgent.IsMobile(destnumbers)){status = SMSStatus.Number;return 0;}string url = string.Format("http://sdk.zhongguowuxian.com:98/ws/BatchSend.aspx?CorpID={0}&Pwd={1}&Mobile={2}&Content={3}&Cell=&SendTime=",this.UserName, this.Password, destnumbers, HttpUtility.UrlEncode(msg, Encoding.GetEncoding("GB2312")));string result = NetAgent.DownloadData(url, Encoding.GetEncoding("GB2312"));//大于等于0的数字,发送成功(得到大于等于0的数字、作为取报告的id);-1、帐号未注册;-2、其他错误;-3、密码错误;-4、手机号格式不对;-5、余额不足;-6、定时发送时间不是有效的时间格式;//-7、请在提交信息末尾添加中文企业签名【】; -8、发送内容需在1到500个字之间;-9、 发送号码为空 this.SaveGateway(result);status = SMSStatus.Success;long code;if (long.TryParse(result, out code)){switch (code){case -1:case -3:status = SMSStatus.Account;break;case -2:case -6:status = SMSStatus.Other;break;case -4:case -9:status = SMSStatus.Number;break;case -5:status = SMSStatus.Money;break;case -7:case -8:status = SMSStatus.Message;break;}}else{status = SMSStatus.Other;}return status == SMSStatus.Success ? 1 : 0;}/// <summary>///获取短信的剩余量/// </summary>/// <returns></returns>public int Balance(){string url = string.Format("http://sdk.zhongguowuxian.com:98/WS/SelSum.aspx?CorpID={0}&Pwd={1}", this.UserName, this.Password);string result = NetAgent.DownloadData(url,Encoding.GetEncoding("GB2312"));int balance;int.TryParse(result, out balance);return balance;}public List<SMSReply> GetReply(){return new List<SMSReply>();}}
}

实例

二:对接支付接口

对接支付接口大体步骤也分为两步,部分支付厂商可能存在小差异

1. 找到合适的支持厂商,签订协议,拿到商户code与签名私钥;

2. 根据提供对接文档Post数据跳转到支付网关;

3. 支付回调结果并验证商户网页(一般做法是把回调地址传给网关,在回调地址中验证支付结果)

下面附上部分代码

        /// <summary>/// Post数据,跳转支付网关/// </summary>public override void GoGateway(){SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();if (this.PayMethod == "bankPay")    // 如果是网银直接支付
            {sParaTemp.Add("defaultbank", this.defaultbank);}switch (this.Service){case "create_partner_trade_by_buyer": //  如果是担保支付case "trade_create_by_buyer":   // 双功能收款sParaTemp.Add("logistics_type", "POST");sParaTemp.Add("logistics_fee", "0.00");sParaTemp.Add("logistics_payment", "BUYER_PAY");sParaTemp.Add("price", base.Money.ToString("0.00"));sParaTemp.Add("quantity", "1");break;}sParaTemp.Add("partner", this.Partner);sParaTemp.Add("_input_charset", _input_charset.ToLower());sParaTemp.Add("service", this.Service);sParaTemp.Add("payment_type", paymentType.ToString());sParaTemp.Add("notify_url", this.NotifyUrl);sParaTemp.Add("return_url", this.ReturnUrl);sParaTemp.Add("seller_email", this.SellerEmail);sParaTemp.Add("out_trade_no", base.OrderID);sParaTemp.Add("subject", base.Name);sParaTemp.Add("total_fee", base.Money.ToString("0.00"));sParaTemp.Add("body", base.Description);sParaTemp.Add("paymethod", this.PayMethod);sParaTemp.Add("anti_phishing_key", this.anti_phishing_key);sParaTemp.Add("exter_invoke_ip", this.clientIP);Encoding encoding = Encoding.GetEncoding(_input_charset);Dictionary<string, string> sPara = this.FilterPara(sParaTemp);//处理字符串string prestr = this.CreateLinkString(sPara);sPara.Add("sign", Sign(prestr)); //签名验证sPara.Add("sign_type", sign_type);string url = string.Format("{0}_input_charset={1}&{2}", GATEWAY, _input_charset, CreateLinkString(sPara));HttpContext.Current.Response.Redirect(url);}public override bool Verify(VerifyCallBack callback){#region =========  DEBUG  ==========StringBuilder sb = new StringBuilder();sb.AppendLine(DateTime.Now + "\n");foreach (var keyValue in this.GetRequestGet()){sb.AppendFormat("{0}:{1}\t", keyValue.Key, keyValue.Value);}sb.AppendLine().AppendLine("------------------------");if (HttpContext.Current != null)System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("~/App_Data/Alipay.log"), sb.ToString(), Encoding.UTF8);#endregionSortedDictionary<string, string> inputPara = this.GetRequestGet();string notify_id = WebAgent.GetParam("notify_id");string sign = WebAgent.GetParam("sign");string status = WebAgent.GetParam("trade_status");Dictionary<string, string> sPara = this.FilterPara(inputPara);string preSignStr = this.CreateLinkString(sPara);if (this.Sign(preSignStr) != sign) return false;if (!string.IsNullOrEmpty(notify_id)){string veryfy_url = VERYFYURL + "partner=" + this.Partner + "&notify_id=" + notify_id;if (NetAgent.DownloadData(veryfy_url, Encoding.GetEncoding(_input_charset)) != "true") return false;}bool isSuccess = false;switch (status){case "TRADE_SUCCESS":   // 高级即时到帐状态下case "TRADE_FINISHED":  // 普通即时到帐状态下case "WAIT_SELLER_SEND_GOODS":  // 担保交易  等待卖家发货
isSuccess = true;break;default:isSuccess = false;break;}if (isSuccess) callback.Invoke();return isSuccess;}

View Code

PS:欢迎扫描下方二维码,加入QQ群

作者:Jacky
来源:https://www.cnblogs.com/ydcnblog/
声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/ydcnblog/p/9257901.html

对接第三方接口(短信,支付)相关推荐

  1. 易语言对接第三方验证码短信接口demo

    本文为您提供了易语言版本的验证码短信接口对接DEMO示例 //接口类型:互亿无线触发短信接口,支持发送验证码短信.订单通知短信等. //账户注册:请通过该地址开通账户 http://user.ihuy ...

  2. 如何用ASP语言对接第三方验证码短信接口?

    ASP对接验证码短信接口DEMO示例 <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <% '接口类型:互亿无 ...

  3. 怎么对接asp语言短信验证码接口?

    对接asp语言短信demo示例 <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <% '接口类型:互亿无线触发 ...

  4. JAVA对接发送SMS短信服务

    JAVA对接发送SMS短信服务 短信服务申请 JAVA对接 代码编写 配置类 SmsComponent nacos配置中心--对应上面读取的参数 调用 Vue前端测试代码 效果 结语 短信服务申请 网 ...

  5. 对接阿里云短信服务(附视频教程)

    阿里云短信服务文档使用指引: https://help.aliyun.com/document_detail/59210.html B站视频教程链接: https://www.bilibili.com ...

  6. 手把手教你对接阿里云短信服务

    正好双十二,最近在做一个小项目需要用到短信业务,注册了新用户125买了5000条短信,有效期2年. 那么如何对接阿里云短信,需要做哪些操作? 可以参考https://help.aliyun.com/d ...

  7. Android实现第三方Mob短信验证登录以及手机号显示和修改用户名(简易新闻 五)

    Android实现第三方Mob短信验证登录以及手机号显示和修改用户名(简易新闻 五) 关于之前的功能实现可以从制作简易新闻App导航篇中查看 因为这篇是写了有一段时间的博客,所以这篇(包括后面的简易新 ...

  8. java给第三方接口发送数据_对接第三方接口--使用post请求发送json数据

    对接第三方接口–使用post请求发送json数据 实习4个多月,终于转正!终于可以安心好好上班,好好学习!第一篇播客记录下工作中的中的小知识点. 本文记录的内容如下: 1.使用HttpClient相关 ...

  9. java对接阿里云短信服务详解(验证码,推广短信,通知短信)

    前言 小前提: - java:springboot框架,maven版本管理. - 阿里云:有账号,已经进行实名认证. java对接阿里云短信服务详解(验证码,推广短信,通知短信) 前言 1. 登录阿里 ...

最新文章

  1. properties 资源文件读取
  2. 清分日期是当天还是第二天_年底了,您的驾驶证是该清分了吗?
  3. 走进WebApiClientCore的设计
  4. .NET Core 2.1中的分层编译(预览)
  5. PHP学习总结(6)——PHP入门篇之PHP语句结束符
  6. android与ndk交互,NDK-JNI与Java的交互 hello-world
  7. webpack3快速入门
  8. jQuery之筛选函数
  9. ArcGIS操作小技巧(五)之色带-----横向(水平)图例
  10. 山大商院java课程_山大商院必读书目推荐——《社会科学方法论》
  11. QT的自动滚动区QScrollArea的用法,图文详解
  12. 拒做背锅侠!如何利用网站性能优化驱动产品体验提升
  13. AD18 SCH Filter面板——智能查找功能
  14. Prolog实现太阳系星体识别专家系统
  15. Nginx 菜鸟教程从初学到应用
  16. 有关刚开始学习Unity的心得
  17. SprinBoot+Jpa实现1024社区系统的最新职位推荐和最热职位推荐
  18. Java 用户评价可信度计算
  19. 威纶通触摸屏学习资料,附带软件eb8000和eb pro助你熟练掌握应用威纶通触摸屏
  20. TUTK摄像头配网方式之AP模式配网

热门文章

  1. C语言输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。
  2. 瑞柏匡丞:你是否希望“被遗忘”
  3. 自动控制原理6.1---系统的设计与校正问题
  4. 基于51单片机智能加湿器(程序+仿真+全套资料)
  5. vslam初总结报告
  6. 浅谈自定义类型-枚举
  7. c 语言字体怎么改,VC++中的字体设置方法详解
  8. 腰带“兄弟”事实上,投资
  9. GNSS位移边坡监测站 尾矿库安全监测仪 山体滑坡卫视
  10. 各个大厂面试题(附答案)