From bbc630235963b69e520ec5ff75f6c3f5c563a202 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Thu, 7 Jul 2022 17:39:13 +0800 Subject: [PATCH] stripe --- .../Controllers/Account/OrderController.php | 28 ++++++++ beike/Shop/Routes/shop.php | 1 + beike/Shop/Services/PaymentService.php | 72 +++++++++++++++---- composer.json | 1 - composer.lock | 65 +---------------- .../checkout/payment/bk_stripe.blade.php | 6 +- 6 files changed, 93 insertions(+), 80 deletions(-) diff --git a/beike/Shop/Http/Controllers/Account/OrderController.php b/beike/Shop/Http/Controllers/Account/OrderController.php index 8e40dce5..5fb9d4ef 100644 --- a/beike/Shop/Http/Controllers/Account/OrderController.php +++ b/beike/Shop/Http/Controllers/Account/OrderController.php @@ -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()); + } + } } diff --git a/beike/Shop/Routes/shop.php b/beike/Shop/Routes/shop.php index 26433645..9331585a 100644 --- a/beike/Shop/Routes/shop.php +++ b/beike/Shop/Routes/shop.php @@ -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') diff --git a/beike/Shop/Services/PaymentService.php b/beike/Shop/Services/PaymentService.php index e74e253b..af191ec8 100644 --- a/beike/Shop/Services/PaymentService.php +++ b/beike/Shop/Services/PaymentService.php @@ -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']; + } } diff --git a/composer.json b/composer.json index 93dad912..3b5cce40 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 9a87d6e0..9f4726ff 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/themes/default/checkout/payment/bk_stripe.blade.php b/themes/default/checkout/payment/bk_stripe.blade.php index 75dd6453..790ecc38 100644 --- a/themes/default/checkout/payment/bk_stripe.blade.php +++ b/themes/default/checkout/payment/bk_stripe.blade.php @@ -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 @@ } }) -@endpush \ No newline at end of file +@endpush