首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Square和Django入门

Square和Django入门
EN

Stack Overflow用户
提问于 2019-08-01 06:09:21
回答 1查看 1.5K关注 0票数 1

我正在尝试获得一个简单的广场信用卡表单,以在我们的Django网站上工作。我刚开始使用Square,但以前也用过类似的API,比如Stripe。

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

前端似乎正常工作,每次我输入假凭据时,它都会生成一个唯一的卡随机数。

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

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

代码语言:javascript
复制
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中演示的示例。

但是,当我在本地主机(http)中使用Square在其test values page上提供的不同演示凭据进行测试时,我始终收到来自test values page的“未授权”错误:

代码语言:javascript
复制
Exception when calling TransactionApi->charge: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict(
{'Content-Type': 'application/json', 'Vary': 'Origin, Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Download-Options': 'noopen', 'X-Frame-Options': 'SAMEORIGIN', 'X-Permitted-Cross-Domain-Policies': 'none', 'X-Xss-Protection': '1; mode=block', 'Date': 'Wed, 31 Jul 2019 20:59:10 GMT', 'keep-alive': 'timeout=60', 'Strict-Transport-Security': 'max-age=631152000', 'content-length': '119'})
HTTP response body: {"errors":[{"category":"AUTHENTICATION_ERROR","code":"UNAUTHORIZED","detail":"This request could not be authorized."}]}

当我查看故障排除文档时,发现Unauthorized error的可能原因是无效的OAuth令牌。然而,Square的演示或示例中没有一个使用OAuth。我不明白为什么一个不在注册页面后面的简单付款表单需要OAuth?

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

EN

回答 1

Stack Overflow用户

发布于 2020-06-10 23:25:14

你需要设置你的访问令牌,然后你的代码才能工作。位置id也没有设置,所以square会发送一个错误。我也遇到了同样的问题,我意识到这是因为我忘了添加访问令牌。希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57299451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档