如何将authorize.net集成到我的wix页面?

时间:2019-05-13 14:16:24

标签: json api payment authorize.net wixcode

我正在使用authorize.net的沙盒API在我的wix(Corvid /代码)环境中测试其网关。有趣的是,当我将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获得良好的响应?像我在邮递员中用相同的代码完成的事情?

预先感谢

1 个答案:

答案 0 :(得分:1)

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

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

这给了我我想要的结果

stringify()将我的对象转换为JSON字符串。我仍然无法获取它来读取传入的JSON,可能不得不使用parse ...但是如果我将其读取为文本,则可以获得所需的信息,并且我的API显示交易成功。