完善stripe

This commit is contained in:
Edward Yang 2022-11-22 11:07:34 +08:00
parent 3ecff0b803
commit eb16ddf105
7 changed files with 49 additions and 30 deletions

View File

@ -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) {

View File

@ -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']);
});

View File

@ -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());

View File

@ -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',
];

View File

@ -25,4 +25,7 @@ return [
'error_cvv' => '请输入安全码',
'error_year' => '请选择年',
'error_month' => '请选择月',
'capture_success' => '支付成功',
'capture_fail' => '支付失败',
];

View File

@ -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'];
}
}

View File

@ -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.',