LoginSignup
2
2

More than 5 years have passed since last update.

Omise APIを超簡単に使う方法

Last updated at Posted at 2018-02-21

curlコマンドとかAPIドキュメント毎回読むのとか面倒くさい問題

OmiseのAPIドキュメントにはいろんなAPIがある。そして、いろんなcurlコマンドがある。

確かに、curlは開発者にはやさしいものである。が、しかしたまにちょっと分かりづらくね?と思ったりしたこともある。

同僚が一生懸命APIをテストしているのだが、エディタにカールコマンドを一時的に貼り付けてそいつのパラメーターをちょっといじったりして、それをさらにコピペして、ターミナルで実行する。

なんかめんどくさいなあ....

Screen Shot 2018-02-21 at 12.42.00.png

もっと空気読んでいろいろやってくれないかなあ....

と思ったのが発端だった。

それで作ったのがこれ
https://github.com/keitaroemotion/omi

omiと書いてオマイと読む。

これは空気を読んでいろいろOmise APIを呼び出してくれる、いわばラッパーのようなものである。本当はgemファイルとかにしたら便利なんだろうが、それは Rubyの使用を強制してしまうかな、と思ったのと、そうすると多言語のやつも用意しないとフェアじゃないかなと思い、まだライブラリにはしていない。

前提条件

ruby2.3.5 以降
Mac OSX Sierra 以降
vim(which vimでちゃんとvimが出てくる状態)

手順

まずomiを取ってくる。

$ git clone https://github.com/keitaroemotion/omi.git
$ cd omi

次に、colorizeとかが必要になってくるので、必要なものをインストールする。

$ bundle install
$ ./installer

設定

設定のためのディレクトリを以下のように設定し、configファイルを編集。

$ mkdir /usr/local/etc/omi/
$ vim /usr/local/etc/omi/config

そして、ダッシュボードのキーの画面に行き、skeyとpkeyを取得する。

例えば、仮にあなたのskey, pkeyが

skey: skey_test_moomindaisuki63636363
pkey: pkey_test_florendaisuki63636363

だったとする。

そしたら、以下のようにconfigファイルは編集される

skey=skey_test_moomindaisuki63636363
pkey=pkey_test_florendaisuki63636363

早速つかってみよう!

以下のようにタイプしてみよう

$ omi

そうすると、こんな風になる

api missing. please input any of the following:

card
charge
customer
dispute
refund
token
recipient
transfer

これは、どのAPIを呼び出したいか教えてほしいと言っている。ので、

$ omi customer

こんなふうに打つと、

omi [customer]  delete    ... delete a customer
omi [customer]  create_wc ... create_a_customer_and_attach_a_card
omi [customer]  create    ... create_a_customer
omi [customer]  add_card  ... attach_a_card_to_customer
omi [customer]  list      ... list_all_customers
omi [customers]           ... list_all_customers
omi [customer]  get       ... retrieve_a_customer
omi [customer]  update    ... update_a_customer
[option: -d dry_run]
[option: -e allows editing the command before execution]
[option: -v allows viewing the execution result]
[option: -l swaps URL into localhost]

こんな感じで、どの引数を入れるとどんなことをしてくれるよ、というのがざっくりリストアップされる。なので、Customerを作りたい(create)と思ったら、

$ omi customer create

とやると、以下のようなコンソールが出力される。

curl -s https://api.omise.co/customers \
  -X POST \
  -u skey_test_moomindaisuki63636363: \
  -d "description=John Doe (id: 30)" \
  -d "email=john.doe@example.com"

{
  "object": "customer",
  "id": "cust_test_moominhoahoa123123123",
  "livemode": false,
  "location": "/customers/cust_test_moominhoahoa123123123",
  "default_card": null,
  "email": "john.doe@example.com",
  "description": "John Doe (id: 30)",
  "metadata": {
  },
  "created": "2018-02-21T03:13:02Z",
  "cards": {
    "object": "list",
    "from": "1970-01-01T00:00:00Z",
    "to": "2018-02-21T03:13:02Z",
    "offset": 0,
    "limit": 20,
    "total": 0,
    "order": null,
    "location": "/customers/cust_test_moominhoahoa123123123/cards",
    "data": [

    ]
  }
}

