Skip to main content

REST client for I'mport;(http://www.iamport.kr)

Project description

https://travis-ci.org/iamport/iamport-rest-client-python.svg?branch=master https://codecov.io/gh/iamport/iamport-rest-client-python/branch/master/graph/badge.svg

Python 사용자를 위한 아임포트 REST API 연동 모듈입니다.

설치

pip install iamport-rest-client

기능

  1. 결제 정보 찾기

  2. 가격 확인

  3. 취소

  4. 비 인증 결제

  5. 정기 예약 결제

  6. 본인인증결과 조회 및 삭제

사용법

준비

사용하기 위해 객체를 만듭니다.

from iamport import Iamport

# 테스트 용
iamport = Iamport(imp_key='{테스트용 키}', imp_secret='{테스트 시크릿}')
# 테스트용 키와 시크릿은 tests/conftest.py 파일에 DEFAULT_TEST_IMP_KEY, DEFAULT_TEST_IMP_SECRET를 참고하세요.

# 실제 상점 정보
iamport = Iamport(imp_key='{발급받은 키}', imp_secret='{발급받은 시크릿}')

찾기

결제를 진행한 상품 아이디나, 전달받은 IMP 아이디를 이용해 결제 정보를 찾습니다.

# 상품 아이디로 조회
response = iamport.find(merchant_uid='{상품 아이디}')

# I'mport; 아이디로 조회
response = iamport.find(imp_uid='{IMP UID}')

가격 확인

실제 제품 가격과 결제된 가격이 같은지 확인합니다.

# 상품 아이디로 확인
iamport.is_paid(product_price, merchant_uid='{상품 아이디}')

# I'mport; 아이디로 확인
iamport.is_paid(product_price, imp_uid='{IMP UID}')

# 이미 찾은 response 재활용하여 확인
iamport.is_paid(product_price, response=response)

취소

결제를 취소합니다.

# 상품 아이디로 취소
response = iamport.cancel(u'취소하는 이유', merchant_uid='{상품 아이디}')

# I'mport; 아이디로 취소
response = iamport.cancel(u'취소하는 이유', imp_uid='{IMP UID}')

# 취소시 오류 예외처리(이미 취소된 결제는 에러가 발생함)
try:
    response = iamport.cancel(u'취소하는 이유', imp_uid='{IMP UID}')
except Iamport.ResponseError as e:
    print e.code
    print e.message  # 에러난 이유를 알 수 있음
except Iamport.HttpError as http_error:
    print http_error.code
    print http_error.reason # HTTP not 200 에러난 이유를 알 수 있음

비인증 결제

1회성 비인증 결제를 진행합니다.

# 테스트용 값
payload = {
    'merchant_uid': '00000000',
    'amount': 5000,
    'card_number': '4092-0230-1234-1234',
    'expiry': '2019-03',
    'birth': '500203',
    'pwd_2digit': '19'
}
try:
    response = iamport.pay_onetime(**payload)
except KeyError:
    # 필수 값이 없을때 에러 처리
    pass
except Iamport.ResponseError as e:
    # 응답 에러 처리
    pass
except Iamport.HttpError as http_error:
    # HTTP not 200 응답 에러 처리
    pass

저장된 빌링키로 재결제합니다.

# 테스트용 값
payload = {
    'customer_uid': '{고객 아이디}',
    'merchant_uid': '00000000',
    'amount': 5000,
}
try:
    response = iamport.pay_again(**payload)
except KeyError:
    # 필수 값이 없을때 에러 처리
    pass
except Iamport.ResponseError as e:
    # 응답 에러 처리
    pass
except Iamport.HttpError as http_error:
    # HTTP not 200 응답 에러 처리
    pass

정기 예약 결제

정기 결제를 예약합니다.

# 테스트용 값
payload = {
    'customer_uid': '{고객 아이디}',
    'schedules': [
        {
            'merchant_uid': 'test_merchant_01',
            # UNIX timestamp
            'schedule_at': 1478150985,
            'amount': 1004
        },
        {
            'merhcant_uid': 'test_merchant_02',
            # UNIX timestamp
            'schedule_at': 1478150985,
            'amount': 5000,
            'name': '{주문명}',
            'buyer_name': '{주문자명}',
            'buyer_email': '{주문자 이메일}',
            'buyer_tel': '{주문자 전화번호}',
            'buyer_addr': '{주문자 주소}',
            'buyer_postcode': '{주문자 우편번호}',
        },
    ]
}
try:
    reponse = iamport.pay_schedule(**payload)
