Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Security header is not valid' using PayPal sandbox in .NET

I am using the PayPal sandbox in ASP.Net C# 4.0. I added the following web references:

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

When I run this code:

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);

In my resp object, the error message says:

Security header is not valid

I was told to give it correct API credentials. I signed up on developer.paypal.com and i'm assuming the email address and password i used are my valid credentials. How and where do I give it my API credentials? Thanks

like image 894
BoundForGlory Avatar asked Sep 18 '12 12:09

BoundForGlory


People also ask

How do I fix my PayPal Security header is not valid?

Cause & Resolution The error message means that the API credentials you have entered are not exactly the same as what they have on file for you. To correct, go to Settings > Card Options > PayPal you should see the API credentials there, compare them to your PayPal ones.

What does security header is not valid mean?

The “Security header is not valid” error message is only caused for two reasons: Wrong Credentials. Make sure that you've put your API Username, API Password and API Signature correctly. Sometimes it happens that during copy and paste there is accidentally a space added, this would trigger this error.

What is the difference between sandbox and live PayPal?

PayPal Sandbox is a virtual testing environment that mimics the live PayPal production environment. It works similarly to making an actual PayPal but without using real credit cards or live PayPal accounts.


1 Answers

Did you check the endpoint addresses in your web.config file

Those should be referenced to following url's

For API Certificate => SOAP https://api.sandbox.paypal.com/2.0/

For API Signature => SOAP https://api-3t.sandbox.paypal.com/2.0/

If you are using Signature then use the following code

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"]
            };

To get Paypal signature follow the link

For more info click here

Update:

Please check the following code it is working for me

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));
like image 148
Raghuveer Avatar answered Sep 30 '22 20:09

Raghuveer