Authorize---支付


1、前置

1、美国项目的一个支付, 这是一个非常优秀的支付, 文档简洁明了, 思路清晰, 接入起来特别让人省心, 尤其是客服真的很棒, 很耐心, 很亲和的感觉, 比起其余的支付这个真的很赞! 官网地址:https://developer.authorize.net/

2、案列采用java编写

3、介绍Authorize订单整合:
      https://developer.authorize.net/api/reference/index.html#payment-transactions-charge-a-credit-card

4、会对一些可能出现的坑,或者说我碰到的坑进行标记

5、介最后贴图验证以及做好后的样子

6、Payment Transactions(支付), Refund a Transaction(退款), Get Transaction Details(查询).
当然他也有捕获和授权操作, 这个没有写, 和前几篇博客差不多

7、代码已上传码云,如果起到帮助还请star一下地址:https://gitee.com/xmaxm/payments_hodgepodge

8、沙箱注册地址:
https://developer.authorize.net/hello_world/sandbox.html

2、准备工作

1、MANEV包:

<!-- authorize支付 -->
<dependency>
    <groupId>net.authorize</groupId>
    <artifactId>anet-java-sdk</artifactId>
    <version>LATEST</version>
</dependency>

2、获取 NAME 和 TRANSACTION_KEY, 登录地址: https://account.authorize.net/

再注册的时候就会有, 如果没有参考如下截图:
在这里插入图片描述


3、订单界面

在这里插入图片描述
Voided: 这个是Void a Transaction, 无效的交易, 我手动处理的

这个就是结算之后的截图:
在这里插入图片描述


4、配置结算时间

待补充截图


3、创建订单并支付:

地址:https://developer.authorize.net/api/reference/index.html#payment-transactions-charge-a-credit-card

public CommonResult authorizePay(String orderNo) {
        // 设置环境沙盒或生产
        if (true) {
            ApiOperationBase.setEnvironment(Environment.SANDBOX);
        } else {
            ApiOperationBase.setEnvironment(Environment.PRODUCTION);
        }
        // 创建带有商家身份验证详细信息的对象
        MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType();
        merchantAuthenticationType.setName(name);
        merchantAuthenticationType.setTransactionKey(transactionKey);

        // 填充付款数据
        PaymentType paymentType = new PaymentType();
        CreditCardType creditCard = new CreditCardType();
        creditCard.setCardNumber("5424000000000015");
        creditCard.setExpirationDate("2020-12");
        creditCard.setCardCode("999");
        paymentType.setCreditCard(creditCard);

        // 设置电子邮件地址(可选)
        CustomerDataType customer = new CustomerDataType();
        customer.setEmail("646236438@qq.com");

        // 创建付款交易对象
        TransactionRequestType txnRequest = new TransactionRequestType();
        // 信用卡充值
        txnRequest.setTransactionType(TransactionTypeEnum.AUTH_CAPTURE_TRANSACTION.value());
        txnRequest.setPayment(paymentType);
        txnRequest.setCustomer(customer);
        txnRequest.setCurrencyCode(currency);
        // 商家分配的采购订单号. 字符串,最多25个字符。
        txnRequest.setPoNumber(orderNo);
        txnRequest.setAmount(new BigDecimal(200).setScale(2, RoundingMode.CEILING));

        //创建API请求并为此特定请求设置参数
        CreateTransactionRequest apiRequest = new CreateTransactionRequest();
        apiRequest.setMerchantAuthentication(merchantAuthenticationType);
        apiRequest.setTransactionRequest(txnRequest);
        // 商家为请求分配的参考ID. 字符串,最多20个字符。
        apiRequest.setRefId(orderNo);

        // 呼叫控制器
        CreateTransactionController controller = new CreateTransactionController(apiRequest);
        controller.execute();

        // 得到回应
        CreateTransactionResponse response = controller.getApiResponse();

        log.info("authorize 支付操作返回结果: " + JSONObject.toJSONString(response));

        // 解析响应以确定结果
        if (response != null) {
            // 如果API响应正常,请继续检查事务响应
            if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
                TransactionResponse result = response.getTransactionResponse();
                if (result.getMessages() != null) {
                    return CommonResult.success("SUCCESS", result.getTransId());
                } else {
                    if (response.getTransactionResponse().getErrors() != null) {
                        return CommonResult.failMessage(500, "Authorize 支付失败", result.getErrors().getError().get(0).getErrorText());
                    }
                }
            } else {
                if (response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null) {
                    return CommonResult.failMessage(500, "Authorize 支付异常", response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
                } else {
                    return CommonResult.failMessage(500, "Authorize 支付异常", response.getMessages().getMessage().get(0).getText());
                }
            }
        } else {
            // 响应为空时显示错误代码和消息
            ANetApiResponse errorResponse = controller.getErrorResponse();
            if (!errorResponse.getMessages().getMessage().isEmpty()) {
                return CommonResult.failMessage(500, "Authorize 支付异常", errorResponse.getMessages().getMessage().get(0).getText());
            }
        }
        return CommonResult.fail(500, "NetworkTimeout");
    }
