Square和Djang入门

2024-09-27 23:23:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图得到一个简单的方形信用卡表格在我们的Django网站上工作。我刚开始使用Square,但在过去也使用过类似的API,比如Stripe。

首先,我跟着他们的Square Payment Form walkthrough。我添加了他们的SqPaymentForm库和我们自己的javascript文件,在那里我初始化了一个新的SqPaymentForm。

前端似乎正在工作,并且每次输入假凭据时都会生成一个唯一的卡nonce。

接下来,我将表单提交到后端(包括隐藏的nonce字段中的card nonce)。

在后端,我安装了Square Connect Python SDK。我尽可能地复制了他们的“Charge the card nonce”示例,替换了我们的沙盒访问令牌和位置ID:

import uuid

import squareconnect
from squareconnect.rest import ApiException
from squareconnect.apis.transactions_api import TransactionsApi

# create an instance of the Transactions API class
api_instance = TransactionsApi()
# setup authorization
api_instance.api_client.configuration.access_token = 'sandbox-access-token'

location_id = 'sandbox-location-id'

nonce = request.POST.get('nonce', 'empty')
if (nonce == 'empty'): print("Throw error")
# print(nonce)

try:
  # Charge
  idempotency_key = str(uuid.uuid1())
  amount = {'amount': 100, 'currency': 'USD'}
  body = {'idempotency_key': idempotency_key, 'card_nonce': nonce, 'amount_money': amount}
  api_response = api_instance.charge(location_id, body)
  print (api_response.transaction)
except ApiException as e:
  print ('Exception when calling TransactionApi->charge: %s\n' % e)

我还尝试过稍微重新格式化这段代码,以适应Square的connect-api-examples on GitHub中演示的示例。

但是,当我在localhost(http)中测试它时,使用Square在其test values page上提供的不同演示凭据,我总是从API收到一个“Unauthorized”错误:

^{pr2}$

当我浏览故障排除文档时,它指出Unauthorized error的可能原因是无效的OAuth令牌。然而,Square的演示或示例都没有使用OAuth。我不明白为什么OAuth对于一个不在注册页面后面的简单付款表单是必需的?

我试着把代码上传到我们的https网站,看看是否需要SSL证书,但是我得到了同样的错误。


Tags: instancekeyimportapiid示例locationcard

热门问题