订阅
设置外部订阅支付方式
将 Stripe Billing 订阅关联到客户已在 Yuno 中验证的卡,或将已关联的订阅转移到另一种已注册的支付方式,不发起支付。
POST
/
external-subscriptions
/
{external_subscription_id}
/
payment-method
设置外部订阅支付方式
curl --request POST \
--url https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method \
--header 'Content-Type: application/json' \
--header 'X-Idempotency-Key: <x-idempotency-key>' \
--header 'private-secret-key: <api-key>' \
--header 'public-api-key: <api-key>' \
--data '
{
"account_id": "9104911d-5df9-429e-8488-ad41abea1a4b",
"customer_payer": {
"id": "cfae0941-7234-427a-a739-ef4fce966c79"
},
"vaulted_token": "2deff5c9-da62-4888-8f05-df76ab2967a5",
"external_customer_id": "cus_RplXYZ"
}
'import requests
url = "https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method"
payload = {
"account_id": "9104911d-5df9-429e-8488-ad41abea1a4b",
"customer_payer": { "id": "cfae0941-7234-427a-a739-ef4fce966c79" },
"vaulted_token": "2deff5c9-da62-4888-8f05-df76ab2967a5",
"external_customer_id": "cus_RplXYZ"
}
headers = {
"X-Idempotency-Key": "<x-idempotency-key>",
"public-api-key": "<api-key>",
"private-secret-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {
'X-Idempotency-Key': '<x-idempotency-key>',
'public-api-key': '<api-key>',
'private-secret-key': '<api-key>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
account_id: '9104911d-5df9-429e-8488-ad41abea1a4b',
customer_payer: {id: 'cfae0941-7234-427a-a739-ef4fce966c79'},
vaulted_token: '2deff5c9-da62-4888-8f05-df76ab2967a5',
external_customer_id: 'cus_RplXYZ'
})
};
fetch('https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'account_id' => '9104911d-5df9-429e-8488-ad41abea1a4b',
'customer_payer' => [
'id' => 'cfae0941-7234-427a-a739-ef4fce966c79'
],
'vaulted_token' => '2deff5c9-da62-4888-8f05-df76ab2967a5',
'external_customer_id' => 'cus_RplXYZ'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-Idempotency-Key: <x-idempotency-key>",
"private-secret-key: <api-key>",
"public-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method"
payload := strings.NewReader("{\n \"account_id\": \"9104911d-5df9-429e-8488-ad41abea1a4b\",\n \"customer_payer\": {\n \"id\": \"cfae0941-7234-427a-a739-ef4fce966c79\"\n },\n \"vaulted_token\": \"2deff5c9-da62-4888-8f05-df76ab2967a5\",\n \"external_customer_id\": \"cus_RplXYZ\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("X-Idempotency-Key", "<x-idempotency-key>")
req.Header.Add("public-api-key", "<api-key>")
req.Header.Add("private-secret-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method")
.header("X-Idempotency-Key", "<x-idempotency-key>")
.header("public-api-key", "<api-key>")
.header("private-secret-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"account_id\": \"9104911d-5df9-429e-8488-ad41abea1a4b\",\n \"customer_payer\": {\n \"id\": \"cfae0941-7234-427a-a739-ef4fce966c79\"\n },\n \"vaulted_token\": \"2deff5c9-da62-4888-8f05-df76ab2967a5\",\n \"external_customer_id\": \"cus_RplXYZ\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-Idempotency-Key"] = '<x-idempotency-key>'
request["public-api-key"] = '<api-key>'
request["private-secret-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"account_id\": \"9104911d-5df9-429e-8488-ad41abea1a4b\",\n \"customer_payer\": {\n \"id\": \"cfae0941-7234-427a-a739-ef4fce966c79\"\n },\n \"vaulted_token\": \"2deff5c9-da62-4888-8f05-df76ab2967a5\",\n \"external_customer_id\": \"cus_RplXYZ\"\n}"
response = http.request(request)
puts response.read_body{
"external_subscription_id": "sub_1QF2abc",
"account_id": "9104911d-5df9-429e-8488-ad41abea1a4b",
"customer_payer": {
"id": "cfae0941-7234-427a-a739-ef4fce966c79"
},
"vaulted_token": "2deff5c9-da62-4888-8f05-df76ab2967a5",
"external_customer_id": "cus_RplXYZ",
"result": "LINKED"
}
在以下两种情况下,将此 endpoint 用于 Stripe Billing 订阅:
关联尚未关联的订阅时:
- 订阅尚未关联到 Yuno。 例如,在您集成 Yuno 之前于 Stripe 中创建的订阅,其客户已在 Yuno 中注册并验证了一张卡。Yuno 会将该订阅关联到这张卡,并由 Yuno 对后续续订收费。
- 订阅已经关联。 Yuno 会将其转移到同一客户的另一种已存储的支付方式。
不会发起支付Yuno 不会向客户收费,也不会执行 $0 验证或 3DS。Yuno 会在 Stripe 中将该支付方式设为订阅的默认支付方式,后续续订将从该支付方式扣款。
请求
| 字段 | 描述 |
|---|---|
external_subscription_id(path) | Stripe 订阅 ID(sub_...),长度为 3 到 255 个字符。 |
account_id | 已接入计费桥接的 Yuno 账户(UUID)。 |
customer_payer.id | 拥有该订阅的 Yuno 客户 ID(UUID)。它不是 Stripe 客户 ID(cus_...)。 |
vaulted_token | 该客户某个已注册支付方式的存储令牌(UUID)。 |
external_customer_id | 该订阅的 Stripe 客户 ID(cus_...),长度为 3 到 255 个字符。关联尚未关联的订阅时必填。转移已关联的订阅时可选:发送后,Yuno 会检查它是否为该订阅的 Stripe 客户。空值视为未发送。 |
X-Idempotency-Key 标头为必填项。使用相同的幂等键重放相同的请求会返回第一次的结果。
响应会返回请求中的字段。当请求发送了 external_customer_id 时,响应还会返回 external_customer_id 和 result:
result | 含义 |
|---|---|
LINKED | 该订阅此前未关联到 Yuno。Yuno 已将其关联到该支付方式。 |
PAYMENT_METHOD_CHANGED | 该订阅此前已关联。Yuno 已将其转移到该支付方式。 |
NO_CHANGE | 该订阅已在使用该支付方式扣款。未做任何更改。 |
- 请求必须发送
external_customer_id。如果不发送,未关联的订阅会返回EXTERNAL_SUBSCRIPTION_NOT_FOUND。 - Stripe 订阅必须处于
active或trialing状态,并且自动收费(collection_method: charge_automatically),其客户必须是external_customer_id。 - 该卡必须此前已在 Yuno 中验证,例如在注册时进行过 $0 验证。Yuno 会使用该验证,以商户发起的方式对续订收费。
错误
| 状态 | 代码 | 含义 | 处理方式 |
|---|---|---|---|
400 | INVALID_PARAMETERS | 某个字段或 X-Idempotency-Key 标头缺失或格式错误,或者该幂等键已用于另一个订阅或支付方式。 | 修正请求。如需进行其他更改,请使用新的幂等键。 |
400 | CUSTOMER_NOT_FOUND | 该客户在此账户中不存在。 | 检查 customer_payer.id 是否为 Yuno 客户 ID,而不是 Stripe 的 cus_ ID。 |
400 | VAULTED_TOKEN_NOT_FOUND | vaulted_token 不属于该客户的支付方式。 | 使用该客户已注册的支付方式中的令牌。 |
400 | VAULTED_TOKEN_NOT_ENROLLED | 该支付方式未注册。仅当请求发送了 external_customer_id 时适用。 | 使用状态为 ENROLLED 的支付方式。 |
400 | VAULTED_TOKEN_WITHOUT_COUNTRY | 该支付方式没有国家信息,而关联订阅需要该信息。 | 使用该客户的另一种支付方式,或重新注册该卡。 |
400 | VAULTED_TOKEN_WITHOUT_NETWORK_TRANSACTION_ID | 该卡从未验证,因此 Yuno 无法以商户发起的方式对续订收费。 | 先验证该卡:使用 external_subscription 对象执行 $0 验证,与免费试用相同。 |
400 | EXTERNAL_SUBSCRIPTION_NOT_FOUND | Stripe 中该账户下没有具有此 ID 的订阅,或者请求未发送 external_customer_id 且该订阅未关联到 Yuno。 | 检查 Stripe 订阅 ID 和账户。如需关联订阅,请发送 external_customer_id。 |
400 | EXTERNAL_SUBSCRIPTION_NOT_ACTIVE | Stripe 订阅既不是 active 也不是 trialing 状态。 | 只有处于 active 或 trialing 状态的订阅才能被关联。 |
400 | EXTERNAL_SUBSCRIPTION_NOT_CHARGED_AUTOMATICALLY | 该 Stripe 订阅通过发送发票收费,而不是自动扣款。 | 在 Stripe 中将 collection_method 设为 charge_automatically。 |
400 | CUSTOMER_MISMATCH | 该订阅属于其他客户,或者 external_customer_id 不是该订阅的 Stripe 客户。 | 发送拥有该订阅的客户。 |
403 | PRODUCT_NOT_ENABLED | 该账户未启用计费桥接。 | 联系您的大客户经理。 |
409 | CONCURRENT_CHANGE | 该订阅的另一个支付方式更改已先完成。 | 在发送新的更改之前,先在 Stripe 中检查该订阅。 |
409 | EXTERNAL_SUBSCRIPTION_PENDING_ACTIVATION | 该订阅正在迁移到 Yuno。仅当请求发送了 external_customer_id 时适用;不发送时,返回 EXTERNAL_SUBSCRIPTION_NOT_FOUND。 | 迁移完成后重试。 |
502 | BILLING_PLATFORM_ERROR | 计费平台无法确认此更改。原支付方式保持不变,或者订阅保持未关联状态。 | 使用新的 X-Idempotency-Key 重试。 |
请求头
路径参数
请求体
application/json
响应
订阅现在会向 vaulted_token 中的支付方式扣款。result 说明它是新关联、已变更,还是本来就在使用该支付方式。
此页面对您有帮助吗?
⌘I
设置外部订阅支付方式
curl --request POST \
--url https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method \
--header 'Content-Type: application/json' \
--header 'X-Idempotency-Key: <x-idempotency-key>' \
--header 'private-secret-key: <api-key>' \
--header 'public-api-key: <api-key>' \
--data '
{
"account_id": "9104911d-5df9-429e-8488-ad41abea1a4b",
"customer_payer": {
"id": "cfae0941-7234-427a-a739-ef4fce966c79"
},
"vaulted_token": "2deff5c9-da62-4888-8f05-df76ab2967a5",
"external_customer_id": "cus_RplXYZ"
}
'import requests
url = "https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method"
payload = {
"account_id": "9104911d-5df9-429e-8488-ad41abea1a4b",
"customer_payer": { "id": "cfae0941-7234-427a-a739-ef4fce966c79" },
"vaulted_token": "2deff5c9-da62-4888-8f05-df76ab2967a5",
"external_customer_id": "cus_RplXYZ"
}
headers = {
"X-Idempotency-Key": "<x-idempotency-key>",
"public-api-key": "<api-key>",
"private-secret-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {
'X-Idempotency-Key': '<x-idempotency-key>',
'public-api-key': '<api-key>',
'private-secret-key': '<api-key>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
account_id: '9104911d-5df9-429e-8488-ad41abea1a4b',
customer_payer: {id: 'cfae0941-7234-427a-a739-ef4fce966c79'},
vaulted_token: '2deff5c9-da62-4888-8f05-df76ab2967a5',
external_customer_id: 'cus_RplXYZ'
})
};
fetch('https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'account_id' => '9104911d-5df9-429e-8488-ad41abea1a4b',
'customer_payer' => [
'id' => 'cfae0941-7234-427a-a739-ef4fce966c79'
],
'vaulted_token' => '2deff5c9-da62-4888-8f05-df76ab2967a5',
'external_customer_id' => 'cus_RplXYZ'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-Idempotency-Key: <x-idempotency-key>",
"private-secret-key: <api-key>",
"public-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method"
payload := strings.NewReader("{\n \"account_id\": \"9104911d-5df9-429e-8488-ad41abea1a4b\",\n \"customer_payer\": {\n \"id\": \"cfae0941-7234-427a-a739-ef4fce966c79\"\n },\n \"vaulted_token\": \"2deff5c9-da62-4888-8f05-df76ab2967a5\",\n \"external_customer_id\": \"cus_RplXYZ\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("X-Idempotency-Key", "<x-idempotency-key>")
req.Header.Add("public-api-key", "<api-key>")
req.Header.Add("private-secret-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method")
.header("X-Idempotency-Key", "<x-idempotency-key>")
.header("public-api-key", "<api-key>")
.header("private-secret-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"account_id\": \"9104911d-5df9-429e-8488-ad41abea1a4b\",\n \"customer_payer\": {\n \"id\": \"cfae0941-7234-427a-a739-ef4fce966c79\"\n },\n \"vaulted_token\": \"2deff5c9-da62-4888-8f05-df76ab2967a5\",\n \"external_customer_id\": \"cus_RplXYZ\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api-sandbox.y.uno/v1/external-subscriptions/{external_subscription_id}/payment-method")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-Idempotency-Key"] = '<x-idempotency-key>'
request["public-api-key"] = '<api-key>'
request["private-secret-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"account_id\": \"9104911d-5df9-429e-8488-ad41abea1a4b\",\n \"customer_payer\": {\n \"id\": \"cfae0941-7234-427a-a739-ef4fce966c79\"\n },\n \"vaulted_token\": \"2deff5c9-da62-4888-8f05-df76ab2967a5\",\n \"external_customer_id\": \"cus_RplXYZ\"\n}"
response = http.request(request)
puts response.read_body{
"external_subscription_id": "sub_1QF2abc",
"account_id": "9104911d-5df9-429e-8488-ad41abea1a4b",
"customer_payer": {
"id": "cfae0941-7234-427a-a739-ef4fce966c79"
},
"vaulted_token": "2deff5c9-da62-4888-8f05-df76ab2967a5",
"external_customer_id": "cus_RplXYZ",
"result": "LINKED"
}