Cancel a subscription at period end
curl --request POST \
--url https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel \
--header 'X-API-Key: <api-key>'import requests
url = "https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel"
headers = {"X-API-Key": "<api-key>"}
response = requests.post(url, headers=headers)
print(response.text)const options = {method: 'POST', headers: {'X-API-Key': '<api-key>'}};
fetch('https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel', 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://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"X-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"
"net/http"
"io"
)
func main() {
url := "https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("X-API-Key", "<api-key>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel")
.header("X-API-Key", "<api-key>")
.asString();require 'uri'
require 'net/http'
url = URI("https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-API-Key"] = '<api-key>'
response = http.request(request)
puts response.read_body{
"data": {
"amountCents": 123,
"billingInterval": "DAY",
"cancelAtPeriodEnd": true,
"canceledAt": "<string>",
"createdAt": "<string>",
"currency": "<string>",
"currentPeriodEnd": "<string>",
"customerEmail": "<string>",
"customerId": "<string>",
"endedAt": "<string>",
"id": "<string>",
"initialOrderId": "<string>",
"latestOrderId": "<string>",
"productId": "<string>",
"productName": "<string>",
"status": "<string>",
"subscriptionId": "<string>",
"updatedAt": "<string>"
},
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}Merchant API 端点
取消订阅
安排有效订阅在当前计费周期结束时取消。
POST
/
v1
/
subscriptions
/
{id}
/
cancel
Cancel a subscription at period end
curl --request POST \
--url https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel \
--header 'X-API-Key: <api-key>'import requests
url = "https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel"
headers = {"X-API-Key": "<api-key>"}
response = requests.post(url, headers=headers)
print(response.text)const options = {method: 'POST', headers: {'X-API-Key': '<api-key>'}};
fetch('https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel', 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://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"X-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"
"net/http"
"io"
)
func main() {
url := "https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("X-API-Key", "<api-key>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel")
.header("X-API-Key", "<api-key>")
.asString();require 'uri'
require 'net/http'
url = URI("https://merchant-api-prod.anyway.sh/v1/subscriptions/{id}/cancel")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["X-API-Key"] = '<api-key>'
response = http.request(request)
puts response.read_body{
"data": {
"amountCents": 123,
"billingInterval": "DAY",
"cancelAtPeriodEnd": true,
"canceledAt": "<string>",
"createdAt": "<string>",
"currency": "<string>",
"currentPeriodEnd": "<string>",
"customerEmail": "<string>",
"customerId": "<string>",
"endedAt": "<string>",
"id": "<string>",
"initialOrderId": "<string>",
"latestOrderId": "<string>",
"productId": "<string>",
"productName": "<string>",
"status": "<string>",
"subscriptionId": "<string>",
"updatedAt": "<string>"
},
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}{
"data": "<unknown>",
"error": {
"code": "<string>",
"message": "<string>"
},
"message": "<string>",
"success": true
}请使用属于目标组织、且为全部权限或勾选了
sales:subscriptions:write 权限的 API 密钥进行认证。
- 在当前计费周期结束时取消。买家在
currentPeriodEnd前仍可使用,cancelAtPeriodEnd变为true。 - Anyway 会向买家发送取消确认邮件,并触发常规的订阅 Webhook,无需你另行发送。
- 可安全重试:对已安排取消的订阅再次调用,返回
200和相同的响应内容。 - 响应与查看订阅记录返回的订阅对象相同。
| 状态码 | 错误码 | 含义 |
|---|---|---|
403 | INSUFFICIENT_SCOPE | 密钥缺少 sales:subscriptions:write。 |
404 | 订阅 ID 不存在,或属于其他组织。 | |
409 | SUBSCRIPTION_NOT_CANCELABLE | 订阅不是有效状态(待支付、已暂停、已取消、已过期或已结束)。 |
500 | INTERNAL_ERROR | 我方内部错误。取消可能已生效,可安全重试。 |
502 | PROVIDER_ERROR | 支付服务商调用失败。取消可能未生效,可安全重试。 |