写本文章的目的是为了记录工作中遇到的问题,方便以后遇到可以迅速解决问题
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> ///