嗨,伙计们,我已经创建了一种 stipe 支付方式,它工作正常,但我想升级到 Stripe v3,但我不知道如何将旧方法转换为新方法,我查看文档,因为他们不太清楚这里是我的旧代码我正在使用 Altorouter 并为数据库和刀片模板引擎照明 Vue 用于前端和 axious 用于 HTTP
我的路线
$router->map('POST', '/cart/payment', 'App\Controllers\CartController@Payment', 'Payment');
我的 cart.blade.php 文件
<button @click.prevent='checkout' class="btn btn-outline-success">Checkout - <i class="fa fa-credit-card"
aria-hidden="true"></i></button>
<span id="proporities" class="hide" data-customer-email='{{ user()->email
data-stripe-api='{{ \App\Classes\Session::get('stripe_key') }}'>
</span>
单击此按钮后,我将信息带到 cart.js 这里是 cart.js 代码
var stripe = StripeCheckout.configure({
key: $('#proporities').data('stripe-api'),
locale: 'auto',
token: function(token) {
var data = $.param({
stripeToken: token.id,
stripeEmail: token.email
});
axios.post('/cart/payment', data).then(function(response) {
$('.notifty').css('display', 'block').delay(2000).slideUp(300).html(response.data.success);
app.showcart(10);
}).catch(function(error) {
console.log(error)
});
}
});
var app = new Vue({
el: '#cart',
data: {
amount: 0
},
methods: {
checkout: function() {
stripe.open({
name: 'Cartzilla Store Inc.',
description: 'Your Shopping Cart',
zipCode: true,
amount: app.amount,
email: $('#proporities').data('customer-email')
});
}
在 cart.js 中将信息传递给我在顶部路由中定义的 PHP 控制器
这是控制器代码
public function payment()
{
$array = [];
if (Request::has('post')) {
$request = Request::get('post');
$email = $request->stripeEmail;
$token = $request->stripeToken;
try {
$customer = Customer::create([
'email' => $email,
'source' => $token
]);
$amount = convertMoney(Session::get('carttotal'));
$charge = Charge::create([
'customer' => $customer->id,
'amount' => $amount,
'description' => user()->fullname . 'Cart Purchase',
'currency' => 'usd'
]);
$orderId = strtoupper(uniqid());
foreach ($_SESSION['user_cart'] as $items) {
$productId = $items['product_id'];
$quantity = $items['quantity'];
$product = Product::where('id', $productId)->first();
if (!$product) {
continue;
}
$totalPrice = $product->price * $quantity;
$totalPrice = number_format($totalPrice, 2);
Order::create([
'user_id' => user()->id,
'product_id' => $productId,
'unit_price' => $product->price,
'status' => 'pending',
'total_price' => $amount,
'order_no' => $orderId,
'quantity' => $quantity
]);
$product->quantity = $product->quantity - $quantity;
$product->save();
array_push($array, [
'name' => $product->name,
'price' => $product->price,
'quantity' => $quantity,
'total' => $totalPrice,
]);
Payment::create([
'user_id' => user()->id,
'amount' => $charge->amount,
'status' => $charge->status,
'order_no' => $orderId
]);
}
Session::remove('user_cart');
echo json_encode(['success' => 'Thank You we Have Recevied Your Payment And Now Processing Your Order']);
} catch (\Throwable $th) {
//throw $th;
}
}
}
除此之外,我必须为订单表和付款表建模
这是模型
订单.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Order extends Model
{
use SoftDeletes;
public $timestamp = true;
public $fillable = ['user_id', 'product_id', 'unit_price', 'qunatity', 'total_price', 'status', 'order_no'];
public $date = ['deleted_at'];
}
和付款.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Payment extends Model
{
use SoftDeletes;
public $timestamp = true;
public $fillable = ['user_id', 'amount', 'status', 'order_no'];
public $date = ['deleted_at'];
}