Jump to content


Photo

Refund API Script (Class.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:10 PM

using System;
using System.Runtime;
using RefundClient.RefundTransaction;

namespace RefundClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
TestClient test = new TestClient();

int result = test.Run( ref args );

return result;
}
}

public class TestClient
{
RefundTransactionReq req;
string sUserName;
string sPassword;
string sUrl;
string sProxy;
string sCertFile;
bool bWaitForEnter;

public TestClient()
{
// Real defaults that can stay normal
req = new RefundTransactionReq();
req.RefundTransactionRequest = new RefundTransactionRequestType();

req.RefundTransactionRequest.version = "1.0";

req.RefundTransactionRequest.RefundType = RefundTransaction.RefundPurposeTypeCodeType.Full;

req.RefundTransactionRequest.Amount = new RefundTransaction.BasicAmountType();
req.RefundTransactionRequest.Amount.currencyID = "USD";
req.RefundTransactionRequest.Amount.Value = 0; //123.45M;

sUserName = "sUserName";

sPassword = "sPassword";

sUrl = "https://api.sandbox.paypal.com/2.0/";
sProxy = "http://sandbox.paypal.com:8080";

sCertFile = "certificate.cer"; // @"C:\Projects\ppapitest\rtanaka.cer";

bWaitForEnter = false;

// Defaults that MUST be overridden.
req.RefundTransactionRequest.TransactionID = "BAD";
}

public int Run( ref string[] args )
{
if (!ParseArgs( ref args, ref req ))
{
return -1;
}

if (!ValidateArgs( ref req ) )
{
return -1;
}

TryTransaction();
return 0;
}

bool ParseArgs(ref string[] args, ref RefundTransactionReq req )
{
int argc = args.Length;
#region Argument Parsing
for(int i = 0; i < argc; i++)
{
if ('-' == args[i][0])
{
switch( args[i][1] )
{
case 'u':
if ( ++i < argc )
sUserName = args[i];
else
argc = 0;
break;
case 'p':
if ( ++i < argc )
sPassword = args[i];
else
argc = 0;
break;
case 'h':
if ( ++i < argc )
sUrl = args[i];
else
argc = 0;
break;
case 'x':
if ( ++i < argc )
sProxy = args[i];
else
argc = 0;
break;
case 'c':
if ( ++i < argc )
sCertFile = args[i];
else
argc = 0;
break;
case 'v':
if ( ++i < argc )
req.RefundTransactionRequest.version = args[i];
else
argc = 0;
break;
case 't':
if ( ++i < argc )
req.RefundTransactionRequest.TransactionID = args[i];
else
argc = 0;
break;
case 'r':
if ( ++i < argc )
{
if ( args[i][0] == 'P' || args[i][0] == 'p' )
req.RefundTransactionRequest.RefundType = RefundTransaction.RefundPurposeTypeCodeType.Partial;
else
req.RefundTransactionRequest.RefundType = RefundTransaction.RefundPurposeTypeCodeType.Full;
}
else
argc = 0;
break;
case 'a':
if ( ++i < argc )
req.RefundTransactionRequest.Amount.Value = Convert.ToDecimal(args[i]);
else
argc = 0;
break;
case '$':
if ( ++i < argc )
req.RefundTransactionRequest.Amount.currencyID = args[i];
else
argc = 0;
break;
case 'm':
if ( ++i < argc )
{
req.RefundTransactionRequest.Memo = args[i];
}
else
argc = 0;
break;
case 'w':
bWaitForEnter = true;
break;
default:
argc = 0;
break;
}
}
else
{
argc = 0;
}
}
#endregion

if ( 0 == argc )
{
Console.WriteLine ("----------------------------------------------------------------------------");
Console.WriteLine ("Usage: RefundClient [options] ");
Console.WriteLine (" [-u Username] : Specify username, default: <{0}>", sUserName);
Console.WriteLine (" [-p Password] : Specify password, default: <{0}>", sPassword);
Console.WriteLine (" [-h urlHost] : Specify url to server, default: <{0}>", sUrl);
Console.WriteLine (" [-x proXy] : Specify url to proxy, default: <{0}>", sProxy);
Console.WriteLine (" [-c Certfile] : Specify certificate file, default: <{0}>", sCertFile);
Console.WriteLine (" [-v Version] : Specify version number, default: <{0}>", req.RefundTransactionRequest.version);
Console.WriteLine (" [-t Transact] : Specify transaction ID ( REQUIRED! ) ");
Console.WriteLine (" [-r Reftype] : Specify refund type (Full or Partial), default: <{0}>", req.RefundTransactionRequest.RefundType);
Console.WriteLine (" [-a Amount] : Specify amount of refund, default: <{0}>", req.RefundTransactionRequest.Amount.Value);
Console.WriteLine (" [-$ currency] : Specify currency, default: <{0}>", req.RefundTransactionRequest.Amount.currencyID);
Console.WriteLine (" [-m \"Memo\"] : Specify memo ");
Console.WriteLine (" [-w] : Wait for enter key after running, default: <{0}>", bWaitForEnter);
Console.WriteLine ("----------------------------------------------------------------------------");
}

return ( 0 != argc );
}

bool ValidateArgs( ref RefundTransactionReq req )
{
if ( req.RefundTransactionRequest.TransactionID == "BAD" )
return false;

return true;
}

public void TryTransaction()
{
Refund refund = new Refund();
refund.sUserName = sUserName;
refund.sPassword = sPassword;
refund.sCertFile = sCertFile;
refund.sUrl = sUrl;
refund.sProxy = sProxy;

RefundTransactionResponse response;

refund.TryTransaction( ref req, out response );

Console.WriteLine( "Soap response: <{0}>", response );
Console.WriteLine( "Soap response.Ack <{0}>", response.Ack );
Console.WriteLine( "Soap response.CorrelationID <{0}>", response.CorrelationID );
Console.WriteLine( "Soap response.Timestamp <{0}>", response.Timestamp );
if ( null == response.Errors )
{
Console.WriteLine( "NO ERRORS" );
}
else
{
Console.WriteLine( " Soap: <{0}> Errors", response.Errors.GetLength(0) );
foreach ( ErrorType error in response.Errors )
{
Console.WriteLine( " Soap Error <{0}>", error.ErrorCode );
Console.WriteLine( " Soap Error <{0}>", error.SeverityCode );
Console.WriteLine( " Soap Error <{0}>", error.ShortMessage );
Console.WriteLine( " Soap Error <{0}>", error.LongMessage );
Console.WriteLine( " -----------" );
}
}

if ( bWaitForEnter )
{
Console.WriteLine("Press enter to exit...");
Console.Read();
}
}
}
}


Paulam
Technical Support Agent





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users