条带:客户的默认来源是来源,无法与现有客户共享

问题描述 投票:0回答:2

我想在我的网站上使用Stripe Connect。我创建了关联帐户和客户,但在尝试与关联帐户共享客户时出错。我明白了:

“您提供的客户没有指定来源。客户的默认来源是来源,无法与现有客户共享。”

Hier是我正在使用的代码:

function addSource($source){    
    $this->source = $source;
}

function addCustomer(){

    $customer = \Stripe\Customer::create(array(
      "description" => "Customer ".$this->getCustomerName(),
      "email" => $this->getCustomerEmail(),
      "source" => $this->source 
                            ));
    $this->customer = $customer;
}

function createAccount(){

        $account = \Stripe\Account::create(array(
            "country" => "FR",
            "type" => "custom"
        ));

    $this->account = $account->id;
}

function connectCustomer(){
    $token = \Stripe\Token::create(array(
      "customer" => $this->customer->id
    ), array("stripe_account" => $this->account));

    $copiedCustomer = \Stripe\Customer::create(array(
    "description" => "Customer for [email protected]",
    "source" => $token->id
    ), array("stripe_account" => $this->account));

    $this->copiedCustomer = $copiedCustomer;
}

通过调试我看到当我尝试在connectCustomer函数中创建$ token时发生问题。客户已在我的条带仪表板上添加了正确的来源。该帐户也已创建。之后我的目标是订阅客户到关联的帐户。我已经成功订阅了他而没有使用Stripe Connect,但现在我需要使用它。我试图在许多其他论坛中找到解决方案,但没有找到类似的东西。

谢谢你的帮助 !

php stripe-connect
2个回答
0
投票

你的代码看起来很好。我不确定为什么它不起作用。也许缺少一些步骤?在创建连接订阅时遇到同样的问题,因为我试图将平台客户与连接计划相关联。错误消息不是很有帮助。我搜索谷歌的消息,并发现了这个问题。来吧,我能够使用平台客户创建一次性连接费用(在我创建共享源之后)。为什么我不能在这里做同样的事情?因为Subscription API需要客户而不是共享源。条纹演示代码没有多大帮助,直到我仔细阅读文档中的前4个项目符号点:https://stripe.com/docs/connect/subscriptions(一个Ahhhhah时刻!)

在Connect中使用订阅具有以下限制:

  • 必须在连接的帐户(而不是您的平台帐户)上创建客户和计划
  • 必须直接在连接的帐户上创建订阅(不支持使用destination参数)
  • 您的平台无法更新或取消未创建的订阅
  • 您的平台无法将application_fee添加到未创建的发票或包含未创建的发票项目的发票中

所以,我只是发布一些伪代码,希望它能帮助下一个遇到上述错误信息的人。

  1. 您必须从前端/客户端javascript创建要重用的源(而不是令牌): stripe.createSource(card,ownerInfo)
  2. 然后,您将使用此源(stripe_token)在平台帐户(stripe_customer_id)上创建客户。这对于一次性连接充电非常有用(如果您需要它)。这也是为了存储原始源(stripe_token),以便您可以稍后为connected / txn_customer_id创建新的可重用令牌/源。

从第3步开始,所有代码都在chargeMonthly函数内部:

  1. 通过提供唯一名称(上面的第3个要点)确保订阅计划是由平台创建的连接计划。
  2. 接下来,使用platform / stripe_customer_id创建一个新的可重用源。
  3. 使用新的可重用源在已连接的帐户上创建客户(txn_customer_id)。
  4. 使用已连接的/ txn_customer_id和已连接的plan_id创建订阅。

public function createNewCustomer(&$input, $forConnect = false) {
  try {
    // Create new stripe customer
    if ($forConnect) {
      $cu = \Stripe\Customer::create(array(
        'email' => $input['email'],
        'source' => $input['connect_token']
      ), array("stripe_account" => $input['txn_account_id']));

      $input['txn_customer_id'] = $cu->id;
    }
    else {
      $cu = \Stripe\Customer::create(array(
        'email' => $input['email'],
        'source' => $input['stripe_token']
      ));

      $input['stripe_customer_id'] = $cu->id;
      $input['txn_customer_id'] = $cu->id;
    }
  } catch (\Stripe\Error\Base $e1) {
    // log error
    \Log::error($e1);
    return false;
  } catch(\Stripe\Error\Card $e2) {
    \Log::error($e2);
    return false;
  } catch (Exception $e) {
    \Log::error($e);
    return false;
  }

  return true;
}

public function chargeMonthly(&$input, $qty = 1) {
  $plan_name = 'yourplatformname-' . $input['amount'] .'-' . $input['banner_slug'];

  // attempt to retrieve monthly plan
  // if not found, create new plan
  try {
    $plan = \Stripe\Plan::retrieve($plan_name, array("stripe_account" => $input['txn_account_id']));
  } catch(\Stripe\Error\InvalidRequest $e1) {
    // ignore error
    // \Log::error($e1);
  } catch(Exception $e) {
    // ignore error
    // \Log::error($e);
  }

  try {
    // create new if not found
    if(empty($plan)) {
      $plan = \Stripe\Plan::create(array(
          'amount'    => $input['amount'],
          'interval'  => 'month',
          'currency'  => 'usd',
          'id'        => $plan_name,
          "product" => array(
            "name"    => $plan_name
          )
        ), array("stripe_account" => $input['txn_account_id']));
    }

    $token = \Stripe\Source::create(array(
      'customer' => $input['stripe_customer_id'],
      'usage' => 'reusable'
    ), array("stripe_account" => $input['txn_account_id']));

    $input['connect_token'] = $token->id;
    $this->createNewCustomer($input, true);

    $sub = \Stripe\Subscription::create(array(
      'customer'                => $input['txn_customer_id'],
      'plan'                    => $plan->id,
      'quantity'                => $qty,
      'application_fee_percent' => $input['application_fee_percent']),
      array('stripe_account' => $input['txn_account_id'])
    );

    $input['txn_id'] = $sub->id;
    $input['txn_log'] = json_encode($sub);
    $input['recurrence_name'] = $plan->id;

    // success
    return true;
  } catch(\Stripe\Error\InvalidRequest $e1) {
    // ignore error
    \Log::error($e1);
    return false;
  } catch(Exception $e) {
    \Log::error($e);
    return false;
  }
}

0
投票

我知道已经太晚了,但这里有一些对我有用的东西。我有同样的错误。我通过创建源而不是令牌来解决它。

所以,替换

 $token = \Stripe\Token::create(array(
      "customer" => $this->customer->id
    ), array("stripe_account" => $this->account));

     $token = \Stripe\Source::create([
                "customer" => $this->customer->id,
            ], ["stripe_account" => $this->account]);

© www.soinside.com 2019 - 2024. All rights reserved.