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

  • 7
    いいね
  • 0
    コメント

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_by_card(nanaco_number, card_number)

nanacoメニュー画面にログインします.(モバイル用のログインは未実装)
事前にdict型でnanacoの情報を用意しておくと、こんな感じでログインすることもできます.

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

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

login_credit_charge(credit_charge_password)

クレジットチャージ画面に遷移します.
クレジットチャージが未登録の場合は、クレジットチャージの案内画面に遷移します.

charge(value)

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

set(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'

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

    nanaco = PyNanaco()
    nanaco.login_by_card(**my_nanaco)
    nanaco.login_credit_charge()
    nanaco.set(
        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_by_card(**my_nanaco)
    nanaco.login_credit_charge('set_credit_charge_password_here')
    nanaco.charge(10000)


def example_set():
    nanaco = PyNanaco()
    nanaco.login_by_card(**my_nanaco2)
    nanaco.login_credit_charge()
    nanaco.set(
        credit=my_card,
        profile=my_profile,
        secure='set_secure_password_here'
    )


def example_cancel():
    nanaco = PyNanaco()
    nanaco.login_by_card(**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万円以上チャージなど)