创建付款
学习如何使用 Square API 进行各种类型的支付。
概述
每次购物时,我们都需要进行支付。根据购买的性质,可以选择不同的支付渠道。例如:
- 在线支付:买家访问卖家的网站或移动应用,并在该应用内完成支付。
- 线下支付:买家在卖家的实体场所使用卖家的POS设备(如iPad)进行支付。
Square Payments API 提供了 CreatePayment 接口,可用于直接让 Square 代为收款,或记录在 Square 外部处理的支付。无论哪种情况,create_payment 调用都会创建一个 Payment 对象,并在响应中返回。create_payment 方法接受的参数如下:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| amount_money | Money | 必填 | 本次支付要收取的金额。 |
| app_fee_money | Money | 可选 | 开发者为协助卖家完成支付而收取的手续费。 |
| autocomplete | Boolean | 可选 | 若设为 true,支付将在可能的情况下自动完成;若设为 false,支付将处于已批准状态,直到显式完成或取消。 |
| billing_address | Address | 可选 | 买家的账单地址。 |
| buyer_email_address | String | 可选 | 买家的电子邮件地址。 |
| idempotency_key | String | 必填 | 用于标识 CreatePayment 请求的唯一字符串,最大长度为 45 个字符。 |
| source_id | String | 必填 | 本次支付的资金来源 ID。根据支付类型,可以是卡凭证、"CASH" 或 "EXTERNAL"。 |
| tip_money | Money | 可选 | 除 amount_money 外额外指定的小费金额。 |
让 Square 代为收款
我们可以让 Square 代表我们接收支付,支持信用卡/借记卡或银行转账方式。
我们可以使用 CreatePayment 接口通过信用卡/借记卡或 Square 礼品卡完成支付。Square 保持 PCI 合规性,并在处理支付时检测欺诈行为。当发起 CreatePayment 请求时,Square 会从指定卡片扣除相应金额,并将该金额存入账户的 Square 余额中。

下面使用 CreatePayment 接口进行一次信用卡支付。
1random_string = ''.join(random.choice(string.ascii_letters) for i in range(40)) 2 3result = client.payments.create_payment( 4 body = { 5 "source_id": "cnon:card-nonce-ok", 6 "idempotency_key": random_string, 7 "amount_money": { 8 "amount": 100, 9 "currency": "USD" 10 } 11 } 12) 13 14if result.is_success(): 15 pprint(result.body) 16elif result.is_error(): 17 pprint(result.errors)
要进行信用卡支付,我们在第 5 行使用 cnon:card-nonce-ok 作为 source_id。以下为不同类型的信用卡支付对应的 source_id 值:
| source_id | 支付方式 |
|---|---|
| cnon:card-nonce-ok | 信用卡 |
| ccof:customer-card-id-ok | 保存的卡片(卡在文件中) |
| cnon:gift-card-nonce-ok | Square 礼品卡 |
“保存的卡片”指安全存储客户卡片信息,以便下次结账时一键扣款,节省时间。
除了成功支付外,若需测试失败支付,可使用以下测试值作为 source_id:
| source_id | 错误映射 |
|---|---|
| Card present: cnon:card-nonce-rejected-cvv | CVV 错误 |
| Card present: cnon:card-nonce-rejected-postalcode Card on file: ccof:customer-card-id-rejected-postalcode | 邮政编码错误 |
| Card present: cnon:card-nonce-rejected-expiration Card on file: ccof:customer-card-id-rejected-expiration | 有效期错误 |
| Card present: cnon:card-nonce-declined Card on file: ccof:customer-card-id-declined | 卡片被拒 |
| Card present: cnon:card-nonce-declined | 卡片支付令牌已被使用 |
| Card present: cnon:gift-card-nonce-insufficient-funds | 礼品卡余额不足 |
| Card present: cnon:gift-card-nonce-insufficient-permission | 礼品卡权限不足 |
ACH 银行转账支付
应用程序可通过 CreatePayment 接口接收自动清算所(ACH)银行转账支付。当 Square 收到 CreatePayment 请求时,会从指定银行账户扣款,并将资金存入账户的 Square 余额。
要进行 ACH 银行转账支付,可在代码第 5 行中提供以下任一支付令牌作为 source_id:
| source_id | 错误映射 | 预期行为 |
|---|---|---|
| bnon:bank-nonce-ok | 请求成功 | |
| bnon:bank-nonce-failure | GENERIC_DECLINE | 请求失败 |
| bnon:bank-nonce-account-unusable | ACCOUNT_UNUSABLE | 请求失败 |
| bnon:bank-nonce-insufficient-funds | INSUFFICIENT_FUNDS | 请求失败 |
| bnon:bank-nonce-invalid-account | INVALID_ACCOUNT | 请求失败 |
| bnon:bank-nonce-buyer-refused-payment | BUYER_REFUSED_PAYMENT | 请求失败 |
在了解如何记录在 Square 外部处理的支付之前,先来看一下创建支付的不同场景。
记录在 Square 外部处理的支付
除了由 Square 直接收款外,我们还可以记录在 Square 外部完成的支付。这包括现金支付或其他外部支付方式。
现金支付
当卖家从买家处收到现金支付时,可以使用 create_payment 方法记录该笔交易。此时,将 source_id 设置为 CASH。除了提供 source_id 和 amount_money 外,还需提供 cash_details,用于说明买家实际支付的金额。
1random_string = ''.join(random.choice(string.ascii_letters) for i in range(40)) 2 3result = client.payments.create_payment( 4 body = { 5 "source_id": "CASH", 6 "idempotency_key": random_string, 7 "amount_money": { 8 "amount": 100, 9 "currency": "USD" 10 }, 11 "cash_details": { 12 "buyer_supplied_money": { 13 "amount": 100, 14 "currency": "USD" 15 } 16 } 17 } 18) 19 20if result.is_success(): 21 pprint(result.body) 22elif result.is_error(): 23 pprint(result.errors)
外部支付
由非 Square 或卖家自身以外的第三方实体完成的支付称为 外部支付。例如,某外卖服务平台上,买家通过该平台下单,平台代收款项后再支付给卖家。其他场景还包括通过加密货币钱包或非 Square 礼品卡付款。卖家可以使用 CreatePayment 接口记录此类外部支付。
与现金支付不同,此处应将 source_id 设置为 EXTERNAL。此外,还需提供 external_details,用于描述外部支付的具体信息。
1random_string = ''.join(random.choice(string.ascii_letters) for i in range(40)) 2 3result = client.payments.create_payment( 4 body = { 5 "source_id": "EXTERNAL", 6 "idempotency_key": random_string, 7 "amount_money": { 8 "amount": 5000, 9 "currency": "USD" 10 }, 11 "external_details": { 12 "type": "OTHER", 13 "source": "外卖配送服务", 14 "source_fee_money": { 15 "amount": 500, 16 "currency": "USD" 17 } 18 } 19 } 20) 21 22if result.is_success(): 23 pprint(result.body) 24elif result.is_error(): 25 pprint(result.errors)