fixed payment

This commit is contained in:
Edward Yang 2022-08-08 16:14:13 +08:00
parent 5c4f167915
commit 96b7481123
7 changed files with 125 additions and 74 deletions

View File

@ -81,32 +81,4 @@ class OrderController extends Controller
$order = OrderRepo::getOrderByNumber($number, $customer);
return (new PaymentService($order))->pay();
}
/**
* 订单支付页面
*
* @param Request $request
* @param $number
* @return array
* @throws \Exception
*/
public function capture(Request $request, $number): array
{
try {
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($number, $customer);
$creditCardData = $request->all();
$result = (new PaymentService($order))->capture($creditCardData);
if ($result) {
$order->status = 'paid';
$order->save();
return json_success('支付成功');
} else {
return json_success('支付失败');
}
} catch (\Exception $e) {
return json_fail($e->getMessage());
}
}
}

View File

@ -69,7 +69,6 @@ Route::prefix('/')
Route::post('checkout/confirm', [CheckoutController::class, 'confirm'])->name('checkout.confirm');
Route::get('orders/{number}/success', [OrderController::class, 'success'])->name('orders.success');
Route::get('orders/{number}/pay', [OrderController::class, 'pay'])->name('orders.pay');
Route::post('orders/{number}/pay', [OrderController::class, 'capture'])->name('orders.capture');
});
Route::prefix('account')

View File

@ -22,17 +22,15 @@ use Stripe\Token;
class PaymentService
{
private $order;
private $orderId;
private $paymentMethodCode;
protected $order;
protected $orderId;
protected $paymentMethodCode;
public function __construct($order)
{
$customer = current_customer();
if (is_numeric($order)) {
$this->order = OrderRepo::getOrderByIdOrNumber($this->orderId, $customer);
$this->order = OrderRepo::getOrderByIdOrNumber($order, $customer);
} elseif ($order instanceof Order) {
$this->order = $order;
}
@ -61,42 +59,4 @@ class PaymentService
$paymentView = view($viewPath, ['order' => $this->order])->render();
return view('checkout.payment', ['order' => $this->order, 'payment' => $paymentView]);
}
/**
* @throws ApiErrorException
*/
public function capture($creditCardData): bool
{
$apiKey = plugin_setting('stripe.secret_key');
Stripe::setApiKey($apiKey);
$token = Token::create([
'card' => [
'number' => $creditCardData['cardnum'],
'exp_year' => $creditCardData['year'],
'exp_month' => $creditCardData['month'],
'cvc' => $creditCardData['cvv'],
],
]);
// $customer = Customer::create([
// 'email' => $this->order->email,
// ]);
// $customerId = $customer['id'];
$tokenId = $token['id'];
$total = round($this->order->total, 2) * 100;
$stripeChargeParameters = array(
'amount' => $total,
'currency' => $this->order->currency_code,
'metadata' => array(
'orderId' => $this->order->id,
),
'source' => $tokenId,
// 'customer' => $customerId,
);
$charge = \Stripe\Charge::create($stripeChargeParameters);
return $charge['paid'] && $charge['captured'];
}
}

View File

@ -0,0 +1,47 @@
<?php
/**
* StripeController.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-08 15:58:36
* @modified 2022-08-08 15:58:36
*/
namespace Plugin\Stripe\Controllers;
use Beike\Repositories\OrderRepo;
use Beike\Shop\Http\Controllers\Controller;
use Beike\Shop\Services\PaymentService;
use Illuminate\Http\Request;
use Plugin\Stripe\Services\StripePaymentService;
class StripeController extends Controller
{
/**
* 订单支付扣款
*
* @param Request $request
* @return array
*/
public function capture(Request $request): array
{
try {
$number = request('order_number');
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($number, $customer);
$creditCardData = $request->all();
$result = (new StripePaymentService($order))->capture($creditCardData);
if ($result) {
$order->status = 'paid';
$order->save();
return json_success('支付成功');
} else {
return json_success('支付失败');
}
} catch (\Exception $e) {
return json_fail($e->getMessage());
}
}
}

View File

@ -0,0 +1,15 @@
<?php
/**
* shop.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-04 16:17:44
* @modified 2022-08-04 16:17:44
*/
use Illuminate\Support\Facades\Route;
use Plugin\Stripe\Controllers\StripeController;
Route::post('/stripe/capture', [StripeController::class, 'capture'])->name('plugin.stripe_capture');

View File

@ -0,0 +1,57 @@
<?php
/**
* StripePaymentService.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-08 16:09:21
* @modified 2022-08-08 16:09:21
*/
namespace Plugin\Stripe\Services;
use Stripe\Token;
use Stripe\Stripe;
use Beike\Shop\Services\PaymentService;
use Stripe\Exception\ApiErrorException;
class StripePaymentService extends PaymentService
{
/**
* @throws ApiErrorException
*/
public function capture($creditCardData): bool
{
$apiKey = plugin_setting('stripe.secret_key');
Stripe::setApiKey($apiKey);
$token = Token::create([
'card' => [
'number' => $creditCardData['cardnum'],
'exp_year' => $creditCardData['year'],
'exp_month' => $creditCardData['month'],
'cvc' => $creditCardData['cvv'],
],
]);
// $customer = Customer::create([
// 'email' => $this->order->email,
// ]);
// $customerId = $customer['id'];
$tokenId = $token['id'];
$total = round($this->order->total, 2) * 100;
$stripeChargeParameters = array(
'amount' => $total,
'currency' => $this->order->currency_code,
'metadata' => array(
'orderId' => $this->order->id,
),
'source' => $tokenId,
// 'customer' => $customerId,
);
$charge = \Stripe\Charge::create($stripeChargeParameters);
return $charge['paid'] && $charge['captured'];
}
}

View File

@ -50,6 +50,7 @@
data: {
form: {
order_number: @json($order->number ?? null),
cardnum: '',
year: '',
month: '',
@ -99,7 +100,7 @@
console.log(this.form);
$http.post(`/orders/${this.source.order.number}/pay`, this.form).then((res) => {
$http.post(`/plugin/stripe/capture`, this.form).then((res) => {
console.log(res)
})
});