Pritesh Mahajan 的答案在正确的轨道上,但我不知道它对他有什么作用,因为抛出和返回会破坏代码。这是一个更完整的示例,将 ctrl.execute 转换为与 await 一起使用的 Promise。
此示例还消除了对全局变量的需求,该变量永远不应该用于 Node 中的事务值。
我还使用了 togo 包中的 to() 函数,该函数实现了 go 语言中的 to 功能,从而消除了对大型 try/catch 块的需要。
此示例是使用运行 NodeJS 14 或更高版本的 ES6 模块的节点项目的完整示例。
这是为与 Accept.js 一起使用而设置的,使用 Authorize.Net 托管的支付信息表单以符合 PCI-DSS SAQ A 的方式收集卡信息。
更多详情请见https://developer.authorize.net/api/reference/features/acceptjs.html#Using_the_Hosted_Payment_Information_Form。
成功交易所需的唯一数据是 authData 和 paymentData。包括运输数据作为示例,如果您需要 Authorize.NET API 提供的任何其他数据,可以轻松修改此功能以支持它。
import authorizenet from "authorizenet";
const ApiContracts = authorizenet.APIContracts;
const ApiControllers = authorizenet.APIControllers;
const SDKConstants = authorizenet.Constants;
function to (promise)
{
return promise
.then(val => [null, val])
.catch(err => [err]);
}
async function chargeCustomer (params = {}) {
/*
params = {
authData: {
api_login_id,
transaction_key,
endpoint
}
paymentData: {
opaqueData: {
dataDescriptor,
dataValue
},
amount
}
shipTo: {
firstName,
lastName,
company,
address,
city,
state,
zip,
country
}
}
*/
if ((!params?.authData?.api_login_id) || (!params?.authData?.transaction_key)) {
throw "missing credentials";
}
let merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType();
merchantAuthenticationType.setName(params.authData.api_login_id);
merchantAuthenticationType.setTransactionKey(params.authData.transaction_key);
let opaqueData = new ApiContracts.OpaqueDataType();
opaqueData.setDataDescriptor(params.opaqueData.dataDescriptor);
opaqueData.setDataValue(params.opaqueData.dataValue);
let paymentType = new ApiContracts.PaymentType();
paymentType.setOpaqueData(opaqueData);
let shipTo = new ApiContracts.CustomerAddressType();
shipTo.setFirstName(params?.shipTo?.firstName || null);
shipTo.setLastName(params?.shipTo?.last_name || null);
shipTo.setCompany(params?.shipTo?.company || null);
shipTo.setAddress(params?.shipTo?.address || null);
shipTo.setCity(params?.shipTo?.city || null);
shipTo.setState(params?.shipTo?.state || null);
shipTo.setZip(params?.shipTo?.zip || null);
shipTo.setCountry(params?.shipTo?.country || null);
let transactionRequestType = new ApiContracts.TransactionRequestType();
transactionRequestType.setTransactionType(ApiContracts.TransactionTypeEnum.AUTHCAPTURETRANSACTION);
transactionRequestType.setPayment(paymentType);
transactionRequestType.setAmount(params?.paymentData?.amount);
transactionRequestType.setShipTo(shipTo);
let createRequest = new ApiContracts.CreateTransactionRequest();
createRequest.setMerchantAuthentication(merchantAuthenticationType);
createRequest.setTransactionRequest(transactionRequestType);
let ctrl = new ApiControllers.CreateTransactionController(createRequest.getJSON());
if (params?.authData?.endpoint === "production") {
ctrl.setEnvironment(SDKConstants.endpoint.production);
}
let [err, response] = await to(new Promise((resolve, reject) => {
ctrl.execute( () => {
let apiResponse = ctrl.getResponse();
let response = new ApiContracts.CreateTransactionResponse(apiResponse);
if (response != null) {
if (response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK) {
if (response.getTransactionResponse().getMessages() != null) {
console.log(JSON.stringify(response));
resolve(response);
} else {
console.debug('Failed Transaction.');
if (response.getTransactionResponse().getErrors() != null) {
reject(response.getTransactionResponse().getErrors())
}
}
} else {
reject('null response from Authorize.Net');
}
};
});
}));
if (err) {
throw err;
}
return response;
}