使用Node.js进行SOAP通信

我需要使用Nodejs进行SOAP通信,并采用了以下的模块来实现。

肥皂模块 mó

用法简单。

如果希望使用以下内容在WSDL中执行ExecutePayment,则可以这样做。

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://www.soap.com/" xmlns:tns="http://www.soap.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://www.soap.com/">
      <s:element name="ExecutePayment">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" name="InParam" type="tns:RequestParam" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="RequestParam">
        <s:sequence>
          <s:element minOccurs="0" name="UserNo" type="s:string" />
          <s:element minOccurs="0" name="DealNo" type="s:string" />
          <s:element minOccurs="0" name="Amount" type="s:string" />
        </s:sequence>
      </s:complexType>
      <s:element name="ExecutePaymentResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" name="OutParam" type="tns:ResponseParam" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ResponseParam">
        <s:sequence>
          <s:element minOccurs="0" name="Result" type="s:string" />
        </s:sequence>
      </s:complexType>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="ExecutePaymentIn">
    <wsdl:part name="parameters" element="tns:ExecutePayment" />
  </wsdl:message>
  <wsdl:message name="ExecutePaymentOut">
    <wsdl:part name="parameters" element="tns:ExecutePaymentResponse" />
  </wsdl:message>
  <wsdl:portType name="PaymentSoapPort">
    <wsdl:operation name="ExecutePayment">
      <wsdl:input message="tns:ExecutePaymentIn" />
      <wsdl:output message="tns:ExecutePaymentOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="PaymentSoapBinding" type="tns:PaymentSoapPort">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    <wsdl:operation name="ExecutePayment">
      <soap:operation soapAction="http://www.soap.com/ExecutePayment" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Payment">
    <wsdl:port name="PaymentSoapPort" binding="tns:PaymentSoapBinding">
      <soap:address location="http://www.soap.com/" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

在JavaScript中,可以通过以下方式进行执行。

var soap = require('soap');

// パラメータを作成
var args = {
  InParam: {
    UserNo: '77770000001',
    DealNo: '20170701000001',
    Amount: '2160'
  }
};

// 実行
soap.createClient(
  'http://www.soap.com/?wsdl' // エンドポイント
  , {} // オプションとして付加したい情報があればセット
  , function(err, client) {
    // Function実行
    client.ExecutePayment(
      // パラメータ
      args
      , function(err, result) {
        // 結果の処理
        var resObj = result.OutParam;
        console.log(resObj.Result);
      }
    );
  }
);

参数以及结果数据将以JSON格式指定和使用。

除了BaicAuth外,还支持Bearer和客户端证书等设置。
请参考:https://github.com/vpulim/node-soap#security

如果需要自定义指定 Header,请使用 addSoapHeader 进行实现。
https://github.com/vpulim/node-soap#outgoing-soap-headers

    client.addSoapHeader({
      HeaderName: {
        hoge: 'xxxx'
      }
    });
bannerAds