首页 文章

获取令牌以使用Stripe创建客户

提问于
浏览
5

我正在使用 Checkout 在我的ASP.NET核心应用程序中实现Stripe .

我知道如何使用 Checkout 获取信用卡收费令牌,但我在哪里获得令牌来创建客户?

在文档中,我看到我需要获取令牌来创建客户,但不确定该令牌来自何处 . https://stripe.com/docs/api/dotnet#create_customer

据我所知,一个令牌只能使用一次,因此它不能与我在收取信用卡之前获得相同的标记 .

2 回答

  • 4

    正如我在这里引用条纹document

    收集客户的付款信息时,会创建条带标记 . 此令牌只能使用一次,但这并不意味着您必须为每笔付款请求客户的卡详细信息 . Stripe提供了一个Customer对象,可以轻松保存此信息以及其他信息供以后使用 . 您可以使用Customer对象创建订阅或未来的一次性费用 .

    您必须做的是创建一个客户,同时从客户那里获取卡详细信息并向该客户收取费用 .

    使用以下代码片段进行操作,这样您将使用单个令牌创建客户并收费

    StripeConfiguration.SetApiKey(secret_key_of_your_account);
    var token = model.Token; // Using ASP.NET MVC
    
    var customers = new StripeCustomerService();
    var charges = new StripeChargeService();
    
    var customer = customers.Create(new StripeCustomerCreateOptions {
      Email = "paying.user@example.com",
      SourceToken = token
    });
    
    // YOUR CODE: Save the customer ID and other info in a database for later.
    
    // YOUR CODE (LATER): When it's time to charge the customer again, retrieve the customer ID.
    var charge = charges.Create(new StripeChargeCreateOptions {
      Amount = 1500, // $15.00 this time
      Currency = "usd",
      CustomerId = customer.Id
    });
    

    阅读参考文档了解更多详情

  • 0
    \Stripe\Stripe::setApiKey("----");
      \Stripe\Stripe::setApiKey(".................");
         $token=   \Stripe\Token::create(array(
         "card" => array(
           "number" => "4242424242424242",
           "exp_month" => 1,
           "exp_year" => 2019,
           "cvc" => "314"
         )
       ));
       $request['stripe_token'] =$token['id'];
      // When Contact person have not Stripe Customer id then we have to do the following process.
         try {
          $customer = \Stripe\Customer::create([
            "description" => "Customer for ".$contactDetails['email'],
            "source" => $request['stripe_token'] // obtained with Stripe.js
          ]);
           // update its customerid in the contact table
    
          // Create Customer then save its id in table and use the customer id when you are  verifiying the customer token
          $result=  \Stripe\Charge::create(array(
            "amount" => $request['amount'],
            "currency" => $request['currency'],
            "customer" => $customer
          ));
          $status = $result['succeeded'];
           if($result['status'] == "succeeded"){
               $success = 'Your payment was successful.';
               $all['payment_done'] = "1";
              $FinalArray = array('status'=>'true','message'=>'Your payment done successful.','result'=>$all);
           }else{
             $FinalArray = array('status'=>'fail','message'=>'Your Token is not generated successfully','result'=>[]);
           }
    
      }
         catch (Exception $e) {
           $error = $e->getMessage();
            $all['payment_done'] = "0";
            $FinalArray = array('status'=>'false','message'=>'The Stripe token id is not correctly','result'=>$all);
         }
    

相关问题