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
什麼可能會丟失或什麼是錯的?謝謝。
這我不清楚你的代碼中的前端是如何決定張貼到insertCharge()方法。你的表單上似乎沒有'action'屬性。另外,你可能不想用Yii來生成表單。從頭開始構建表單幾乎肯定是100%,因爲Checkout將會提供一些魔術來發布所提供的操作,並在發出POST請求時重寫。 – korben
所以你建議我把一個動作方法發佈令牌?即
是的!這或多或少是我所掌握的。在這種情況下,只需從Yii拿走腳手架即可確保Google Checkout按預期行事。 – korben