また、もし結果をvimエディタで表示したい場合、(コンソールだと中をじっくり見れないし、結果をコピペとかしにくい)-v オプションを使用する。

$ omi customer create -v

また、ある程度各APIのパラメータにはデフォルトの値が入ってしまっているので、そいつを編集してから実行したい場合、

$ omi customer create -e

とやると、curl実行以前にvimエディタを開き、コマンドのパラメータをいじることができる。

また、使うcurlコマンドは見たいけど実際に実行はしたくないんだよねという場合、dry-runオプションを設けてあるので、

$ omi customer create -d

とか

$ omi customer create -d -v

とかすると、実行するためのコマンドだけを参照することができる。

また、暫定的に設定したpkey, skey以外の値を入れて使いたいんだよね的な場合、

$ omi customer create pkey_warumono_moomin skey_manuke_floren

こんな感じでセットすると、引数として入れた pkey, skeyが使われる。

引数を指定する場合

上記のpkey, skey以外でも、引数を指定する場合がある。例えば、

特定のお客さんを課金するために、その人に紐づいたクレジットカード情報を登録したいよね的な場合だ。その場合、以下のコマンドを使うが、

$ omi customer add_card  

で、これを実行すると以下のように怒られる

You need argument token=             

これは、どのクレジットカードだよ?と言われているので、Omise.jsで取得したトークンIDを入れないといけない。

例えば、そのtoken_idが token_moomin_poaaaa_moomimoomi1234

だとしたら、

$ omi customer add_card token=token_moomin_poaaaa_moomimoomi1234

こんな風にしないといけない。

また、これはテスト限定の話(本番では絶対やっちゃダメ)だが、トークンを仮に作ったりできる。それは、

$ omi token create

これを実行すると、自動的にクリップボードに

token=token_....

をコピーしてくれるので、これを使うのも一つの手である。

Charge API

別の例として、Charge APIを見てみよう。

$ omi charge

omi [charge  ]  list      ... list all charges
omi [charges ]            ... list all charges
omi [charge  ]  update    ... update_a_charge
omi [charge  ]  reverse   ... reverse_an_uncaptured_charge
omi [charge  ]  get       ... retrieve_a_charge
omi [charge  ]  coib      ... create_an_offsite_internet_banking_charge
omi [charge  ]  coa       ... create_an_offsite_alipay_charge
omi [charge  ]  wt        ... charge_a_card_using_a_token
omi [charge  ]  wcc       ... charge_a_card_using_a_customer_and_a_card
omi [charge  ]  wc        ... charge_a_card_using_a_customer
omi [charge  ]  capture   ... capture_an_authorized_charge
[option: -d dry_run]
[option: -e allows editing the command before execution]
[option: -v allows viewing the execution result]
[option: -l swaps URL into localhost]

そうすると charge apiを使用する際の一通りのシナリオを見ることができる。

例えば、以下のようにすると

$ omi charges

こんなふうに課金の一覧が全部みれる。

curl -s -X GET https://api.omise.co/charges \
  -u skey_test_moominhoahoamoomin:

