【问题标题】:how do i integrate authorize.net into my wix page?我如何将 authorize.net 集成到我的 wix 页面中?
【发布时间】:2019-09-30 12:38:03
【问题描述】:

我正在使用 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 错误,因为返回代码包含 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 获得良好的响应?就像我在邮递员中使用相同的代码一样?

提前致谢

【问题讨论】:

    标签: json api payment authorize.net velo


    【解决方案1】:
    return fetch(url, {
        method: "post", 
        headers: {"Content-Type": "application/json"}, 
        body: JSON.stringify(data)
      })
    
      .then(response => console.log(response.text())
      )
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-05
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 2011-01-12
      相关资源
      最近更新 更多