Eagle
Eagle

Reputation: 31

Error in C# WinForm Paypal integration with asmx web service

I am trying to integrate Paypal payments in a .NET 4.0 C# WinForm application doing a DirectPayment (without opening the browser with the paypal site) trought an asmx webservices calling this method from a WebMethod

public DoDirectPaymentResponseType DoDirectPaymentAPIOperation(string 
    amountinUSDollar, string creditCardType, string creditCardNumber, int 
    exp_month, int exp_year, string firstName, string middleName, string 
    lastName, string address1, string address2, string city, string state, 
    string zip, string phoneNumber, string CVV2)
{
    DoDirectPaymentResponseType response = new 
    DoDirectPaymentResponseType();
    DoDirectPaymentReq wrapper = new DoDirectPaymentReq();
    DoDirectPaymentRequestType request = new DoDirectPaymentRequestType();
    DoDirectPaymentRequestDetailsType requestDetails = new 
        DoDirectPaymentRequestDetailsType();
    requestDetails.PaymentAction = (PaymentActionCodeType)               
        Enum.Parse(typeof(PaymentActionCodeType), "SALE");
    CreditCardDetailsType creditCard = new CreditCardDetailsType();
    PayerInfoType cardOwner = new PayerInfoType();
    PersonNameType payer = new PersonNameType();
    payer.FirstName = firstName;
    payer.MiddleName = middleName;
    payer.LastName = lastName;
    cardOwner.PayerName = payer;
    creditCard.CardOwner = cardOwner;
    creditCard.CreditCardNumber = creditCardNumber; 
    if (creditCardType == "VISA")
    {
        creditCard.CreditCardType = CreditCardTypeType.VISA;
    }
    else if (creditCardType == "MasterCard")
    {
        creditCard.CreditCardType = CreditCardTypeType.MASTERCARD;
    }
    creditCard.CVV2 = CVV2;
    creditCard.ExpMonth = exp_month;
    creditCard.ExpYear = exp_year;
    requestDetails.PaymentDetails = new PaymentDetailsType();
    requestDetails.PaymentDetails.NotifyURL = "http://IPNhost";
    BasicAmountType paymentAmount = new 
        BasicAmountType(CurrencyCodeType.USD, amountinUSDollar);
    requestDetails.PaymentDetails.OrderTotal = paymentAmount;
    wrapper.DoDirectPaymentRequest = request;
    Dictionary<string, string> config = getConfigDict();
    PayPalAPIInterfaceServiceService service = new 
        PayPalAPIInterfaceServiceService(config);
    response = service.DoDirectPayment(wrapper);
}

When it try to make the call to the API, in the last line of the code I get this error: "Exception in HttpConnection Execute: Invalid HTTP response The request was aborted: Could not create SSL/TLS secure channel."} System.Exception {PayPal.Exception.PayPalException}.

I had a .NET 4.5 WebForm web application with Paypal integrated in which I had to define this

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

in its Global.asax file and it solves the problem for that specific application and I have found I have to use this

ServicePointManager.SecurityProtocol = (SecurityProtocolType) 3072; 

for .NET 4.0 but my question is:

If this would be the solution to may problem, where should I put this line in my code? Because there is no Global.asax in my asmx webservice.

Upvotes: 1

Views: 91

Answers (1)

DiskJunky
DiskJunky

Reputation: 4971

Outlining the source of the reported issue above as per the comments in the OP. The exception was thrown because the NotifyURL wasn't in https.

Upvotes: 1

Related Questions