【发布时间】: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 的脚手架
-
关于“错误请求 (#400) 无法验证您的数据提交。”错误 - 它通常是由 csrf 验证问题引起的。您可以为特定操作禁用 Yii2 的 csrf 验证 - 在您的情况下,这似乎是
SiteController中的actionCharge()。更多详情 - stackoverflow.com/questions/27126050/…
标签: php yii2 stripe-payments