決済APIでカードの有効期限切れの対応ってどうするの?
PryとOmiseのRubyライブラリを使って、有効期限が切れるカードを持っているCustomerを検索する方法を書いておきます。
# pryを実行 pry # omiseライブラリを読込 require "omise" # APIキーを設定 Omise.api_key = "秘密鍵(シークレットキー)" # 試しにdescriptionを更新するCustomerを取得 customer_id = "cust_test_xxxxx" customer = Omise::Customer.retrieve(customer_id) # デフォルトカードの有効期限を取得 expiration_month = customer.default_card.expiration_month expiration_year = customer.default_card.expiration_year expiration = "#{expiration_month}/#{expiration_year}" # customerのdescriptionへデフォルトカードの有効期限を入れる customer.update({description: expiration}) # 3/2019のデフォルトカードを持っているCustomerを検索 search_result = Omise::Search.execute(scope: "customer", query: expiration) search_result.data.each do |customer| # ここで有効期限が切れそうなcustomerへ何かをする処理をいれる。 end
Customerへカードを紐づけるChargeをしている人には重宝するかもしれません。
Customerは Omise::Customer.list
で一覧を取得することもできるので、これを使っていっきにdescriptionをアップデートしてもいいかもです。