5

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.

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

What could be missing or what is wrong? Thanks.

Jan Beeck
  • 303
  • 8
  • 24
  • It's unclear to me from your code how the front-end is deciding to post to the insertCharge() method. There doesn't seem to be an `action`-attribute on your form. Additionally, you probably don't want to use Yii to generate the form. It's almost definitely 100% wiser to build the form from scratch, as Checkout is going to do some magic to post to the action provided and will override when the POST-request is made. – korben Dec 14 '16 at 21:05
  • So you are suggesting me to put an action method to post the token? i.e.
    – Jan Beeck Dec 14 '16 at 21:31
  • Yep! That's more or less what I'm getting at. Just take away the scaffolding from Yii in this case to ensure that Checkout behaves as expected. – korben Dec 14 '16 at 22:26
  • I have the following header and no scaffolding from Yii2
    Since I am working the stripeToken on charge.php and on Yii2. However I got this error: "Bad Request (#400) Unable to verify your data submission." Any idea of how to overcome this issue? Thanks.
    – Jan Beeck Dec 15 '16 at 04:10
  • Regarding "Bad Request (#400) Unable to verify your data submission." error - it is usually caused by csrf validation problem. You can disable Yii2's csrf validation for a particular action - in your case which seems to `actionCharge()` in `SiteController`. For more details - http://stackoverflow.com/questions/27126050/getting-bad-request-400-on-ajax-calls-using-yii-2 – sm1979 Dec 15 '16 at 06:57
  • Thank you very much it worked with the latter. – Jan Beeck Dec 16 '16 at 00:48

1 Answers1

3

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

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');
}
Jan Beeck
  • 303
  • 8
  • 24