【问题标题】:How to integrate Stripe payments into Yii2?如何将 Stripe 支付集成到 Yii2 中?
【发布时间】:2017-04-30 07:09:15
【问题描述】:

我有以下代码,它运行没有错误,但是它不会将资金插入到 Stripe 服务器。 Stripe 库安装正确。

config.php

    <?php
    //require_once('vendor/autoload.php');

    $stripe = array(
      "secret_key"      => "sk_test_key",
      "publishable_key" => "pk_test_key"
    );

\Stripe\Stripe::setApiKey($stripe['secret_key']);

站点控制器.php

public function actionSend()
    {
        $model = new SendForm();

            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->insertCharge(); 
                //Yii::$app->session->setFlash('Successfully charged $20.00!');
                return $this->render('send-confirm', ['model' => $model]);
            } else {
                return $this->render('send', [
                    'model' => $model,
                ]);
            }

    }// end function

发送.php

    <?php $form = ActiveForm::begin(['options' => ['method' => 'post']]); ?>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="<?php echo $stripe['publishable_key']; ?>"
    data-name="TEST"
    data-description="Testing"
    data-amount="2000"
    data-locale="auto">

   </script>
   <?php ActiveForm::end(); ?>

SendForm.php

class SendForm extends Model
{   

   public function insertCharge()
   {

     \Stripe\Stripe::setApiKey(Yii::$app->stripe->secret_key);

      $request = Yii::$app->request;

      $token = $request->post('stripeToken');

      //$token  = $_POST['stripeToken'];

      $customer = \Stripe\Customer::create(array(
          'email' => 'customer@example.com',
          'source'  => $token
      ));

      $charge = \Stripe\Charge::create(array(
          'customer' => $customer->id,
          'amount'   => 2000,
          'currency' => 'usd'
      ));

   }//end function

}//end class

可能缺少什么或有什么问题?谢谢。

【问题讨论】:

  • 从您的代码中我不清楚前端如何决定发布到 insertCharge() 方法。您的表单上似乎没有action-属性。此外,您可能不想使用 Yii 来生成表单。从头开始构建表单几乎肯定 100% 更明智,因为 Checkout 会做一些魔术来发布到提供的操作,并且会在发出 POST 请求时覆盖。
  • 所以你建议我放一个动作方法来发布令牌?即
  • 是的!这或多或少是我的意思。在这种情况下,只需从 Yii 中移除脚手架,以确保 Checkout 的行为符合预期。
  • 我有以下标题,并且没有来自 Yii2 的脚手架
    因为我在 Charge.php 上使用 stripeToken 和在 Yii2 上。但是我收到了这个错误:“错误的请求 (#400) 无法验证您的数据提交。”知道如何克服这个问题吗?谢谢。
  • 关于“错误请求 (#400) 无法验证您的数据提交。”错误 - 它通常是由 csrf 验证问题引起的。您可以为特定操作禁用 Yii2 的 csrf 验证 - 在您的情况下,这似乎是 SiteController 中的 actionCharge()。更多详情 - stackoverflow.com/questions/27126050/…

标签: php yii2 stripe-payments


【解决方案1】:

我通过移除视图上的 Yii2 表单脚手架并在控制器上添加 beforeAction 解决了这个问题。

发送.php

&lt;form action="index.php?r=site%2Fcharge" method="post"&gt;

站点控制器.php

public function beforeAction($action)
{
    $this->enableCsrfValidation = false;
    return parent::beforeAction($action);
}

public function actionCharge()
{
    return $this->render('charge');
}

【讨论】:

    猜你喜欢
    • 2020-10-26
    • 2022-06-12
    • 2023-03-03
    • 1970-01-01
    • 2012-10-12
    • 2019-09-28
    • 2018-12-17
    • 2020-02-26
    • 2013-04-29
    相关资源
    最近更新 更多