1.申请沙箱登录账号
2.登录后,申请api凭证和密钥
'use strict'; const ApiContracts = require('authorizenet').APIContracts; const ApiControllers = require('authorizenet').APIControllers; const SDKConstants = require('authorizenet').Constants; module.exports = class extends think.Service { //信用卡支付 async authorizeCreditCard(data) { // 创建带有商家身份验证详细信息的对象 var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType(); // merchantAuthenticationType.setName('*****'); // merchantAuthenticationType.setTransactionKey('********'); // 填充付款数据 var creditCard = new ApiContracts.CreditCardType(); creditCard.setCardNumber(data.CardNumber);//信用卡账号 creditCard.setExpirationDate(data.ExpirationDate);//信用卡过期时间 //支付 var paymentType = new ApiContracts.PaymentType(); paymentType.setCreditCard(creditCard); //创建付款交易对象 var transactionRequestType = new ApiContracts.TransactionRequestType(); transactionRequestType.setTransactionType(ApiContracts.TransactionTypeEnum.AUTHONLYTRANSACTION); transactionRequestType.setPayment(paymentType); transactionRequestType.setAmount(data.Amount);//价格 //创建请求 var createRequest = new ApiContracts.CreateTransactionRequest(); createRequest.setMerchantAuthentication(merchantAuthenticationType); createRequest.setTransactionRequest(transactionRequestType); //调用 var ctrl = new ApiControllers.CreateTransactionController(createRequest.getJSON()); //切换环境,注释测试,放开生产 ctrl.setEnvironment(SDKConstants.endpoint.production); return new Promise((resolve, reject) => { let res = {}; ctrl.execute(function(){ var apiResponse = ctrl.getResponse(); //得到返回值 var response = new ApiContracts.CreateTransactionResponse(apiResponse); //支付结果打印 // console.log(JSON.stringify(response, null, 2)); if(response != null){ if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK){ if(response.getTransactionResponse().getMessages() != null){ //返回交易id res.code = 200; res.message = response.getTransactionResponse().getTransId(); } else { if(response.getTransactionResponse().getErrors() != null){ res.code = 500; res.message = response.getTransactionResponse().getErrors().getError()[0].getErrorText(); } } }else { if(response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null){ res.code = 500; res.message = response.getTransactionResponse().getErrors().getError()[0].getErrorText(); } else { res.code = 500; res.message = response.getMessages().getMessage()[0].getText(); } } }else { res.code = 500; res.message = 'Null Response.'; } resolve(res); }); }); } //信用卡退款(一天前的订单才可以退款,可在官网配置) async authorizeRefund(data) { var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType(); merchantAuthenticationType.setName('******'); merchantAuthenticationType.setTransactionKey('**********'); var creditCard = new ApiContracts.CreditCardType(); creditCard.setCardNumber('4242424242424242'); creditCard.setExpirationDate('0822'); var paymentType = new ApiContracts.PaymentType(); paymentType.setCreditCard(creditCard); var transactionRequestType = new ApiContracts.TransactionRequestType(); transactionRequestType.setTransactionType(ApiContracts.TransactionTypeEnum.REFUNDTRANSACTION); transactionRequestType.setPayment(paymentType); transactionRequestType.setAmount(6); transactionRequestType.setRefTransId('*******'); var createRequest = new ApiContracts.CreateTransactionRequest(); createRequest.setMerchantAuthentication(merchantAuthenticationType); createRequest.setTransactionRequest(transactionRequestType); //pretty print request //console.log(JSON.stringify(createRequest.getJSON(), null, 2)); var ctrl = new ApiControllers.CreateTransactionController(createRequest.getJSON()); return new Promise((resolve, reject) => { let res = {}; ctrl.execute(function(){ var apiResponse = ctrl.getResponse(); var response = new ApiContracts.CreateTransactionResponse(apiResponse); //pretty print response //console.log(JSON.stringify(response, null, 2)); if(response != null){ if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK){ if(response.getTransactionResponse().getMessages() != null){ //返回交易id res.code = 200; res.message = response.getTransactionResponse().getTransId(); } else { if(response.getTransactionResponse().getErrors() != null){ res.code = 500; res.message = response.getTransactionResponse().getErrors().getError()[0].getErrorText(); } } } else { if(response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null){ res.code = 500; res.message = response.getTransactionResponse().getErrors().getError()[0].getErrorText(); } else { res.code = 500; res.message = response.getMessages().getMessage()[0].getText(); } } }else { res.code = 500; res.message = 'Null Response.'; } resolve(res); }); }); } }
以上都是封装好的支付和退款,直接传参调方法就完事了!
注意;测authorize.net正式环境时,要求全球通用的信用卡.