2016-12-14 18 views
3

我有以下代碼,它運行時沒有錯誤,但它不會將資金插入到條帶服務器上。條紋庫安裝正確。如何將條紋支付整合到Yii2中?

的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' => '[email protected]', 
      'source' => $token 
    )); 

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

    }//end function 

}//end class 

什麼可能會丟失或什麼是錯的?謝謝。

+0

這我不清楚你的代碼中的前端是如何決定張貼到insertCharge()方法。你的表單上似乎沒有'action'屬性。另外,你可能不想用Yii來生成表單。從頭開始構建表單幾乎肯定是100%,因爲Checkout將會提供一些魔術來發布所提供的操作,並在發出POST請求時重寫。 – korben

+0

所以你建議我把一個動作方法發佈令牌?即

+0

是的!這或多或少是我所掌握的。在這種情況下,只需從Yii拿走腳手架即可確保Google Checkout按預期行事。 – korben

回答

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'); 
} 
+0

很高興看到你的工作! – korben