diff --git a/plugins/Paypal/Controllers/PaypalController.php b/plugins/Paypal/Controllers/PaypalController.php index cf42b729..afe572ae 100644 --- a/plugins/Paypal/Controllers/PaypalController.php +++ b/plugins/Paypal/Controllers/PaypalController.php @@ -16,12 +16,12 @@ namespace Plugin\Paypal\Controllers; -use Beike\Repositories\OrderRepo; -use Beike\Services\StateMachineService; use Illuminate\Http\Request; +use Beike\Repositories\OrderRepo; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\DB; use Srmklive\PayPal\Services\PayPal; +use Beike\Services\StateMachineService; class PaypalController { @@ -56,11 +56,6 @@ class PaypalController $this->paypalClient->setAccessToken($token); } - public function test() - { - - } - /** * 创建 paypal 订单 @@ -74,15 +69,14 @@ class PaypalController $orderNumber = $data['orderNumber']; $customer = current_customer(); $order = OrderRepo::getOrderByNumber($orderNumber, $customer); - $orderTotalUsd = currency_format($order->total, 'USD', '', false); $paypalOrder = $this->paypalClient->createOrder([ "intent" => "CAPTURE", "purchase_units" => [ [ "amount" => [ - "currency_code" => 'USD', - "value" => round($orderTotalUsd, 2), + "currency_code" => $order->currency_code, + "value" => $order->total, ], 'description' => 'test' ] @@ -90,7 +84,6 @@ class PaypalController ]); return response()->json($paypalOrder); - //return redirect($paypalOrder['links'][1]['href'])->send(); } @@ -113,7 +106,7 @@ class PaypalController try { DB::beginTransaction(); if ($result['status'] === "COMPLETED") { - StateMachineService::getInstance($order)->changeStatus('paid'); + StateMachineService::getInstance($order)->changeStatus(StateMachineService::PAID); DB::commit(); } } catch (\Exception $e) { diff --git a/plugins/Paypal/Routes/shop.php b/plugins/Paypal/Routes/shop.php index 76005aa7..d40b5028 100644 --- a/plugins/Paypal/Routes/shop.php +++ b/plugins/Paypal/Routes/shop.php @@ -13,7 +13,6 @@ use Illuminate\Support\Facades\Route; use Plugin\Paypal\Controllers\PaypalController; Route::group(['prefix' => 'paypal'], function () { - Route::get('/test', [PaypalController::class, 'test']); Route::post('/create', [PaypalController::class, 'create']); Route::post('/capture', [PaypalController::class, 'capture']); }); diff --git a/plugins/Stripe/Controllers/StripeController.php b/plugins/Stripe/Controllers/StripeController.php index 8974435c..a156bf69 100644 --- a/plugins/Stripe/Controllers/StripeController.php +++ b/plugins/Stripe/Controllers/StripeController.php @@ -11,10 +11,10 @@ namespace Plugin\Stripe\Controllers; -use Beike\Repositories\OrderRepo; -use Beike\Shop\Http\Controllers\Controller; -use Beike\Shop\Services\PaymentService; use Illuminate\Http\Request; +use Beike\Repositories\OrderRepo; +use Beike\Services\StateMachineService; +use Beike\Shop\Http\Controllers\Controller; use Plugin\Stripe\Services\StripePaymentService; class StripeController extends Controller @@ -34,11 +34,10 @@ class StripeController extends Controller $creditCardData = $request->all(); $result = (new StripePaymentService($order))->capture($creditCardData); if ($result) { - $order->status = 'paid'; - $order->save(); - return json_success('支付成功'); + StateMachineService::getInstance($order)->changeStatus(StateMachineService::PAID); + return json_success(trans('Stripe::common.capture_success')); } else { - return json_success('支付失败'); + return json_success(trans('Stripe::common.capture_fail')); } } catch (\Exception $e) { return json_fail($e->getMessage()); diff --git a/plugins/Stripe/Lang/en/common.php b/plugins/Stripe/Lang/en/common.php index 79c62913..3954dd41 100644 --- a/plugins/Stripe/Lang/en/common.php +++ b/plugins/Stripe/Lang/en/common.php @@ -25,4 +25,7 @@ return [ 'error_cvv' => 'Please enter the security code', 'error_year' => 'Please select the year', 'error_month' => 'Please select a month', + + 'capture_success' => 'Capture Successfully', + 'capture_fail' => 'Capture Failed', ]; diff --git a/plugins/Stripe/Lang/zh_cn/common.php b/plugins/Stripe/Lang/zh_cn/common.php index b46f5edc..08737d22 100644 --- a/plugins/Stripe/Lang/zh_cn/common.php +++ b/plugins/Stripe/Lang/zh_cn/common.php @@ -25,4 +25,7 @@ return [ 'error_cvv' => '请输入安全码', 'error_year' => '请选择年', 'error_month' => '请选择月', + + 'capture_success' => '支付成功', + 'capture_fail' => '支付失败', ]; diff --git a/plugins/Stripe/Services/StripePaymentService.php b/plugins/Stripe/Services/StripePaymentService.php index 4f1b92f2..4691588a 100644 --- a/plugins/Stripe/Services/StripePaymentService.php +++ b/plugins/Stripe/Services/StripePaymentService.php @@ -18,6 +18,12 @@ use Stripe\Exception\ApiErrorException; class StripePaymentService extends PaymentService { + // 零位十进制货币 https://stripe.com/docs/currencies#special-cases + const ZERO_DECIMAL = [ + 'BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', + 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF', + ]; + /** * @throws ApiErrorException */ @@ -34,24 +40,40 @@ class StripePaymentService extends PaymentService ], ]); - // $customer = Customer::create([ - // 'email' => $this->order->email, - // ]); - // $customerId = $customer['id']; - $tokenId = $token['id']; - $total = round($this->order->total, 2) * 100; + $currency = $this->order->currency_code; + + if (!in_array($currency, self::ZERO_DECIMAL)) { + $total = round($this->order->total, 2) * 100; + } else { + $total = floor($this->order->total); + } + $stripeChargeParameters = array( 'amount' => $total, - 'currency' => $this->order->currency_code, + 'currency' => $currency, 'metadata' => array( 'orderId' => $this->order->id, ), 'source' => $tokenId, - // 'customer' => $customerId, + // 'customer' => $this->createCustomer(), ); $charge = \Stripe\Charge::create($stripeChargeParameters); return $charge['paid'] && $charge['captured']; } + + + /** + * 创建 stripe customer + * @return mixed + * @throws ApiErrorException + */ + private function createCustomer(): mixed + { + $customer = \Stripe\Customer::create([ + 'email' => $this->order->email, + ]); + return $customer['id']; + } } diff --git a/resources/lang/en/shop/carts.php b/resources/lang/en/shop/carts.php index c348d737..e7a17c02 100644 --- a/resources/lang/en/shop/carts.php +++ b/resources/lang/en/shop/carts.php @@ -21,14 +21,14 @@ return [ 'shipping_fee' => 'Shipping Fee', 'all' => 'All', 'selected' => 'Selected', - 'to_checkout' => 'To Checkout', + 'to_checkout' => 'Checkout', 'cart_empty' => 'Your shopping cart is empty', 'go_buy' => 'You can go and see what you want to buy', 'go_shopping' => 'Go Shopping', 'must_select' => 'Please select at least one product', 'mini' => 'Your cart', 'delete' => 'Delete', - 'check_cart' => 'Check the shopping cart', + 'check_cart' => 'Shopping Cart', 'invalid_customer' => 'Invalid customer.', 'empty_selected_products' => 'Empty selected products.',