10
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nodejsでSOAP通信する

Last updated at Posted at 2017-07-29

NodejsでSOAP通信をする必要があり、以下のモジュールを採用して実装しました。

soapモジュール

使い方はシンプル。

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>

jsでは以下のように書くことで実行できます。

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'
      }
    });
10
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?