与 php 共享客户条带和创建费用时出错

Error sharing customer stripe and create charges with php

对不起我的英语。

我正在尝试使用独立帐户在 stripe 中使用关联帐户创建费用。我正在使用这篇文章连接我的帐户:Sample OAuth Applications。我有 3 个帐户:A) 主帐户。在这个帐户中是我们应用程序的客户。账号B和账号C是账号A关联的其他账号(我在账号A的设置里看到这两个账号是关联的,这说明两个账号已经关联了,对吧?)。所有用户的应用程序都在帐户 A 中,我们希望共享到帐户 B 和 C,这是我们的目标。

连接帐户后,我们将 acces_token 和刷新令牌保存在我们的数据库中。共享客户的 docs 说我们需要用 customer_id 和 account_id 创建一个令牌,以便用客户的信用卡信息创建一个令牌,但是当我们执行代码时它returns 错误:

提供的密钥 'sk_test_********************uJ3l' 无权访问帐户 'acct_--------------'(或该帐户不存在)。应用程序访问权限可能已被撤销。

我们测试了传递给 api 的几种变量组合,结果总是 returns 错误。

在这种情况下做错了什么。你能帮帮我们吗???

这是我们使用的代码:所有评论都是我们尝试的另一个测试:

public function createtransaction($customer_id, $paymentDesc, $destinationAccount) {

        $errors = array();
        try {
            $secret_key = $this->get_secret_key();

            \Stripe\Stripe::setApiKey($secret_key);

            // THIS ONLY FOR TESTING PURPOSE. WE WANT SEE IF CAN ACCESS TO CONNECTED ACCOUNT
            // Fetching an account just needs the ID as a parameter
            // WHIT ACCESS_TOKEN WORKS THE RETRIEVE METHOD
            // $account = \Stripe\Account::retrieve('access_token_from_db');
            // WHIT ACCOUNT_ID DOES NOT. RETURNS AN ERROR
            // $account = \Stripe\Account::retrieve('acct_***********');
            // echo "<pre>------";
            // print_r($account);
            // echo "-----</pre>";


            // HERE ONLY WE CAN TEST IF WE CAN CREATE A CUSTOMER. THIS IS ONLY TO CHECK IF THE ACCOUNT IS CONNECTED. THE CUSTOMER ALREADY EXISTS IN MAIN ACCOUNT 
            // Recommended: sending API key with every request
            // \Stripe\Customer::create(
            //   array("description" => "example@stripe.com"),
            //   array("api_key" => 'sk_test_***************') // account's access token from the Connect flow
            // SAME. WITH stripe_account DOES NOT WORK. WHIT api_key PARAM WORKS WELL
            // );

            // THE DOCS SAYS WE NEED CREATE A TOEKN WITH customer AND stripe_account, BUT IT RETURNS AN ERROR OF PERMISSION
            // Create a Token from the existing customer on the platform's account
            // $token = \Stripe\Token::create(
            //  array("customer" => $customer_id),
            //  array("stripe_account" => 'acct_********************') // id of the connected account
            // );

            // echo "<pre>------";
            // print_r($token);
            // echo "-----</pre>";

            // ONE COMBINATION. DOES NOT WORK
            // var_dump($customer_id, $paymentDesc, $destinationAccount);
            // $charge = \Stripe\Charge::create(array(
            //  "amount" => $paymentDesc['total'] * 100,
            //  "currency" => "usd",
            //  "source" => $token,
            //  "description" => $paymentDesc['description'],
            //  'destination' => $destinationAccount
            // ));
            //
            //

            // IT WORKS IN THE MAIN ACCOUNT BUT IT IS NOT OUR GOAL, BUT WE CAN TRANSFER THE MONEY TO ANOTHER CONNECTED ACCOUNT
            // $charge = \Stripe\Charge::create(array(
            //  'amount' => $paymentDesc['total'] * 100,
            //  'currency' => 'usd',
            //  'customer' => $customer_id,
            //  "description" => $paymentDesc['description']
            // ));

            // ANOTHER COMBINATION. DOES NOT WORK
            /*$charge = \Stripe\Charge::create(array(
                'amount' => $paymentDesc['total'] * 100,
                'currency' => 'usd',
                "description" => $paymentDesc['description'],
                'customer' => $customer_id
            ), array('stripe_account' => 'acct_************'));*/

            // ANOTHER COMBINATION. DOES NOT WORK
            /*$charge = \Stripe\Charge::create(array(
                'amount' => $paymentDesc['total'] * 100,
                'currency' => 'usd',
                "description" => $paymentDesc['description'],
                'customer' => $customer_id
            ), array('api_key' => 'ACCESS_TOKEN_IN_DB'));

            echo "<pre>------";
            print_r($charge);
            echo "-----</pre>";

            return array('charge' => $charge);*/

        } catch(Stripe_CardError $e) {
            $errors = array('error' => false, 'message' => 'Card was declined.', 'e' => $e);
        } catch (Stripe_InvalidRequestError $e) {
            $errors = array('error' => false, 'message' => 'Invalid parameters were supplied to Stripe\'s API', 'e' => $e);
        } catch (Stripe_AuthenticationError $e) {
            $errors = array('error' => false, 'message' => 'Authentication with Stripe\'s API failed!', 'e' => $e);
        } catch (Stripe_ApiConnectionError $e) {
            $errors = array('error' => false, 'message' => 'Network communication with Stripe failed', 'e' => $e);
        } catch (Stripe_Error $e) {
            $errors = array('error' => false, 'message' => 'Stripe error. Something wrong just happened!', 'e' => $e);
        } catch (Exception $e) {
            $errors = array('error' => false, 'message' => 'An error has ocurred getting info customer.', 'e' => $e);
        }

        return $errors;
    }

您不需要保存 access_tokenrefresh_token,只需要包含连接帐户 ID 的 stripe_user_id 即可使用 Connect。

您可以对所有请求使用您平台的秘密 API 密钥,并将连接的帐户 ID 传递给 Stripe-account header,如共享客户文档中所示。更一般地说,这个 header 可以添加到几乎每个 API 请求中,以将其定向到连接的帐户:

https://stripe.com/docs/connect/authentication#authentication-via-the-stripe-account-header

在你的情况下你会这样做:

// Create a Token from the existing customer on the platform's account
$sharedToken = \Stripe\Token::create(
  array("customer" => CUSTOMER_ID, "card" => CARD_ID),
  array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID) // id of the connected account
);

$sharedCustomer = \Stripe\Customer::create(array(
  "description" => "Customer for noah.robinson@example.com",
  "source" => $sharedToken),
  array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID) 
);

然后您可以用同样的方式从这个共享客户创建费用:

\Stripe\Charge::create(array(
  "amount" => 2000,
  "currency" => "usd",
  "customer" => $sharedCustomer),
  array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID) 
);