Authorize.net Java示例

时间:2015-05-06 21:24:42

标签: java authorize.net

我正在寻找开发简单的Java程序,该程序将发送请求并从“Authorize.net”获得响应,如授权,捕获,撤销,退款交易等。我已在authorized.net创建了我的测试帐户 - { {3}}。我做了大量的一周,并没有找到任何示例作为参考。

我正在使用AIM进行开发,假设我有AuthTransaction的代码

GenericValue cc = (GenericValue) params.get("creditCard");
        String currency = (String) params.get("currency");
        String amount = ((BigDecimal)params.get("processAmount")).toString();
        String number = UtilFormatOut.checkNull(cc.getString("cardNumber"));
        String expDate = UtilFormatOut.checkNull(cc.getString("expireDate"));
        String cardSecurityCode = (String) params.get("cardSecurityCode");
        AIMRequest.put("x_Amount", amount);
        AIMRequest.put("x_Currency_Code", currency);        
        AIMRequest.put("x_Method", props.getProperty("method"));
        AIMRequest.put("x_Type", props.getProperty("transType"));
        AIMRequest.put("x_Card_Num", number);
        AIMRequest.put("x_Exp_Date", expDate);
        if (UtilValidate.isNotEmpty(cardSecurityCode)) {
            AIMRequest.put("x_card_code", cardSecurityCode);
        }
        if (AIMRequest.get("x_market_type") != null) {
            AIMRequest.put("x_card_type", getCardType(UtilFormatOut.checkNull(cc.getString("cardType"))));
        }

但我不知道如何拨打电话请求并获得回复。有人请指导我/提供示例代码吗?

1 个答案:

答案 0 :(得分:1)

是的,Authorize.net确实提供了来自http://mvnrepository.com/artifact/net.authorize/anet-java-sdk的Java SDK和maven依赖。

Authorize.Net SDK包括标准付款,定期结算和客户资料。

我通过遵循Java代码

执行简单的AuthCapture事务
package com.auth.net;

import java.math.BigDecimal;

import net.authorize.Environment;
import net.authorize.Merchant;
import net.authorize.TransactionType;
import net.authorize.aim.Result;
import net.authorize.aim.Transaction;
import net.authorize.data.creditcard.CreditCard;

public class AIMauthCaptureTransactionDemo {
    public static final String apiLoginID= "Your Sandbox API Login ID";
    public static final String transactionKey= "Your Sandbox API Transaction Key";

    public static void main(String[] args) {
        Merchant merchant = Merchant.createMerchant(Environment.SANDBOX, apiLoginID, transactionKey);
        // create credit card
        CreditCard creditCard = CreditCard.createCreditCard();
        creditCard.setCreditCardNumber("4111 1111 1111 1111");
        creditCard.setExpirationMonth("12");
        creditCard.setExpirationYear("2018");

        // create transaction
        Transaction authCaptureTransaction = merchant.createAIMTransaction
                (TransactionType.AUTH_CAPTURE, new BigDecimal("5.00"));

        authCaptureTransaction.setCreditCard(creditCard);

        @SuppressWarnings("unchecked")
        Result<Transaction> result = (Result<Transaction>)merchant.postTransaction(authCaptureTransaction);

        if(result.isApproved()) {
            System.out.println("Response Code : "+ result.getReasonResponseCode());
            System.out.println("Response Text : " + result.getResponseText());
            System.out.println("Transaction Id: " + result.getTarget().getTransactionId());
            System.out.println("AuthorizationCode : "+result.getTarget().getAuthorizationCode());
        } 
        else if (result.isDeclined()) {
            System.out.println(result.getReasonResponseCode() + " : " + result.getResponseText());
        }
        else {
            System.out.println(result.getReasonResponseCode() + " : " + result.getResponseText());
        }
    }

}

这里,请参考下面的屏幕截图中我之前执行过的第一笔交易

enter image description here