【问题标题】:Error When Attempting to $ajax post to authorize.net API with XML Data尝试 $ajax 发布以使用 XML 数据授权.net API 时出错
【发布时间】:2016-03-12 15:24:03
【问题描述】:

我目前正在尝试向 Authorize.net AIM api 发出请求。我已经在 Post Man 中成功发送了帖子。我遇到的问题与发布的数据必须在 xml 中有关。当我尝试如下例所示将数据设置为 xml 时,我收到 Unexpected token ILLEGAL 错误。对这个错误的任何帮助都会很棒。此外,如果有更好的方法来格式化帖子的 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");
    }
});

【问题讨论】:

  • 查看双引号字符串中出现的 XML 中的双引号。这行不通。

标签: javascript ajax xml api authorize.net


【解决方案1】:

卢克,您现在可以在 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"
                    }
                ]
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    我最终使用 PHP 来发出请求,我能够使用单引号将 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>
    

    '; 谢谢你的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 2021-05-24
      • 2014-01-31
      • 2022-08-20
      • 1970-01-01
      • 2021-08-29
      • 2020-05-12
      相关资源
      最近更新 更多