{
  "object": "list",
  "from": "1970-01-01T00:00:00Z",
  "to": "2018-02-21T03:47:04Z",
  "offset": 0,
  "limit": 20,
  "total": 25,
  "order": "chronological",
  "location": "/charges",
  "data": [
    {
      "object": "charge",
      "id": "chrg_test_moominmumi4455mumimumoo",
      "livemode": false,
      "location": "/charges/chrg_test_moominmumi4455mumimumoo",
      "amount": 100,
      "currency": "jpy",
      "description": "Another description",
      "metadata": {
      },
      "status": "successful",
      "capture": true,
      "authorized": true,
      "reversed": false,
      "paid": true,
      "transaction": "trxn_test_5apmoomoomoomoo",
      "source_of_fund": "card",
      "refunded": 100,
      "refunds": {
        "object": "list",
        "from": "1970-01-01T00:00:00Z",
        "to": "2018-02-21T03:47:05Z",
        "offset": 0,
        "limit": 20,
        "total": 1,
        "order": null,
        "location": "/charges/chrg_test_moominmumi4455mumimumoo/refunds",
        "data": [
          {
            "object": "refund",
            "id": "rfnd_test_moomidayohoahoadayo",
            "location": "/charges/chrg_test_moominmumi4455mumimumoo/refunds/rfnd_test_moomidayohoahoadayo",
            "created": "2018-01-21T12:43:58Z",
            "amount": 100,
            "currency": "jpy",
            "voided": true,
            "charge": "chrg_test_moominmumi4455mumimumoo",
            "transaction": "trxn_test_5apcwsjmoomindayo6363c1x",
            "metadata": {
            }
          }
        ]
      },
      "return_uri": null,
      "offsite": null,
      "offline": null,
      "reference": null,
      "authorize_uri": null,
      "failure_code": null,
      "failure_message": null,
      "card": {
        "object": "card",
        "id": "card_test_5amoomoogbu",
        "livemode": false,
        "country": "us",
        "city": "Bangkok",
        "postal_code": "10320",
        "financing": "",
        "bank": "",
        "last_digits": "4242",
        "brand": "Visa",
        "expiration_month": 10,
        "expiration_year": 2018,
        "fingerprint": "L30aDYuM+oomindayoHoahoa6363dayooonO6Go=",
        "name": "Somchai Prasert",
        "security_code_check": true,
        "created": "2018-01-21T11:40:33Z"
      },
      "customer": null,
      "ip": null,
      "dispute": null,
      "created": "2018-01-21T11:40:42Z"
    },
    {
      "object": "charge",
      "id": "chrg_test_moomin6363636363636",
      "livemode": false,
      "location": "/charges/chrg_test_5apcqmoooooooo",
      "amount": 100,
      "currency": "jpy",
      "description": null,
      "metadata": {
      },
      "status": "successful",
      "capture": true,
      "authorized": true,
      "reversed": false,
      "paid": true,
      "transaction": "trxn_test_5apcdaisukimoomoo",
      "source_of_fund": "card",
      "refunded": 100,
      "refunds": {
        "object": "list",
        "from": "1970-01-01T00:00:00Z",
        "to": "2018-02-21T03:47:05Z",
        "offset": 0,
        "limit": 20,
        "total": 1,
        "order": null,
        "location": "/charges/chrg_test_5amoomindaisuki/refunds",
        "data": [
          {
            "object": "refund",
            "id": "rfnd_test_5apcx1g3i1n6y1mooomin",
            "location": "/charges/chrg_test_5ap6363636363/refunds/rfnd_test_63636363moomooo",
            "created": "2018-01-21T12:44:40Z",
            "amount": 100,
            "currency": "jpy",
            "voided": true,
            "charge": "chrg_test_5apc63636363636363jgasr",
            "transaction": "trxn_test_5apcx16363636363636pyu",
            "metadata": {
            }
          }
        ]
      },
      "return_uri": null,
      "offsite": null,
      "offline": null,
      "reference": null,
      "authorize_uri": null,
      "failure_code": null,
      "failure_message": null,
      "card": {
        "object": "card",
        "id": "card_test_5amoominpdayo1guhoahoaa0i",
        "livemode": false,
        "country": "us",
        "city": "Bangkok",
        "postal_code": "10320",
        "financing": "",
        "bank": "",
        "last_digits": "4242",
        "brand": "Visa",
        "expiration_month": 10,
        "expiration_year": 2018,
        "fingerprint": "L30aDYuM+oomindayoHoahoa6363dayooonO6Go=",
        "name": "Somchai Prasert",
        "security_code_check": true,
        "created": "2018-01-21T12:25:01Z"
      },
      "customer": null,
      "ip": null,
      "dispute": null,
      "created": "2018-01-21T12:25:09Z"
    },
  ]
}

とか....


まだまだ書きたい事項はたくさんあるが、時間の関係で一応ここまでとする。

またこの記事の更新か、追加の記事でそれについては連携します。

以上。

まだβ版なので、使用中になんかトラブルとかエラーとかあったらコメントとかGitHubのIssuesとかで指摘してもらえると助かります。

2
2
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
2
2