You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP对接PayPal定期付款无限周订阅设置失效问题咨询

问题原因

你提交的请求参数存在JSON语法错误,是导致所有配置不生效的核心原因:

  • 代码中计划类型字段写为"type: INFINITE",属于非法JSON结构,键和值没有拆分,PayPal接口无法正确识别该字段,会默认将计划判定为固定周期类型,因此只执行1次就自动终止。
  • 手动拼接JSON字符串极易出现这类语法错误,也会导致interval_unit等其他参数解析异常。

修复方案

  1. 修正JSON中计划类型的语法,将错误的"type: INFINITE"改为合法键值对:
"type": "INFINITE"
  1. 建议改用PHP内置的json_encode()方法生成请求参数,避免手动拼接带来的语法错误,参考优化后的代码如下:
public function createNewPlan($name, $description, $amount)
{
    if (strlen($this->productId) <= 0) {
        return false;
    }
    
    $header = [
        'Content-type: application/json',
        'Authorization: Bearer ' . $this->token
    ];
    $url = $this->apiUrl . '/v1/billing/plans';
    
    // 用数组构造参数,json_encode自动生成合法JSON
    $params = [
        'product_id' => $this->productId,
        'name' => $name,
        'description' => $description,
        'type' => 'INFINITE',
        'billing_cycles' => [
            [
                'frequency' => [
                    'interval_unit' => 'WEEK',
                    'interval_count' => 1
                ],
                'tenure_type' => 'REGULAR',
                'sequence' => 1,
                'total_cycles' => 0,
                'pricing_scheme' => [
                    'fixed_price' => [
                        'value' => (string)$amount,
                        'currency_code' => 'EUR'
                    ]
                ]
            ]
        ],
        'payment_preferences' => [
            'auto_bill_outstanding' => true,
            'setup_fee' => [
                'value' => '0',
                'currency_code' => 'EUR'
            ],
            'setup_fee_failure_action' => 'CONTINUE',
            'payment_failure_threshold' => 2
        ]
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    $err = curl_errno($ch);
    $errmsg = curl_error($ch);
    curl_close($ch);
    
    if ($err || $errmsg) {
        return false;
    }
    
    $json = json_decode($result);
    return $json->id ?? false;
}

验证要点

修复后在沙箱后台查看计划详情,确认:

  • 计划类型显示为「无限期」
  • 计费周期显示为每周1次
  • 总周期数显示为无限

内容的提问来源于stack exchange,提问作者Ahmet Eroğlu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 00:06:04