尝试使用 XML 数据将 $ajax post 转换为 authorize.net API 时出错
Error When Attempting to $ajax post to authorize.net API with XML Data
我目前正在尝试向 Authorize.net AIM api 发出请求。我已经在 Post Man 中成功发送了 post。我遇到的问题与 posted 数据必须在 xml 中有关。当我尝试将数据设置为 xml 时,如下面的示例所示,我收到意外令牌非法错误。对这个错误的任何帮助都会很棒。此外,如果有更好的方法来为 post 格式化 xml,请告诉我我环顾四周分配没有成功。
$.ajax({
type : "POST",
url : "https://apitest.authorize.net/xml/v1/request.api",
data : "<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
<name>xxxxxxxxxxx</name>
<transactionKey>xxxxxxxxxxx</transactionKey>
</merchantAuthentication>
<refId>123456</refId>
<transactionRequest>
<transactionType>authCaptureTransaction</transactionType>
<amount>5</amount>
<payment>
<creditCard>
<cardNumber>5424000000000015</cardNumber>
<expirationDate>1220</expirationDate>
<cardCode>999</cardCode>
</creditCard>
</payment>
<order>
<invoiceNumber>INV-12345</invoiceNumber>
<description>Product Description</description>
</order>
<lineItems>
<lineItem>
<itemId>1</itemId>
<name>vase</name>
<description>Cannes logo </description>
<quantity>18</quantity>
<unitPrice>45.00</unitPrice>
</lineItem>
</lineItems>
<tax>
<amount>4.26</amount>
<name>level2 tax name</name>
<description>level2 tax</description>
</tax>
<duty>
<amount>8.55</amount>
<name>duty name</name>
<description>duty description</description>
</duty>
<shipping>
<amount>4.26</amount>
<name>level2 tax name</name>
<description>level2 tax</description>
</shipping>
<poNumber>456654</poNumber>
<customer>
<id>99999456654</id>
</customer>
<billTo>
<firstName>Ellen</firstName>
<lastName>Johnson</lastName>
<company>Souveniropolis</company>
<address>14 Main Street</address>
<city>Pecan Springs</city>
<state>TX</state>
<zip>44628</zip>
<country>USA</country>
</billTo>
<shipTo>
<firstName>China</firstName>
<lastName>Bayles</lastName>
<company>Thyme for Tea</company>
<address>12 Main Street</address>
<city>Pecan Springs</city>
<state>TX</state>
<zip>44628</zip>
<country>USA</country>
</shipTo>
<customerIP>192.168.1.1</customerIP>
<transactionSettings>
<setting>
<settingName>testRequest</settingName>
<settingValue>false</settingValue>
</setting>
</transactionSettings>
<userFields>
<userField>
<name>MerchantDefinedFieldName1</name>
<value>MerchantDefinedFieldValue1</value>
</userField>
<userField>
<name>favorite_color</name>
<value>blue</value>
</userField>
</userFields>
</transactionRequest>
</createTransactionRequest>",
dataType : "xml",
success : function(msg){
console.log("success");
}
error : function(msg) {
console.log("fail");
}
});
卢克,您现在可以 post 您在 JSON 中的请求,Authorize.Net 支持 JSON,这是 Authorize.net 的示例信用卡收费请求
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "API_LOGIN_ID",
"transactionKey": "API_TRANSACTION_KEY"
},
"refId": "123456",
"transactionRequest": {
"transactionType": "authCaptureTransaction",
"amount": "5",
"payment": {
"creditCard": {
"cardNumber": "5424000000000015",
"expirationDate": "1220",
"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"
}
]
}
}
}
}
我最终使用 PHP 发出请求,并且能够像本示例中那样在 xml 周围使用单引号将 xml 形成为字符串。
$retCustXml = '
<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
<name>'.$loginID.'</name>
<transactionKey>'.$transactionKey.'</transactionKey>
</merchantAuthentication>
<refId>1234546</refId>
<transactionRequest>
<transactionType>authCaptureTransaction</transactionType>
<amount>'.$amount.'</amount>
<profile>
<customerProfileId>'.$customerProfileId.'</customerProfileId>
<paymentProfile>
<paymentProfileId>'.$paymentProfileId.'</paymentProfileId>
</paymentProfile>
</profile>
<order>
<invoiceNumber>'.$invoiceNumber.'</invoiceNumber>
<description>'.$description.'</description>
</order>
</transactionRequest>
</createTransactionRequest>
';
谢谢你的帮助。
我目前正在尝试向 Authorize.net AIM api 发出请求。我已经在 Post Man 中成功发送了 post。我遇到的问题与 posted 数据必须在 xml 中有关。当我尝试将数据设置为 xml 时,如下面的示例所示,我收到意外令牌非法错误。对这个错误的任何帮助都会很棒。此外,如果有更好的方法来为 post 格式化 xml,请告诉我我环顾四周分配没有成功。
$.ajax({
type : "POST",
url : "https://apitest.authorize.net/xml/v1/request.api",
data : "<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
<name>xxxxxxxxxxx</name>
<transactionKey>xxxxxxxxxxx</transactionKey>
</merchantAuthentication>
<refId>123456</refId>
<transactionRequest>
<transactionType>authCaptureTransaction</transactionType>
<amount>5</amount>
<payment>
<creditCard>
<cardNumber>5424000000000015</cardNumber>
<expirationDate>1220</expirationDate>
<cardCode>999</cardCode>
</creditCard>
</payment>
<order>
<invoiceNumber>INV-12345</invoiceNumber>
<description>Product Description</description>
</order>
<lineItems>
<lineItem>
<itemId>1</itemId>
<name>vase</name>
<description>Cannes logo </description>
<quantity>18</quantity>
<unitPrice>45.00</unitPrice>
</lineItem>
</lineItems>
<tax>
<amount>4.26</amount>
<name>level2 tax name</name>
<description>level2 tax</description>
</tax>
<duty>
<amount>8.55</amount>
<name>duty name</name>
<description>duty description</description>
</duty>
<shipping>
<amount>4.26</amount>
<name>level2 tax name</name>
<description>level2 tax</description>
</shipping>
<poNumber>456654</poNumber>
<customer>
<id>99999456654</id>
</customer>
<billTo>
<firstName>Ellen</firstName>
<lastName>Johnson</lastName>
<company>Souveniropolis</company>
<address>14 Main Street</address>
<city>Pecan Springs</city>
<state>TX</state>
<zip>44628</zip>
<country>USA</country>
</billTo>
<shipTo>
<firstName>China</firstName>
<lastName>Bayles</lastName>
<company>Thyme for Tea</company>
<address>12 Main Street</address>
<city>Pecan Springs</city>
<state>TX</state>
<zip>44628</zip>
<country>USA</country>
</shipTo>
<customerIP>192.168.1.1</customerIP>
<transactionSettings>
<setting>
<settingName>testRequest</settingName>
<settingValue>false</settingValue>
</setting>
</transactionSettings>
<userFields>
<userField>
<name>MerchantDefinedFieldName1</name>
<value>MerchantDefinedFieldValue1</value>
</userField>
<userField>
<name>favorite_color</name>
<value>blue</value>
</userField>
</userFields>
</transactionRequest>
</createTransactionRequest>",
dataType : "xml",
success : function(msg){
console.log("success");
}
error : function(msg) {
console.log("fail");
}
});
卢克,您现在可以 post 您在 JSON 中的请求,Authorize.Net 支持 JSON,这是 Authorize.net 的示例信用卡收费请求
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "API_LOGIN_ID",
"transactionKey": "API_TRANSACTION_KEY"
},
"refId": "123456",
"transactionRequest": {
"transactionType": "authCaptureTransaction",
"amount": "5",
"payment": {
"creditCard": {
"cardNumber": "5424000000000015",
"expirationDate": "1220",
"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"
}
]
}
}
}
}
我最终使用 PHP 发出请求,并且能够像本示例中那样在 xml 周围使用单引号将 xml 形成为字符串。
$retCustXml = '
<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
<name>'.$loginID.'</name>
<transactionKey>'.$transactionKey.'</transactionKey>
</merchantAuthentication>
<refId>1234546</refId>
<transactionRequest>
<transactionType>authCaptureTransaction</transactionType>
<amount>'.$amount.'</amount>
<profile>
<customerProfileId>'.$customerProfileId.'</customerProfileId>
<paymentProfile>
<paymentProfileId>'.$paymentProfileId.'</paymentProfileId>
</paymentProfile>
</profile>
<order>
<invoiceNumber>'.$invoiceNumber.'</invoiceNumber>
<description>'.$description.'</description>
</order>
</transactionRequest>
</createTransactionRequest>
'; 谢谢你的帮助。