如何将条纹支付整合到Yii2中?

时间:2016-12-14 20:09:28

标签: php yii2 stripe-payments

我有以下代码,它运行没有错误,但它不会将资金插入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']);

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

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

可能缺少什么或出了什么问题?感谢。

1 个答案:

答案 0 :(得分:3)

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

send.php

<form action="index.php?r=site%2Fcharge" method="post">

SiteController.php

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

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