LoginSignup
3
3

More than 5 years have passed since last update.

NodejsからYahoo Wallet Fastpay APIを使う

Posted at

概要

 トークンを使ってチャージをしますが、NodejsのAPIライブラリは用意されていません。
 FastPay APIライブラリ ※PHP,Ruby,Pythonのみ(2016.2現在)

そこで、cURLのパラメータを参考にして、nodejsからAPIを呼ぶ実装を作ってみました。

準備

express

  • 今回はexpressjsを使います。express-generatorでテンプレを作成します。
npm install express-generator -g

request

  • APIへPOSTするのに、requestモジュールを使います。
npm install request 

実装

FastPay.jsのaction:遷移先の実装(部分)です。router.postが呼ばれる想定です。

router.post('fastpay_complete', function (req, res, next) {
    var fastpayToken = req.body.fastpayToken;   // req.bodyで渡されてきます
    var amount = req.body.amount;  //  req.body.amount;
    var yUser = 'yahooFastpay.secret';  // fastpay secret keyです。
    var fastpayApiUrl = 'https://fastpay.yahooapis.jp/v1/charges';
    var fastpayApiProxy = '';  // プロキシがある場合は設定します

    request({
        method: 'POST',
        proxy: fastpayApiProxy,
        url: fastpayApiUrl,
        auth: {
            user: yUser,
            pass: ''
        },
        form: {
            amount: amount,
            card: fastpayToken,
            capture: false
        },
        json: true
    }, function (error, response) {
        if ( error ) {
            console.log(error);
        }
        else {
            if( resonse.body.error ){   // fastpay APIからのエラーの場合
                //HTTPステータスコード、Types、Codeなどを見て処理     
            }
            else{
                // 正常系処理
            }
        }
    });

});

まとめ

  • 上記のような実装で、NodeJs(Express)で使えるようになりました。
  • 間違いなどありましたらご指摘ください。
3
3
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
3
3