This commit is contained in:
Edward Yang 2022-07-07 17:39:13 +08:00
parent c67a46362e
commit bbc6302359
6 changed files with 93 additions and 80 deletions

View File

@ -81,4 +81,32 @@ 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

@ -54,6 +54,7 @@ Route::prefix('/')
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

@ -13,7 +13,10 @@ namespace Beike\Shop\Services;
use Beike\Models\Order;
use Beike\Repositories\OrderRepo;
use Cartalyst\Stripe\Stripe;
use Stripe\Customer;
use Stripe\Exception\ApiErrorException;
use Stripe\Stripe;
use Stripe\Token;
class PaymentService
{
@ -43,24 +46,67 @@ class PaymentService
{
if ($this->paymentMethodCode == 'bk_stripe') {
$apiKey = setting('bk_stripe.secret_key');
$stripe = Stripe::make($apiKey, '2020-08-27');
Stripe::setApiKey($apiKey);
// Stripe::setApiVersion('2020-08-27');
/**
$customer = $stripe->customers()->create([
'email' => $this->order->email,
]);
$customers = $stripe->customers()->all();
$charge = $stripe->charges()->create([
'customer' => $customer['id'],
'currency' => 'USD',
'amount' => 50.49,
]);
* $customer = $stripe->customers()->create([
* 'email' => $this->order->email,
* ]);
*
* $customers = $stripe->customers()->all();
*
* $charge = $stripe->charges()->create([
* 'customer' => $customer['id'],
* 'currency' => 'USD',
* 'amount' => 50.49,
* ]);
**/
return view("checkout.payment.{$this->paymentMethodCode}", ['order' => $this->order]);
// echo $charge['id'];
} else {
return view('没有支付方式');
}
}
/**
* @throws ApiErrorException
*/
public function capture($creditCardData): bool
{
if ($this->order->status != 'unpaid') {
throw new \Exception('订单已支付');
}
$apiKey = setting('bk_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' => 'USD',
'metadata' => array(
'orderId' => $this->order->id,
),
'source' => $tokenId,
// 'customer' => $customerId,
);
$charge = \Stripe\Charge::create($stripeChargeParameters);
return $charge['paid'] && $charge['captured'];
}
}

View File

@ -7,7 +7,6 @@
"require": {
"php": "^7.3|^8.0",
"ext-json": "*",
"cartalyst/stripe": "^2.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.65",

65
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "3e50a922b00db84582eaa827333fe7da",
"content-hash": "c52e9e1b5b722611e7a5c8632e2611df",
"packages": [
{
"name": "asm89/stack-cors",
@ -134,69 +134,6 @@
],
"time": "2021-08-15T20:50:18+00:00"
},
{
"name": "cartalyst/stripe",
"version": "v2.4.6",
"source": {
"type": "git",
"url": "https://github.com/cartalyst/stripe.git",
"reference": "e75b4d714fec5f034d533e55f5365f0229b5686d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/cartalyst/stripe/zipball/e75b4d714fec5f034d533e55f5365f0229b5686d",
"reference": "e75b4d714fec5f034d533e55f5365f0229b5686d",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"guzzlehttp/guzzle": "~6.0|~7.0",
"php": ">=5.5.9"
},
"require-dev": {
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^9.0"
},
"type": "library",
"extra": {
"component": "package",
"branch-alias": {
"dev-master": "2.4.x-dev"
}
},
"autoload": {
"psr-4": {
"Cartalyst\\Stripe\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Cartalyst LLC",
"email": "help@cartalyst.com",
"homepage": "https://cartalyst.com"
}
],
"description": "A comprehensive Stripe API package.",
"keywords": [
"cartalyst",
"php",
"stripe"
],
"support": {
"issues": "https://github.com/cartalyst/stripe/issues",
"source": "https://github.com/cartalyst/stripe/tree/v2.4.6"
},
"time": "2021-10-09T10:40:49+00:00"
},
{
"name": "dflydev/dot-access-data",
"version": "v3.0.1",

View File

@ -123,7 +123,9 @@
return;
}
$http.post(`orders/${this.source.order.number}/pay`, this.form).then((res) => {
console.log(this.form);
$http.post(`/orders/${this.source.order.number}/pay`, this.form).then((res) => {
console.log(res)
})
});
@ -131,4 +133,4 @@
}
})
</script>
@endpush
@endpush