authorize 支付操作返回结果: {"messages":{"message":[{"code":"I00001","text":"Successful."}],"resultCode":"OK"},"refId":"20201109793223","transactionResponse":{"accountNumber":"XXXX0015","accountType":"MasterCard","authCode":"1HN636","avsResultCode":"Y","cavvResultCode":"2","cvvResultCode":"P","messages":{"message":[{"code":"1","description":"This transaction has been approved."}]},"networkTransId":"2B8VVTWHC0WYKKEV9RGC3R2","refTransID":"","responseCode":"1","testRequest":"0","transHash":"","transHashSha2":"","transId":"40056262861"}}

4、申请退款:

地址:https://developer.authorize.net/api/reference/index.html#payment-transactions-refund-a-transaction

public CommonResult authorizeRefund(String amount, String transId) {
        // 设置环境沙盒或生产
        if (true) {
            ApiOperationBase.setEnvironment(Environment.SANDBOX);
        } else {
            ApiOperationBase.setEnvironment(Environment.PRODUCTION);
        }
        // 创建带有商家身份验证详细信息的对象
        MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType();
        merchantAuthenticationType.setName(name);
        merchantAuthenticationType.setTransactionKey(transactionKey);

        // 填充退款数据
        PaymentType paymentType = new PaymentType();
        CreditCardType creditCard = new CreditCardType();
        String bankCard = "5424000000000015";
        creditCard.setCardNumber(bankCard.substring(bankCard.length() - 4));
        creditCard.setExpirationDate("XXXX");
        paymentType.setCreditCard(creditCard);

        // 创建付款交易对象
        TransactionRequestType txnRequest = new TransactionRequestType();
        txnRequest.setTransactionType(TransactionTypeEnum.REFUND_TRANSACTION.value());
        txnRequest.setAmount(new BigDecimal(amount).setScale(2, RoundingMode.CEILING));
        txnRequest.setPayment(paymentType);
        txnRequest.setRefTransId(transId);

        //创建API请求并为此特定请求设置参数
        CreateTransactionRequest apiRequest = new CreateTransactionRequest();
        apiRequest.setMerchantAuthentication(merchantAuthenticationType);
        apiRequest.setTransactionRequest(txnRequest);

        // 呼叫控制器
        CreateTransactionController controller = new CreateTransactionController(apiRequest);
        controller.execute();

        // 得到回应
        CreateTransactionResponse response = controller.getApiResponse();

        log.info("authorize 退款操作返回结果: " + JSONObject.toJSONString(response));

        // 解析响应以确定结果
        if (response != null) {
            // 如果API响应正常,请继续检查事务响应
            if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
                TransactionResponse result = response.getTransactionResponse();
                if (result.getMessages() != null) {
                    return CommonResult.success("SUCCESS", result.getTransId());
                } else {
                    if (response.getTransactionResponse().getErrors() != null) {
                        return CommonResult.failMessage(500, "Authorize 申请退款失败", result.getErrors().getError().get(0).getErrorText());
                    }
                }
            } else {
                if (response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null) {
                    return CommonResult.failMessage(500, "Authorize 申请退款异常", response.getTransactionResponse().getErrors().getError().get(0).getErrorText());
                } else {
                    return CommonResult.failMessage(500, "Authorize 申请退款异常", response.getMessages().getMessage().get(0).getText());
                }
            }
        } else {
            // 响应为空时显示错误代码和消息
            ANetApiResponse errorResponse = controller.getErrorResponse();
            if (!errorResponse.getMessages().getMessage().isEmpty()) {
                return CommonResult.failMessage(500, "Authorize 申请退款异常", errorResponse.getMessages().getMessage().get(0).getText());
            }
        }
        return CommonResult.fail(500, "NetworkTimeout");
    }
