checkout.com Python开发包

checkout-sdk的Python项目详细描述


checkout.com python软件开发工具包

Build Statuscodecov

安装

pip install --upgrade checkout-sdk

来源:

python setup.py install

要求

  • Python3.4+

用法

使用环境变量

os.environ['CKO_SECRET_KEY']='<your secret key>'os.environ['CKO_SANDBOX']='True|true|1'# else is False (Production)os.environ['CKO_LOGGING']='debug|DEBUG|info|INFO'# ...importcheckout_sdkassdkapi=sdk.get_api()

使用初始化值

importcheckout_sdkassdkapi=sdk.get_api(secret_key='<your secret key>')# default sandbox = True

设置默认值

sdk.default_currency=sdk.Currency.EURsdk.default_auto_capture=Truesdk.default_auto_capture_delay=0# valid: 0 - 168 (hours)sdk.default_payment_type=sdk.PaymentType.Regular

付款请求

全卡

try:payment=api.payments.request(card={'number':'4242424242424242','expiryMonth':6,'expiry_year':2025,# snake_case is auto converted'cvv':'100'},value=100,# centscurrency=sdk.Currency.USD,# or 'usd'customer='customer@email.com'# Email or Customer Id)print(payment.id)print(payment.card.id)print(payment.customer.id)print(payment.http_response.body)# JSON bodyexceptsdk.errors.CheckoutSdkErrorase:print('{0.http_status}{0.error_code}{0.elapsed}{0.event_id} // {0.message}'.format(e))

卡号
try:payment=api.payments.request(card='card_713A3978-AFB2-4D30-BF9A-BA55714DC309',value=100,# centscurrency=sdk.Currency.USD,# or 'usd'customer='customer@email.com'# Email or Customer Id)ifpayment.approved:# ...exceptsdk.errors.CheckoutSdkErrorase:print('{0.http_status}{0.error_code}{0.elapsed}{0.event_id} // {0.message}'.format(e))

支付历史

获取历史记录

try:history=api.payments.history(payment_id)# charge_00000000000000000000forchargeinhistory.charges:print(charge.id)print(charge.created)print(charge.response_code)exceptsdk.errors.CheckoutSdkErrorase:print('{0.http_status}{0.error_code}{0.elapsed}{0.event_id} // {0.message}'.format(e))

有关详细信息,请参见payment_history.pycharge.py

3ds支持

支持3DS的完整卡

try:payment=api.payments.request(card={'number':'4242424242424242','expiryMonth':6,'expiryYear':2025,'cvv':'100'},value=100,currency=sdk.Currency.USD,customer='customer@email.com',charge_mode=sdk.ChargeMode.ThreeDS)print(payment.requires_redirect)# Trueprint(payment.id)# Payment Tokenprint(payment.redirect_url)# ACS Urlprint(payment.http_response.body)# JSON bodyexceptsdk.errors.CheckoutSdkErrorase:print('{0.http_status}{0.error_code}{0.elapsed}{0.event_id} // {0.message}'.format(e))

Important: If you use the Checkout.com Risk Engine to upgrade to a 3DS flow (from N3D) depending on criteria, you must always check for payment.requires_redirect first.

带3DS支持+N3D降级选项的完整卡

try:payment=api.payments.request(card={'number':'4242424242424242','expiryMonth':6,'expiryYear':2025,'cvv':'100'},value=5000,currency=sdk.Currency.USD,customer='customer@email.com',charge_mode=sdk.ChargeMode.ThreeDS,attempt_n3d=True)print(payment.downgraded)# Trueprint(payment.id)# Payment Tokenprint(payment.redirect_url)# Success/Confirmation Urlexceptsdk.errors.CheckoutSdkErrorase:print('{0.http_status}{0.error_code}{0.elapsed}{0.event_id} // {0.message}'.format(e))

Important: Value needs to be set to 5000 to simulate a 20153 response code on the Sandbox environment, which will then attempt an N3D charge.

替代付款

支付令牌请求
try:token=api.tokens.request_payment_token(value=100,# centscurrency=sdk.Currency.USD,# or 'usd'track_id='001',success_url='http://success.com',fail_url='http://retry.com')print(token.id)print(token.http_response.body)# JSON bodyexceptsdk.errors.CheckoutSdkErrorase:print('{0.http_status}{0.error_code}{0.elapsed}{0.event_id} // {0.message}'.format(e))

请求其他付款
# first, create a payment token as per the above code sampletry:payment=self.client.alternative_payment_request(apm_id=sdk.AlternativePaymentMethodId.IDEAL,payment_token=token.id,user_data={'issuerId':'INGBNL2A'},customer='joesmith@gmail.com'# Email or Customer Id)print(payment.requires_redirect)# Trueprint(payment.id)# Payment Tokenprint(payment.redirect_url)# Alternative Payment Urlprint(payment.http_response.body)# JSON bodyexceptsdk.errors.CheckoutSdkErrorase:print('{0.http_status}{0.error_code}{0.elapsed}{0.event_id} // {0.message}'.format(e))

其他付款信息

Important: Only iDEAL is supported at the moment when using alternative payment info.

try:info=self.client.alternative_payment_info(apm_id=sdk.AlternativePaymentMethodId.IDEAL)print(info.http_response.body)# JSON bodyexceptsdk.errors.CheckoutSdkErrorase:print('{0.http_status}{0.error_code}{0.elapsed}{0.event_id} // {0.message}'.format(e))

异常处理

classCheckoutSdkError(Exception):# catch allclassAuthenticationError(CheckoutSdkError):# 401classBadRequestError(CheckoutSdkError):# 400classResourceNotFoundError(CheckoutSdkError):# 404classTimeout(CheckoutSdkError):classTooManyRequestsError(CheckoutSdkError):# 422classApiError(CheckoutSdkError):# 500 / fallback

The SDK will not do any offline validation of card data, IDs, etc. Provided the values and types are correct, all business validations are handled at API level. On that note, expect ValueError and TypeError for incorrect usage.

处理API验证异常

API响应

{"eventId":"00000000-0000-0000-0000-000000000000","errorCode":"70000","message":"Validation error","errorMessageCodes":["70034","70013"],"errors":["Invalid card id","Invalid customer id"]}

异常处理

exceptsdk.errors.BadRequestErrorase:ife.validation_error:# error_code == 70000print(e.errors)# dictionary { msg_code: msg }formsg_codeine.errors:print(msg_code)# e.g. 70034print(e.errors[msg_code])# e.g. Invalid card id

记录

os.environ['CKO_LOGGING']='debug|DEBUG|info|INFO'

或者…

importlogginglogging.getLogger('cko').setLevel(logging.DEBUG)

测试套件

测试当前需要沙盒帐户。这最终将被传入的checkout.com mock api所取代。

export CKO_SECRET_KEY="<your secret key>"
export CKO_LOGGING="info|debug"
python setup.py test

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用JNA创建同一库的多个实例?   java在将Graphql查询作为JSON字符串传递时收到意外的令牌错误   OAuth2 oltu的java问题   java桌面应用程序使用的好的嵌入式数据库是什么?   java Firebase数据库高级查询选项   java正在使磁盘上的EhCache元素过期   java 安卓还原处于backstack中的片段的实例状态   XMemcached中的java异步集   java TimescaleDB是否使用与Postgresql完全相同的JDBC驱动程序?   java从网站c读取信息#   检查java Android中的字符串是否只包含数字和空格   c#如何向web服务发送特殊字符?   grails无法调用需要java的方法。lang.类参数?   java我在组合框中调用的方法不会运行所有代码,它只运行部分代码   java发送带有标头的HTTP GET请求