Jump to content


Photo

Refund API (Refund.cs)


  • This topic is locked This topic is locked
No replies to this topic

#1 PayPal_Paulam

PayPal_Paulam

    Average Member

  • Members
  • 511 posts

Posted 02 January 2004 - 01:11 PM

#define WS_SECURITY

using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Web.Services.Protocols;

using Microsoft.Web.Services;
using Microsoft.Web.Services.Security;

using RefundClient.RefundTransaction;

namespace RefundClient
{
/// <summary>
/// Summary description for Refund.
/// </summary>
public class Refund
{
public string sUserName;
public string sPassword;
public string sUrl;
public string sProxy;
public string sCertFile;

public Refund()
{
// Adjust these default values to whatever your default should be
sUserName = "sUserName";
sPassword = "sPassword";
sUrl = "https://api.sanbox.paypal.com/2.0/";
sProxy = "http://sandbox.paypal.com:8080";
sCertFile = "certificate.cer";
}

public bool TryTransaction( ref RefundTransactionReq req, out RefundTransactionResponse response )
{
response = new RefundTransactionResponse();
try
{

ServicePointManager.CertificatePolicy = new MyCertificateValidation();

RefundTransaction.ppapiService PayPalAPI = new RefundTransaction.ppapiService();
PayPalAPI.Url = sUrl;

/////////////////////////////////////
// Set up ms-security
UsernameToken token = new UsernameToken(sUserName, sPassword, PasswordOption.SendPlainText);
#if (WS_SECURITY)
SoapContext requestContext = PayPalAPI.RequestSoapContext;
// A compile error here -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^ means you need to
// change the ppapiService to derive from Microsoft.Web.Services.WebServicesClientProtocol

if ( 0 != sProxy.Length )
{
// Configure the proxy
IWebProxy proxyObject = new WebProxy(sProxy, false);
PayPalAPI.Proxy = proxyObject;
}

// Set the TTL to one minute
requestContext.Timestamp.Ttl = 60000;

requestContext.Path.MustUnderstand = true;

// Add the security token and request a signature
requestContext.Security.Tokens.Add(token);
#endif
////////////////////////////////////////////
// Load the client certificate from a file.
X509Certificate x509 = X509Certificate.CreateFromCertFile(sCertFile);

// Add the client certificate to the ClientCertificates property
// of the proxy class.
PayPalAPI.ClientCertificates.Add(x509);

////////////////////////////////////////////
// Do the transaction
response = PayPalAPI.RefundTransaction(req);
}
catch(WebException e)
{
if (e.Status == WebExceptionStatus.TrustFailure)
{
// Code for handling security certificate problems goes here.
}
// Other exception handling goes here
Console.WriteLine( "Web Exception: {0}", e );
return false;
}
catch(SoapHeaderException e)
{
string sException = e.ToString();
Console.WriteLine( "Soap Header Exception: {0}", sException );
}
catch (System.Web.Services.Protocols.SoapException se)
{
Console.WriteLine( "Soap Exception: {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unknown Exception: {0}", e);
}
return true;
}
}

public enum CertificateProblem : uint
{
CertEXPIRED = 0x800B0101,
CertVALIDITYPERIODNESTING = 0x800B0102,
CertROLE = 0x800B0103,
CertPATHLENCONST = 0x800B0104,
CertCRITICAL = 0x800B0105,
CertPURPOSE = 0x800B0106,
CertISSUERCHAINING = 0x800B0107,
CertMALFORMED = 0x800B0108,
CertUNTRUSTEDROOT = 0x800B0109,
CertCHAINING = 0x800B010A,
CertREVOKED = 0x800B010C,
CertUNTRUSTEDTESTROOT = 0x800B010D,
CertREVOCATION_FAILURE = 0x800B010E,
CertCN_NO_MATCH = 0x800B010F,
CertWRONG_USAGE = 0x800B0110,
CertUNTRUSTEDCA = 0x800B0112
}

public class MyCertificateValidation : ICertificatePolicy
{
// Default policy for certificate validation.
public static bool DefaultValidate = false;

public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,
WebRequest request, int problem)
{
bool ValidationResult=false;
Console.WriteLine("Certificate Problem with accessing " +
request.RequestUri);
Console.Write("Problem code 0x{0:X8}",(int)problem);
Console.WriteLine(GetProblemMessage((CertificateProblem)problem));

ValidationResult = DefaultValidate;
// Check for policy common name mismatch.
if (problem == 0 || (uint)problem == 0x800b010f || (uint)problem == 0x800B0109)
ValidationResult = true;

return ValidationResult;
}

private String GetProblemMessage(CertificateProblem Problem)
{
String ProblemMessage = "";
Type t = typeof(CertificateProblem);
String ProblemCodeName = Enum.GetName(t,Problem);
if(ProblemCodeName != null)
ProblemMessage = ProblemMessage + "-Certificateproblem:" +
ProblemCodeName;
else
ProblemMessage = ", Unknown Certificate Problem";
return ProblemMessage;
}

}

}


Paulam
Technical Support Agent





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users