如何将 authorize.net 整合到我的 Wix 页面中?

how do i integrate authorize.net into my wix page?

我正在使用 authorize.net 的沙箱 API 在我的 wix (corvid/code) 环境中测试他们的网关。有趣的是,当我将 JSON 发送到沙箱 API 时,我得到了一个有效的 JSON 响应,批准了(假)交易。但是,当我通过 Wix 进行设置时,我的控制台出现数据错误。我建立在现有文件的基础上,我已经能够 运行 基本 API 响应,以及更高级的带有令牌响应的身份验证。所以代码有效,只是不适用于 authorize.net。鉴于我的专业水平,我认为这可能是我做错了什么。我已经尽职调查,没有关于这个话题的问题。这是我的代码:

///front end, from the corvid page's code
import {buyIt} from 'backend/authorizeNet';       

export function button1_click(event) {
        buyIt();
}

非常基础,只是从我的后端 onClick 调用代码。文件路径是正确的。这是后端的模块:

////       backend/authorizeNet.jsw
import {fetch} from 'wix-fetch';  

export function buyIt() {

  let data = {
        "createTransactionRequest": {
        "merchantAuthentication": {
        "name": "***************",
        "transactionKey": "****************"
        },
        "refId": "123456",
        "transactionRequest": {
        "transactionType": "authCaptureTransaction",
        "amount": "5",
        "payment": {
            "creditCard": {
            "cardNumber": "5424000000000015",
            "expirationDate": "2020-12",
            "cardCode": "999"
            }
        },
        "lineItems": {
            "lineItem": {
            "itemId": "1",
            "name": "vase",
            "description": "Cannes logo",
            "quantity": "18",
            "unitPrice": "45.00"
            }
        },
        "tax": {
            "amount": "4.26",
            "name": "level2 tax name",
            "description": "level2 tax"
        },
        "duty": {
            "amount": "8.55",
            "name": "duty name",
            "description": "duty description"
        },
        "shipping": {
            "amount": "4.26",
            "name": "level2 tax name",
            "description": "level2 tax"
        },
        "poNumber": "456654",
        "customer": {
            "id": "99999456654"
        },
        "billTo": {
            "firstName": "Ellen",
            "lastName": "Johnson",
            "company": "Souveniropolis",
            "address": "14 Main Street",
            "city": "Pecan Springs",
            "state": "TX",
            "zip": "44628",
            "country": "USA"
        },
        "shipTo": {
            "firstName": "China",
            "lastName": "Bayles",
            "company": "Thyme for Tea",
            "address": "12 Main Street",
            "city": "Pecan Springs",
            "state": "TX",
            "zip": "44628",
            "country": "USA"
        },
        "customerIP": "192.168.1.1",
        "transactionSettings": {
            "setting": {
            "settingName": "testRequest",
            "settingValue": "false"
            }
        },
        "userFields": {
            "userField": [
            {
                "name": "MerchantDefinedFieldName1",
                "value": "MerchantDefinedFieldValue1"
            },
            {
                "name": "favorite_color",
                "value": "blue"
            }
            ]
        }
        }
    }
  }

  return fetch("https://test.authorize.net/xml/v1/request.api", {
    "method": "post", 
    "headers": {"Content-Type": "application/json"}, 
    "body": data
  })
  .then(response => {console.log(response.json())});///if response.text is used, it gives details

}

注意后端代码的末尾,调用 response.json 给我一个 json 错误,因为 return 代码包含 HTML 说我已经请求无效数据。如果我将其更改为 response.text,我会在我的控制台中看到:

//console response with response.text
{...}
isFulfilled: 
true
isRejected: 
false
fulfillmentValue: 
"<HTML><HEAD>\n<TITLE>Bad Request</TITLE>\n</HEAD><BODY>\n<H1>Bad Request</H1>\nYour browser sent a request that this server could not understand.<P>\nReference&#32;&#35;7&#46;1d60fea5&#46;1557756725&#46;387c74\n</BODY>\n</HTML>\n"

如何从 API 获得良好的响应?就像我在邮递员中使用相同的代码一样?

提前致谢

return fetch(url, {
    method: "post", 
    headers: {"Content-Type": "application/json"}, 
    body: JSON.stringify(data)
  })

  .then(response => console.log(response.text())
  )

这让我得到了我想要的结果

stringify() 将我的对象转换为 JSON 字符串。我仍然无法让它读取传入的 JSON,可能必须使用解析...但是如果我以文本形式读取,我会得到我想要的信息并且我的 API 显示交易成功。