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

WordPress中Stripe订阅X个月后自动取消方案咨询

无需第三方工具实现Stripe订阅自动取消及WordPress站内支付方案

一、Stripe原生实现订阅到期自动取消(99%+可靠)

直接用Stripe自带功能,完全摆脱第三方工具依赖,稳定性拉满:

  • 新建产品时直接配置固定期限:
    在Stripe后台创建按月订阅产品时,选择「订阅」定价模型,开启「固定期限」选项,分别设置3、6、12个月的期限,到期后订阅会自动取消。这是Stripe原生支持的功能,无第三方环节,可靠性最高。
  • 已有订阅补设自动取消:
    针对已存在的订阅,可在Stripe后台找到对应订阅,进入「订阅日程」页面,添加「取消」动作并设置触发时间为订阅开始后的第3/6/12个月;也可以调用Stripe的subscriptionSchedules.createAPI,指定end_behavior为cancel并设置结束时间。

二、Webhooks+自定义定时任务(灵活场景备选)

如果需要更灵活的取消规则(比如根据用户后续行为调整取消时间),可以用Stripe Webhooks配合服务器定时任务:

  • 监听Stripe的customer.subscription.created事件,当用户创建订阅时,记录订阅ID和目标取消时间。
  • 用服务器级别的Cron定时任务(比WordPress内置定时更可靠,后者需要站点有访问触发),到时间调用Stripe的subscriptions.cancelAPI执行取消操作。

三、WordPress站内集成Stripe支付(无需跳转)

两种方案可选,都能实现用户在站内完成支付:

  • 插件方案:如果用WooCommerce,直接用官方的Stripe for WooCommerce插件;不用WooCommerce的话,选Simple Stripe Payments这类轻量插件,配置好Stripe密钥后就能生成站内支付表单,用户无需跳转到Stripe页面。
  • 自定义代码方案(适合有开发能力的场景):
    1. 在WordPress主题的functions.php中引入Stripe JS库并配置参数:
      function load_stripe_scripts() {
        wp_enqueue_script('stripe', 'https://js.stripe.com/v3/', array(), null, true);
        wp_enqueue_script('site-stripe-payment', get_template_directory_uri() . '/js/stripe-pay.js', array('stripe'), null, true);
        wp_localize_script('site-stripe-payment', 'stripe_data', array(
          'pk' => '你的Stripe公钥',
          'ajax_url' => admin_url('admin-ajax.php')
        ));
      }
      add_action('wp_enqueue_scripts', 'load_stripe_scripts');
      
    2. 前端写支付表单,用Stripe Elements收集卡片信息,通过AJAX提交到后台:
      const stripe = Stripe(stripe_data.pk);
      const elements = stripe.elements();
      const cardEl = elements.create('card');
      cardEl.mount('#card-element');
      
      document.getElementById('pay-form').addEventListener('submit', async (e) => {
        e.preventDefault();
        const { error, paymentMethod } = await stripe.createPaymentMethod({
          type: 'card',
          card: cardEl,
        });
      
        if (!error) {
          fetch(stripe_data.ajax_url, {
            method: 'POST',
            body: JSON.stringify({
              action: 'make_stripe_sub',
              pm_id: paymentMethod.id,
              price_id: '对应Stripe定价计划ID(3/6/12个月分别对应)'
            }),
            headers: { 'Content-Type': 'application/json' }
          });
        }
      });
      
    3. 后台写AJAX处理函数创建订阅:
      function make_stripe_subscription() {
        require_once(get_template_directory() . '/vendor/autoload.php');
        \Stripe\Stripe::setApiKey('你的Stripe秘钥');
      
        $post_data = json_decode(file_get_contents('php://input'), true);
        $customer = \Stripe\Customer::create([
          'payment_method' => $post_data['pm_id'],
          'email' => wp_get_current_user()->user_email,
          'invoice_settings' => ['default_payment_method' => $post_data['pm_id']],
        ]);
      
        $subscription = \Stripe\Subscription::create([
          'customer' => $customer->id,
          'items' => [['price' => $post_data['price_id']]],
          'payment_behavior' => 'default_incomplete',
          'expand' => ['latest_invoice.payment_intent'],
        ]);
      
        wp_send_json_success($subscription);
      }
      add_action('wp_ajax_make_stripe_sub', 'make_stripe_subscription');
      add_action('wp_ajax_nopriv_make_stripe_sub', 'make_stripe_subscription');
      

内容的提问来源于stack exchange,提问作者Luciano

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 23:15:37