【问题标题】:'Security header is not valid' using PayPal sandbox in .NET在 .NET 中使用 PayPal 沙箱的“安全标头无效”
【发布时间】:2012-09-10 17:59:24
【问题描述】:

我在 ASP.Net C# 4.0 中使用 PayPal 沙箱。我添加了以下网络参考:

https://www.sandbox.paypal.com/wsdl/PayPalSvc.wsdl
https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl 

当我运行这段代码时:

PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutReq req = new PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutReq()
        {
            SetExpressCheckoutRequest = new PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutRequestType()
            {
                Version = Version,
                SetExpressCheckoutRequestDetails = reqDetails
            }
        };

        // query PayPal and get token
        PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutResponseType resp = BuildPayPalSandboxWebservice().SetExpressCheckout(req);

在我的 resp 对象中,错误消息说:

安全标头无效

我被告知要给它正确的 API 凭据。我在 developer.paypal.com 上注册,并假设我使用的电子邮件地址和密码是我的有效凭据。我如何以及在哪里向它提供我的 API 凭据?谢谢

【问题讨论】:

  • 您使用的是证书授权还是基于签名的授权?
  • 我在 .Net 中使用基于证书的授权使用 Paypal API 时遇到了类似的问题,我之前给出的答案可能会有所帮助 - stackoverflow.com/a/11720512/969613

标签: c# paypal-sandbox


【解决方案1】:

您是否检查了 web.config 文件中的端点地址

这些应该参考以下网址

对于 API 证书 => SOAP https://api.sandbox.paypal.com/2.0/

对于 API 签名 => SOAP https://api-3t.sandbox.paypal.com/2.0/

如果您使用的是签名,请使用以下代码

CustomSecurityHeaderType type = new CustomSecurityHeaderType();
            type.Credentials = new UserIdPasswordType()
            {
                Username = ConfigurationManager.AppSettings["PayPalUserName"], //Not paypal login username
                Password = ConfigurationManager.AppSettings["PayPalPassword"], //not login password
                Signature = ConfigurationManager.AppSettings["PayPalSignature"]
            };

要获得 Paypal 签名,请关注link

欲了解更多信息click here

更新:

请检查以下对我有用的代码

CustomSecurityHeaderType type = new CustomSecurityHeaderType();
            type.Credentials = new UserIdPasswordType()
            {
                Username = ConfigurationManager.AppSettings["PayPalUserName"],
                Password = ConfigurationManager.AppSettings["PayPalPassword"],
                Signature = ConfigurationManager.AppSettings["PayPalSignature"]
            };


            PaymentDetailsItemType[] pdItem = new PaymentDetailsItemType[1];
            pdItem[0] = new PaymentDetailsItemType() 
            { 
                Amount = new BasicAmountType(){currencyID = CurrencyCodeType.USD,Value = ItemPrice},
                Name = ItemName,
                Number = ItemNumber,
                Description = ItemDescription, 
                ItemURL = ItemUrl
            };

            SetExpressCheckoutRequestDetailsType sdt = new SetExpressCheckoutRequestDetailsType();
            sdt.NoShipping = "1";
            PaymentDetailsType pdt = new PaymentDetailsType()
            {
                OrderDescription = OrderDesc,
                PaymentDetailsItem = pdItem,
                OrderTotal = new BasicAmountType()
                {                    
                    currencyID = CurrencyCodeType.USD,
                    Value = ItemPrice
                }
            };

            sdt.PaymentDetails = new PaymentDetailsType[] { pdt };
            sdt.CancelURL = "http://localhost:62744/PaymentGateway/PaymentFailure.aspx";
            sdt.ReturnURL = "http://localhost:62744/PaymentGateway/ExpressCheckoutSuccess.aspx";

            SetExpressCheckoutReq req = new SetExpressCheckoutReq()
            {
                SetExpressCheckoutRequest = new SetExpressCheckoutRequestType()
                {
                    SetExpressCheckoutRequestDetails = sdt,
                    Version = "92.0"
                }
            };
            var paypalAAInt = new PayPalAPIAAInterfaceClient();
            var resp = paypalAAInt.SetExpressCheckout(ref type, req);
            if (resp.Errors != null && resp.Errors.Length > 0)
            {
                // errors occured
                throw new Exception("Exception(s) occured when calling PayPal. First exception: " +
                    resp.Errors[0].LongMessage);
            }

            Response.Redirect(string.Format("{0}?cmd=_express-checkout&token={1}",
                ConfigurationManager.AppSettings["PayPalOriginalUrl"], resp.Token));

【讨论】:

  • 感谢您的回复。我将这些端点放在 web.config 的什么位置?我从未输入过 Web 服务的端点。我拥有的 WCF 服务。
  • @user1202717 创建服务引用时,Visual Studio 将创建端点,检查这些端点是否符合您的要求?
  • 感谢您提供此信息...沙盒 API 凭据是否与 PayPalAPI 凭据相同?
  • 否,您使用哪种身份验证机制进行 API 调用?是基于签名还是证书?
  • 这正是我想要的。我喜欢直接给我答案。非常感谢!!!!!!
猜你喜欢
  • 2016-01-24
  • 2015-06-18
  • 2014-06-23
  • 2015-11-26
  • 2017-11-09
  • 2014-04-05
  • 2014-01-26
  • 2012-01-14
  • 2012-01-04
相关资源
最近更新 更多