这个是我第二天进行的退款操作:
authorize 退款操作返回结果: {"messages":{"message":[{"code":"I00001","text":"Successful."}],"resultCode":"OK"},"transactionResponse":{"accountNumber":"XXXX0015","accountType":"MasterCard","authCode":"","avsResultCode":"P","cavvResultCode":"","cvvResultCode":"","messages":{"message":[{"code":"1","description":"This transaction has been approved."}]},"refTransID":"40056263214","responseCode":"1","testRequest":"0","transHash":"","transHashSha2":"","transId":"40056286199"}}

在这里插入图片描述

5、查询订单:

地址:https://developer.authorize.net/api/reference/index.html#transaction-reporting-get-transaction-details

public CommonResult authorizeQuery(String transId) {
        // 设置环境沙盒或生产
        if (true) {
            ApiOperationBase.setEnvironment(Environment.SANDBOX);
        } else {
            ApiOperationBase.setEnvironment(Environment.PRODUCTION);
        }
        // 创建带有商家身份验证详细信息的对象
        MerchantAuthenticationType merchantAuthenticationType = new MerchantAuthenticationType();
        merchantAuthenticationType.setName(name);
        merchantAuthenticationType.setTransactionKey(transactionKey);
        
        GetTransactionDetailsRequest getRequest = new GetTransactionDetailsRequest();
        getRequest.setMerchantAuthentication(merchantAuthenticationType);
        getRequest.setTransId(transId);

        GetTransactionDetailsController controller = new GetTransactionDetailsController(getRequest);
        controller.execute();

        // 得到回应
        GetTransactionDetailsResponse response = controller.getApiResponse();

        log.info("authorize 查询操作返回结果: " + JSONObject.toJSONString(response));

        if (response != null) {
            if (response.getMessages().getResultCode() == MessageTypeEnum.OK) {
                return CommonResult.success("SUCCESS", response.getMessages().getMessage().get(0).getText());
            } else {
                log.error("authorize 查询订单未能获取信息:  {}", response.getMessages().getResultCode());
                return CommonResult.failMessage(500, "Authorize 支付查询失败", response.getMessages().getResultCode());
            }
        }
        return CommonResult.fail(500, "NetworkTimeout");
    }
支付成功后查询:
authorize 查询操作返回结果: {"clientId":"sdk-java-2.0.1","messages":{"message":[{"code":"I00001","text":"Successful."}],"resultCode":"OK"},"transaction":{"aVSResponse":"Y","authAmount":200.00,"authCode":"1HN636","cardCodeResponse":"P","customer":{"email":"646236438@qq.com"},"customerIP":"171.00.00.111","marketType":"eCommerce","order":{"discountAmount":0,"purchaseOrderNumber":"20201109793223","taxIsAfterDiscount":false},"payment":{"creditCard":{"cardNumber":"XXXX0015","cardType":"MasterCard","expirationDate":"XXXX"}},"product":"Card Not Present","recurringBilling":false,"responseCode":1,"responseReasonCode":1,"responseReasonDescription":"Approval","settleAmount":200.00,"submitTimeLocal":1604865630560,"submitTimeUTC":1604923230560,"taxExempt":false,"transId":"40056262861","transactionStatus":"capturedPendingSettlement","transactionType":"authCaptureTransaction"},"transrefId":"20201109793223"}