except KeyError:
    # 필수 값이 없을때 에러 처리
    pass
except Iamport.ResponseError as e:
    # 응답 에러 처리
    pass
except Iamport.HttpError as http_error:
    # HTTP not 200 응답 에러 처리
    pass

정기 결제 예약을 취소합니다.

# 테스트용 값 (merchant_uid 가 누락되면 customer_uid 에 대한 결제예약정보 일괄취소)
payload = {
    'customer_uid': '{고객 아이디}',
    'merchant_uid': 'test_merchant_01',
}
try:
    response = iamport.pay_unschedule(**payload)
except KeyError:
    # 필수 값이 없을때 에러 처리
    pass
except Iamport.ResponseError as e:
    # 응답 에러 처리
    pass
except Iamport.HttpError as http_error:
    # HTTP not 200 응답 에러 처리
    pass

결제 사전 검증

결제될 내역에 대한 사전정보를 등록합니다

# 테스트용 값
amount = 12000
mid = 'merchant_test'
try:
    response = iamport.prepare(amount=amount, merchant_uid=mid)
except Iamport.ResponseError as e:
    # 응답 에러 처리
    pass
except Iamport.HttpError as http_error:
    # HTTP not 200 응답 에러 처리
    pass

등록된 사전정보를 확인합니다

# 테스트용 값
amount = 12000
mid = 'merchant_test'
try:
    result = iamport.prepare_validate(merchant_uid=mid, amount=amount)
except Iamport.ResponseError as e:
    # 응답 에러 처리
    pass
except Iamport.HttpError as http_error:
    # HTTP not 200 응답 에러 처리
    pass

본인인증 결과 조회 및 관리

본인인증결과를 조회합니다.

try:
    response = iamport.find_certification(imp_uid='{IMP UID}')
except Iamport.ResponseError as e:
    # 응답 에러 처리
    pass
except Iamport.HttpError as http_error:
    # HTTP not 200 응답 에러 처리
    pass

본인인증결과를 아임포트에서 삭제합니다.

try:
    response = iamport.cancel_certification(imp_uid='{IMP UID}')
except Iamport.ResponseError as e:
    # 응답 에러 처리
    pass
except Iamport.HttpError as http_error:
    # HTTP not 200 응답 에러 처리
    pass

개발환경 및 테스트 설정

macOS 기준 pyenv 설치 권장

# pyenv 준비
brew install pyenv
pyenv install -s 2.7.17
pyenv install -s 3.5.8
pyenv install -s 3.6.9
pyenv install -s 3.7.5
pyenv install -s 3.8.0
pyenv install -s pypy-5.7.1
pyenv local 2.7.17 3.5.8 3.6.9 3.7.5 3.8.0 pypy-5.7.1
pip install pytest pytest-cov collective.checkdocs Pygments tox-pyenv

# tox
tox

# 커버리지 확인
python -m pytest tests/ --cov=./

# 문서 확인
python setup.py checkdocs

기여

할 일

  • 결제 목록 읽기

  • 비인증 결제 세부 기능 지원

  • 문서화

  • 기타 등등

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iamport-rest-client-0.8.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iamport_rest_client-0.8.2-py2.py3-none-any.whl (11.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file iamport-rest-client-0.8.2.tar.gz.

File metadata

  • Download URL: iamport-rest-client-0.8.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

File hashes

Hashes for iamport-rest-client-0.8.2.tar.gz
Algorithm Hash digest
SHA256 607aa8c53ae35b3e07970f05a138a16bcc6a36e66b95ffcf837cd46c5d8283ea
MD5 dba6e8d9327293050e66903cb646174e
BLAKE2b-256 8b2bc05ad67668f64a3233d5679aa2194cfa690d2c17337284bfb1e043752c11

See more details on using hashes here.

File details

Details for the file iamport_rest_client-0.8.2-py2.py3-none-any.whl.

File metadata

  • Download URL: iamport_rest_client-0.8.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

File hashes

Hashes for iamport_rest_client-0.8.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 dad2c4c2dba1462b27d385a3ff9da0ca028a897ec11b3b408d213ca4f532f9a3
MD5 74e8525bf3db6a76a864e2f54c579d95
BLAKE2b-256 94df09fb1895bb333cfaeb0ccd0876fc0491ef0fb436c953430fd3005bb5896a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page