如何将 Stripe 支付整合到 Yii2 中? [英] How to integrate Stripe payments into Yii2?

查看:52
本文介绍了如何将 Stripe 支付整合到 Yii2 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code, it runs without error, however it does not insert funds onto the Stripe server. The Stripe library is installed correctly.

配置文件

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

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

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

SiteController.php

SiteController.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

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

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

What could be missing or what is wrong? Thanks.

推荐答案

我通过移除视图上的 Yii2 表单脚手架并在控制器上添加 beforeAction 来解决该问题.

I resolved the issue by removing the Yii2 form scaffolding on the view and adding a beforeAction on the controller.

发送.php

SiteController.php

SiteController.php

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

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

这篇关于如何将 Stripe 支付整合到 Yii2 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