LoginSignup
20
19

More than 5 years have passed since last update.

pythonでnanacoクレジットチャージできるモジュールPyNanacoを作った

Last updated at Posted at 2017-07-05

GitHub

PyNanaco

これは何ですか

  • seleniumを使ってnanacoウェブページにアクセスし, ログイン・ログアウト, クレジットチャージ, クレジットカード設定・解除ができます.
  • 1回のコマンドで最大50,000円までのクレジットチャージが可能です.

メリット

  • 50,000円を1回の操作でチャージできるのでweb画面を操作するより簡単
  • 複数枚のnanacoにクレジットカードを設定・解除する作業も数行で記述可能
  • 別途cronやスケジューラーと連動させればバッチ処理でnanacoチャージやクレジットカード設定変更が可能.

インストール方法

PyPIに登録してないのでGitからどうぞ.

pip install git+https://github.com/sawadyrr5/PyNanaco

インストール時にseleniumも合わせてインストールされるはずですが、されない場合はpip install seleniumでインストールしてください.

core.pyと同じ場所にchromedriver.exeを配置する必要があります.
入手はこちらからどうぞ.
chrome webdriver

メソッド解説

login(nanaco_number, card_number, password)

nanacoメニュー画面にログインします.
card_numberを入力するとカード記載の番号で, passwordを入力するとモバイル会員用パスワードでログインします.(両方入力した場合はcard_number優先)
事前にdict型でnanacoの情報を用意しておくと, こんな感じでログインすることもできます.

my_nanaco = dict(
    nanaco_number='xxxxxxxxxxxxxxxx',
    card_number='yyyyyyy'
)

nanaco = PyNanaco()
nanaco.login(**my_nanaco)

login_credit_charge(credit_charge_password)

クレジットチャージ画面に遷移します.
クレジットチャージが登録済みの場合は, 登録されているカード番号を返します.('xxxx-xxxx-xxxx-1234'のような文字列)
クレジットチャージが未登録の場合は, クレジットチャージの案内画面に遷移します.

history()

クレジットシャージ履歴を取得します.
戻り値としてdict(charged_count=x, charged_amount=y)を返します.

charge(value)

チャージします.
valueは1,000円単位で50,000円まで入力可能です.
なお30,000円を超える場合, 2回目が最低5,000円になるように分けてチャージ処理を行います.
(例:
50,000円 = 30,000円 + 20,000円
31,000円 = 26,000円 + 5,000円)
処理中にPGSE09エラーなどが発生した場合はPyNanacoCreditChargeError例外を上げて止まります.

register(credit, profile, secure)

クレジットカード情報をセットします.
creditprofileはdict型で、secureは文字列で指定してください.(ここのインタフェースは上手くまとまっていないので, 後から変更するかもしれません)

my_card = dict(
    number='xxxxxxxxxxxxxxxx',
    expire_month='mm',
    expire_year='yyyy',
    code='xxx',
    phone='xxxxxxxxxxx'
)

my_profile = dict(
    name='john doe',
    birthday=datetime(1980, 1, 1),
    password='xxxxxxxx',
    mail='xxx@xxx.xxx',
    send_information='2'
)

secure='secure_password_here'

registerを行う場合あらかじめlogin_credit_charge()を実行しておいてください.

    nanaco = PyNanaco()
    nanaco.login_by_card(**my_nanaco)
    nanaco.login_credit_charge()
    nanaco.register(
        credit=my_card,
        profile=my_profile,
        secure='set_secure_password_here'
    )

cancel()

クレジットカードの設定を解除します.
あらかじめlogin_credit_charge()でクレジットチャージメニューにログインする必要があります.

logout()

ログアウトします.

quit()

chromedriverを終了します.

サンプルコード

# -*- coding: utf-8 -*-
from datetime import datetime

from pynanaco.core import PyNanaco

# set your nanaco card information.
# (credit charge ready.)
my_nanaco = dict(
    nanaco_number='xxxxxxxxxxxxxxxx',
    card_number='yyyyyyy'
)

# set your nanaco card information.
# (credit charge not ready.)
my_nanaco2 = dict(
    nanaco_number='xxxxxxxxxxxxxxxx',
    card_number='yyyyyyy'
)

# set your credit-card information.
my_card = dict(
    number='xxxxxxxxxxxxxxxx',
    expire_month='mm',
    expire_year='yyyy',
    code='xxx',
    phone='xxxxxxxxxxx'
)

# set your profile.
my_profile = dict(
    name='john doe',
    birthday=datetime(1980, 1, 1),
    password='xxxxxxxx',
    mail='xxx@xxx.xxx',
    send_information='2'
)


def example_charge():
    nanaco = PyNanaco()
    nanaco.login(**my_nanaco)
    nanaco.login_credit_charge('set_credit_charge_password_here')
    nanaco.charge(10000)


def example_register():
    nanaco = PyNanaco()
    nanaco.login(**my_nanaco2)
    nanaco.login_credit_charge()
    nanaco.register(
        credit=my_card,
        profile=my_profile,
        secure='set_secure_password_here'
    )


def example_cancel():
    nanaco = PyNanaco()
    nanaco.login(**my_nanaco)
    nanaco.login_credit_charge('set_credit_charge_password_here')
    nanaco.cancel()


if __name__ == '__main__':
    example_charge()
    example_set()
    example_cancel()

工夫した点

  • サイト側の設計変更の影響を極小化するためにPageObjectsパターンを採用し、ページとメソッドに関する記述をpage.pyに、ページ要素のセレクタの記述をlocators.pyにそれぞれまとめました.

課題

  • エラー処理の実装(月15回以上チャージ, 月20万円以上チャージなど)
20
19
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
20
19