RefundTransaction


/**
 * @author bcoffman
 * who gratefully acknowledges the initial contribution of gyue,rtanaka and schen.
 *
 * java RefundTransaction <transaction_id>
 *
 */

import java.net.URL;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.MessageElement;
import org.apache.axis.AxisFault;
import org.apache.axis.client.*;
import java.io.*;
import java.util.*;
import com.paypal.api.client.*;        //PayPal Library, See the src directory.
import eBLBaseComponents.apis.ebay.*;  // eBay Business Logic definition, from eBL_* wsdl.
import PayPalAPI.api.ebay.*;  // Based on PayPalSvc.wsdl.
import CoreComponentTypes.apis.ebay.*;


public class RefundTransaction
{
    public static void main(String[] args) throws Exception
    {
        if (args.length < 1)
        {
                System.out.println("Usage: java RefundTransaction <TXN-ID> [<MEMO>]");
                return;
        }
        //
        RefundTransaction client = new RefundTransaction();
        ReadArgs a = new ReadArgs("h:s u:s p:s k:s w:s v:s",args);
        if (!a.ok()) usage();
        Hashtable h = a.hash();
        String[] argv = a.args();
        if (argv.length>2 || argv.length<1) {
            System.out.println("Usage: java RefundTransaction <TXN-ID> [<MEMO>]");
            return;
        }
        String memo = null;
        if (argv.length==2) {memo=argv[1];}
        SimpleConfigFile cfg = new SimpleConfigFile("client_config.nvp");

        if (a.contains("h")) {cfg.set("url",(String)a.val("h"));}
        if (a.contains("u")) {cfg.set("user_name",(String)a.val("u"));}
        if (a.contains("p")) {cfg.set("user_passwd",(String)a.val("p"));}
        if (a.contains("k")) {cfg.set("cert_file",(String)a.val("k"));}
        if (a.contains("w")) {cfg.set("cert_password",(String)a.val("w"));}
        if (a.contains("v")) {cfg.set("refund_version",(String)a.val("v"));}
        client.runRefundTransaction(argv[0],memo,cfg);
    }

    public RefundTransaction() {}

    public static void usage() {
        System.out.println("usage: java <action> [options] <txn-id>");
        System.out.println("  Options:");
        System.out.println("     -f config-file   Default: client_config.nvp in current directory.");
        System.out.println("                      Command line options override this file.");
        System.out.println("     -h url");
        System.out.println("     -u username");
        System.out.println("     -p password");
        System.out.println("     -k keystore-file (as in *.p12, aka PKCS12)");
        System.out.println("     -w keystore-paswd");
        //System.out.println("     -v version (as in "1.0" -- currently not supported by axia 1.1)");
        System.out.println("     -v version ");
        System.out.println("     -e ");
        System.out.println("  actions:");
        System.out.println("     RefundTransaction <transaction-id> [<memo>]");
        System.out.println("     GetTxnDetails <transaction-id>");
        System.exit(1);
    }