退款成功后查询:
authorize 查询操作返回结果: {"clientId":"sdk-java-2.0.1","messages":{"message":[{"code":"I00001","text":"Successful."}],"resultCode":"OK"},"transaction":{"aVSResponse":"Y","authAmount":200.00,"authCode":"AVE191","batch":{"batchId":"11110323","settlementState":"settledSuccessfully","settlementTimeLocal":1604917910323,"settlementTimeUTC":1604975510323},"cardCodeResponse":"P","customer":{"email":"646236438@qq.com"},"customerIP":"171.00.00.111","marketType":"eCommerce","order":{"discountAmount":0,"purchaseOrderNumber":"20201109793224","taxIsAfterDiscount":false},"payment":{"creditCard":{"cardNumber":"XXXX0015","cardType":"MasterCard","expirationDate":"XXXX"}},"product":"Card Not Present","recurringBilling":false,"responseCode":1,"responseReasonCode":1,"responseReasonDescription":"Approval","settleAmount":200.00,"submitTimeLocal":1604866679387,"submitTimeUTC":1604924279387,"taxExempt":false,"transId":"40056263214","transactionStatus":"settledSuccessfully","transactionType":"authCaptureTransaction"},"transrefId":"20201109793224"}

6:杂谈

1、很简单的一个支付, 客服给人的感觉很有亲和力!

2、基于订单的状态码:
Thank you for holding. First, regarding the automatic question, yes settlement with Authorize.Net happens once a day automatically. This process begins at your “Transaction Cut off time”. Currently that is set to: 4:00 PM PDT.
This can however vary depending on the Transaction status. If its “Captured/ Pending Settlement”. Thats great, its automatic. But for all other statuses, here is a support article that explains what they mean in case you need to take an action:
https://support.authorize.net/s/article/What-Are-the-Possible-Transaction-Statuses

3、交易截止时间如何设置? 也就是自动结算的意思
Well, the Transaction cut off time, just means when we begin to report your activity for funding. All new accounts have this set to 4PM Pacific time. But you can change this setting by following these steps:
-Click Account from the main toolbar.
-Click Settings from the main left side menu.
-Click Transaction Cut-Off Time near the bottom of the page.
-Click Edit.
-Select the desired Transaction Cut-Off Time.
-Click Submit.
NOTE: Changing the Transaction Cut-Off Time may delay settlement of the current batch by one day if the new time is less than 24 hours from the last settlement.

4、代码上传码云地址:https://gitee.com/xmaxm/payments_hodgepodge

5、需要配置结算时间, 他们不是实时结算, 一天为一个结算时间, 所以如果当天订单需要退款, 调用第4步的申请退款是会失败的,
可以参考这个API:https://developer.authorize.net/api/reference/index.html#payment-transactions-void-a-transaction

6、没有结算的订单就进行退款, 是会失败的, 只有结算的订单才可以进行退款操作

7、捕获和授权的流程, 如果不清楚, 可以参考我支付系列的文章PayPal支付

8、官方GitHub: https://github.com/search?utf8=%E2%9C%93&q=org%3AAuthorizeNet+sample&type=Repositories&ref=searchresults

9、很重要一点, 他们不是实时的, 他们是按一个交易周期进行操作, 应该是D+1吧, 没有测试过T+1

10、关于 transId:0 , 账号分为测试账号和正式账号, ApiOperationBase.setEnvironment设置的是基于该账号是什么类型. 而在后台有一个test mode , 这个是用来区分你的账号可以用测试卡还是正式卡
For transactions processed in Test Mode, you will receive a Transaction ID of 0.
To process Live transactions, you would need to remove your account from Test Mode by clicking the orange banner at the top of your Authorize.Net account and moving the button to LIVE.




提供一个群:807770565,欢迎各位进来尬聊 (人不多, 进了就不要退了, 要不就不进, 跪求留一点人, 人多了就活跃了, 跪谢)
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值