H5网站接入Paypal支付接口

本文详述了如何在H5网站上接入Paypal支付接口,通过使用Paypal的REST API,介绍了从下载Paypal DLL到配置Web.config,以及处理支付请求和IPN来更新订单状态的步骤。
摘要由CSDN通过智能技术生成

写本文章的目的是为了记录工作中遇到的问题,方便以后遇到可以迅速解决问题

paypal支付接口实现由几种方法,我使用的是REST API

所以在这里值介绍这种方法的使用

我们使用vs的Nuget下载Paypal的dll

首先需要配置Web.config文件

1.在configuration节点下配置configsections中的section节点

2.在configuration节点下配置paypal节点

<configuration> 
<configSections> <!--IMPORTANT: Make sure you add the configSections node to your config file so that the rest of the config file is picked by the application--> <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" /> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>
  <paypal>
    <settings>
      <!-- Replace the mode to `security-test-sandbox` to test if your server supports TLSv1.2. For more information follow README instructions.-->
      <add name="mode" value="sandbox" />
      <add name="connectionTimeout" value="360000" />
      <add name="requestRetries" value="1" />
      <add name="clientId" value="ATnMWGSD5FAe8_kd5eA83ZN5V5jiRY57CCqYUG6We3uDrIfZNSolS_CZ0DK2I74yf03IEcN8HgMIl3Md" />
      <add name="clientSecret" value="ENW9z0srkaWrowK5BQif52xHJoDoTaqZU38bz-y0mAz37sHDD9okHubjPcr0gmkZtlR9MbVuMshl_hyX" />
    </settings>
  </paypal>
</configuration>

clientid,和clientSecret的值需要自己在沙箱中配置,才能得到

用户点击付款按钮时,调用这个方法

引用命名空间

using PayPal.Api;

 public ActionResult PaypalPayment(string orderNo)
        {
            try
            {
                var config = ConfigManager.Instance.GetProperties();//读取配置文件
                var accessToken = new OAuthTokenCredential(config).GetAccessToken();
                var apiContext = new APIContext(accessToken);
                string payerId = Request.Params["PayerID"];//用户登录账号付款后,这个payerid才有值
                if (string.IsNullOrEmpty(payerId))//还没有登陆付款
                {
                    PaypalTradeWayPayServer pay = new PaypalTradeWayPayServer();
                    var approvalUrl = pay.payment(orderNo, Request, Session, apiContext);//发送给paypal的参数方法
                    if (!string.IsNullOrEmpty(approvalUrl))
                    {
                        return Redirect(approvalUrl);
                    }
                    else
                    {
                        return RedirectToAction("Error404", "MPublic", null);
                    }
                }
                else
                {
    
var guid = Request.Params["guid"];//登陆付款后才进入这里 // Using the information from the redirect, setup the payment to execute. var paymentId = Session[guid] as string; var paymentExecution = new PaymentExecution() { payer_id = payerId }; var payment = new Payment() { id = paymentId }; // Execute the payment. var executedPayment = payment.Execute(apiContext, paymentExecution); ViewBag.result = "success"; } } catch (PayPal.PayPalException ex) { //插入错误日志 } return View("PayResult"); }

payment方法:

具体参数请自行查看

/// <summary>
        /// 得到ItemList
        /// </summary>
        /// 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值