public string SetExpressCheckout(PayPalInformation payPalInformation, string payPalSubmitUrl, string payPalCancelUrl) { string result = string.Empty; SetExpressCheckoutRequestType SetECReqType = new SetExpressCheckoutRequestType(); SetExpressCheckoutRequestDetailsType SetECReqTypeDetails = new SetExpressCheckoutRequestDetailsType(); SetExpressCheckoutResponseType SetECRes = new SetExpressCheckoutResponseType(); SetExpressCheckoutReq SetECReq = new SetExpressCheckoutReq(); SetECReqType.Version = "53.0"; SetECReqTypeDetails.OrderTotal = new BasicAmountType(); SetECReqTypeDetails.OrderTotal.currencyID = CurrencyCodeType.GBP; SetECReqTypeDetails.OrderTotal.Value = payPalInformation.Order.SubTotal.ToString(); //order.OrderTotal = GetAmountValue(payment.GrossAmount); //order.Tax = GetAmountValue(payment.TaxAmount); SetECReqTypeDetails.ReturnURL = ConfigurationManager.AppSettings["PayPalReturnURL"]; SetECReqTypeDetails.CancelURL = ConfigurationManager.AppSettings["PayPalCancelURL"]; SetECReqType.SetExpressCheckoutRequestDetails = SetECReqTypeDetails; SetECReq.SetExpressCheckoutRequest = SetECReqType; UserIdPasswordType user = new UserIdPasswordType(); user.Username = this.APIUsername; user.Password = this.APIPassword; user.AuthCert = CertPath; PayPalAPIAASoapBinding ppInterface = new PayPalAPIAASoapBinding(); ppInterface.Url = this.APIPath; ppInterface.RequesterCredentials = new CustomSecurityHeaderType(); ppInterface.RequesterCredentials.Credentials = user; //this is .net 2.0 specific portion of the code //that allows us to have the .p12 on the filesystem and //not have to register it with WinHttpCertCfg uses //X509Certificate2 class. FileStream fStream = File.Open(CertPath, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fStream.Length]; int count = fStream.Read(buffer, 0, buffer.Length); fStream.Close(); //use .net 2.0 X509Certificate2 class to read .p12 from filesystem //where "12345678" is the private key password X509Certificate2 x509 = new X509Certificate2(buffer, CertPassword); ppInterface.ClientCertificates.Add(x509); //service.ClientCertificates.Add(x509); try { SetECRes = ppInterface.SetExpressCheckout(SetECReq); switch (SetECRes.Ack) { case AckCodeType.Success: result = SetECRes.Token; break; default: // show errors if Ack is NOT Success result = "API response: <b>" + SetECRes.Ack.ToString() + "</b><br> Timestamp: <b>" + SetECRes.Timestamp.ToLongTimeString() + "</b><br> Version: <b>" + SetECRes.Version.ToString() + "</b><br> Error code: <b>" + SetECRes.Errors[0].ErrorCode + "</b><br> Short error: <b>" + SetECRes.Errors[0].ShortMessage + "</b><br> Long error: <b>" + SetECRes.Errors[0].LongMessage; break; } } catch (Exception ex) { result = ex.ToString(); } return result; }
public PayPalInformation GetExpressCheckoutDetails(PayPalInformation payPalInformation, string token) { GetExpressCheckoutDetailsResponseType GetECDetailsRes = new GetExpressCheckoutDetailsResponseType(); GetExpressCheckoutDetailsRequestType GetECDetailsReqType = new GetExpressCheckoutDetailsRequestType(); GetExpressCheckoutDetailsReq GetECDetailsReq = new GetExpressCheckoutDetailsReq(); GetECDetailsReqType.Version = "53.0"; GetECDetailsReqType.Token = token; GetECDetailsReq.GetExpressCheckoutDetailsRequest = GetECDetailsReqType; PayPalAPIAASoapBinding PPInterface = new PayPalAPIAASoapBinding(); UserIdPasswordType user = new UserIdPasswordType(); user.Username = this.APIUsername; user.Password = this.APIPassword; user.AuthCert = CertPath; PPInterface.Url = this.APIPath; PPInterface.RequesterCredentials = new CustomSecurityHeaderType(); PPInterface.RequesterCredentials.Credentials = new UserIdPasswordType(); PPInterface.RequesterCredentials.Credentials = user; FileStream fStream = File.Open(CertPath, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fStream.Length]; int count = fStream.Read(buffer, 0, buffer.Length); fStream.Close(); X509Certificate2 x509 = new X509Certificate2(buffer, CertPassword); PPInterface.ClientCertificates.Add(x509); //PayPalInformation PPInformation = new PayPalInformation(); GetECDetailsRes = PPInterface.GetExpressCheckoutDetails(GetECDetailsReq); if (GetECDetailsRes.Ack == AckCodeType.Success) { payPalInformation.Order.Enduser.FirstName = GetECDetailsRes.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.FirstName; payPalInformation.Order.Enduser.LastName = GetECDetailsRes.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.LastName; payPalInformation.Order.ShippingAddress.AddressLine = GetECDetailsRes.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street1; payPalInformation.Order.ShippingAddress.AddressLine2 = GetECDetailsRes.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street2; payPalInformation.Order.ShippingAddress.City = GetECDetailsRes.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CityName; payPalInformation.Order.ShippingAddress.PostalCode = GetECDetailsRes.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.PostalCode; payPalInformation.Order.PayerId = GetECDetailsRes.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerID; } return payPalInformation; }