    public void runRefundTransaction(String txn_id, String memo, SimpleConfigFile cfg) throws Exception {
        // First, we load the config file, and build a request object...
        String url = cfg.val("url");
        System.out.println("connecting to /""+url+"/": testing RefundTransaction(/""+txn_id+"/")");
 
        //PpapiPortType binding = null;
        PayPalAPIInterface binding = null;
        try {
            binding = new PayPalAPIInterfaceServiceLocator().getPayPalAPI(new URL(url));
            ((PayPalAPISoapBindingStub)binding).setTimeout(60000);
        }
        catch(Exception e) {
     org.apache.axis.AxisFault afe = null;
     if (e instanceof org.apache.axis.AxisFault) {
  afe = (org.apache.axis.AxisFault) e;
     }
     String note = afe.getFaultString();
     System.out.println("Got AxisFault exception " + note);
     e.printStackTrace();
           // throw AxisFault.makeFault(e);
     return;
        }

        RefundTransactionRequestType request = new RefundTransactionRequestType();

        request.setVersion(cfg.val("refund_version")); // A bug in axis 1.1 causes this to fail.
 
 String refund_type = cfg.val("refund_is");
 if (refund_type.length() != 0)
     request.setRefundType(eBLBaseComponents.apis.ebay.RefundPurposeTypeCodeType.fromString(refund_type));

 String amount = new String(cfg.val("refund_amount"));
 String currencyID = new String(cfg.val("refund_currency_code"));

 if (amount.length() != 0) {
     CoreComponentTypes.apis.ebay.BasicAmountType amountType = new CoreComponentTypes.apis.ebay.BasicAmountType(amount);
     request.setAmount(amountType);

     if (currencyID.length() != 0)
      amountType.setCurrencyID(eBLBaseComponents.apis.ebay.CurrencyCodeType.fromString(currencyID));
 }

 if (memo == null) {
     memo = cfg.val("refund_memo");
 }
 if (memo.length() != 0)
     request.setMemo(memo);
 
        //String curr_name = new String(cfg.val("refund_currency_code"));
        request.setTransactionID(new eBLBaseComponents.apis.ebay.TransactionId(txn_id));

        _RefundTransactionReq _req = new _RefundTransactionReq();
        _req.setRefundTransactionRequest(request);


        // Use proxy settings if set in config file...
        if (cfg.val("proxy_host") != null) {
            System.setProperty("http.proxyHost",cfg.val("proxy_host"));
            if (cfg.val("proxy_host") != null) {
                System.setProperty("http.proxyPort",cfg.val("proxy_port"));
            } else {
                System.setProperty("http.proxyPort","80");
            }
        }

 // Insert ebl security ...
 PrefixedQName pqn = new PrefixedQName("urn:ebay:api:PayPalAPI",
      "RequesterCredentials", "");
 SOAPHeaderElement eblSecurity = new SOAPHeaderElement(pqn); 
 eblSecurity.setMustUnderstand(true);
 
 MessageElement credentials = new MessageElement("Credentials", "ebl", "urn:ebay:apis:eBLBaseComponents");

 MessageElement username = new MessageElement("", "ebl:Username");
 MessageElement password = new MessageElement("", "ebl:Password");
 MessageElement subject = new MessageElement("", "ebl:Subject");
 username.setObjectValue(cfg.val("user_name"));
 credentials.addChild(username);
 password.setObjectValue(cfg.val("user_passwd"));
 credentials.addChild(password);
 subject.setObjectValue(cfg.val("user_subject"));
 credentials.addChild(subject);
 eblSecurity.addChild(credentials);
 org.apache.axis.client.Stub s = (Stub)binding;
 s.setHeader(eblSecurity);
 

        // Below allows SSL connections...
        org.apache.axis.AxisProperties.setProperty("axis.socketSecureFactory",
                                                   "com.paypal.api.client.SSLFactory");

 
 //Print the request object
 printRefundReq(request);

        // This actually does the transaction ...
 //RefundTransactionResponse resp = null;
 RefundTransactionResponseType resp = null;
 try {
         resp = binding.refundTransaction(_req);
 } catch(Exception e) {
  org.apache.axis.AxisFault afe = null;
  if (e instanceof org.apache.axis.AxisFault){
   afe = (org.apache.axis.AxisFault)e;
  }
  String note = afe.dumpToString();
  System.out.println("Failed on transaction: " + note); 
  //e.printStackTrace();
  return;
 }

 //process the response...
 printRefundRes(resp);

    }

    public void printRefundReq(RefundTransactionRequestType req) {
        System.out.println();
        System.out.println("RefundTransaction Request ---------");
 
        System.out.println("transactionID: " + req.getTransactionID().toString());
 if (req.getRefundType() != null)
            System.out.println("refundType: " + req.getRefundType().toString());
 if (req.getAmount()!= null) {
            System.out.println("amount: " + req.getAmount().toString());
     if (req.getAmount().getCurrencyID() != null)
             System.out.println("currency: " + req.getAmount().getCurrencyID().toString());
 }

 if (req.getMemo() != null)
            System.out.println("memo:  " + req.getMemo().toString());
 if (req.getVersion() != null)
     System.out.println("version: " + req.getVersion().toString());
 
    }

    //public void printRefundRes(RefundTransactionResponse res) {
    public void printRefundRes(RefundTransactionResponseType res) {
        // Process the response...
        System.out.println();
        System.out.println("RefundTransaction Response ---------");
        System.out.println("Ack:  " + res.getAck().toString());
        System.out.println("TimeStamp:  " + res.getTimestamp().getTime());
 if (res.getCorrelationID() != null)
            System.out.println("CorrelationID: " + res.getCorrelationID().toString());
 if (res.getVersion() != null)
            System.out.println("Version:  " + res.getVersion().toString());
 if (res.getBuild() != null)
            System.out.println("Build:  " + res.getBuild().toString());

        eBLBaseComponents.apis.ebay.ErrorType err[] = res.getErrors();
        for (int i=0; err!=null&&i<err.length; i++) {
            System.out.println();
            System.out.println("Error Code: " + err[i].getErrorCode());
            System.out.println("Short mssg: " + err[i].getShortMessage());
            System.out.println("Long messg: " + err[i].getLongMessage());
            System.out.println("Severity:   " + err[i].getSeverityCode());
 }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章