plugin code format

This commit is contained in:
Edward Yang 2023-02-15 09:44:47 +08:00
parent d381396062
commit d0c7b43cce
26 changed files with 272 additions and 267 deletions

View File

@ -86,6 +86,7 @@ class OrderController extends Controller
$customer = current_customer(); $customer = current_customer();
$order = OrderRepo::getOrderByNumber($number, $customer); $order = OrderRepo::getOrderByNumber($number, $customer);
hook_action('account.order.pay.before', ['order' => $order]); hook_action('account.order.pay.before', ['order' => $order]);
return (new PaymentService($order))->pay(); return (new PaymentService($order))->pay();
} catch (\Exception $e) { } catch (\Exception $e) {
return redirect(shop_route('account.order.show', $number))->withErrors($e->getMessage()); return redirect(shop_route('account.order.show', $number))->withErrors($e->getMessage());

View File

@ -48,7 +48,6 @@
}, },
"exclude": [ "exclude": [
"database", "database",
"plugins",
"beike/Hook" "beike/Hook"
], ],
"notName": [ "notName": [

View File

@ -37,10 +37,10 @@ class Bootstrap
'icon' => $pluginResource['icon'], 'icon' => $pluginResource['icon'],
'cost' => $this->getShippingFee($checkout), 'cost' => $this->getShippingFee($checkout),
]; ];
return $quotes; return $quotes;
} }
/** /**
* 计算固定运费 * 计算固定运费
* *
@ -57,8 +57,9 @@ class Bootstrap
return $shippingValue; return $shippingValue;
} elseif ($shippingType == 'percent') { } elseif ($shippingType == 'percent') {
return $amount * $shippingValue / 100; return $amount * $shippingValue / 100;
} else {
return 0;
} }
return 0;
} }
} }

View File

@ -16,7 +16,7 @@ return [
'type' => 'select', 'type' => 'select',
'options' => [ 'options' => [
['value' => 'fixed', 'label_key' => 'common.flat_shipping'], ['value' => 'fixed', 'label_key' => 'common.flat_shipping'],
['value' => 'percent', 'label_key' => 'common.percentage'] ['value' => 'percent', 'label_key' => 'common.percentage'],
], ],
'required' => true, 'required' => true,
], ],
@ -25,5 +25,5 @@ return [
'label_key' => 'common.shipping_value', 'label_key' => 'common.shipping_value',
'type' => 'string', 'type' => 'string',
'required' => true, 'required' => true,
] ],
]; ];

View File

@ -27,7 +27,6 @@ class Bootstrap
// $this->modifySetting(); // $this->modifySetting();
} }
/** /**
* 在前台网页头部添加二级菜单链接 * 在前台网页头部添加二级菜单链接
*/ */
@ -36,13 +35,13 @@ class Bootstrap
add_hook_filter('menu.content', function ($data) { add_hook_filter('menu.content', function ($data) {
$data[] = [ $data[] = [
'name' => trans('LatestProducts::header.latest_products'), 'name' => trans('LatestProducts::header.latest_products'),
"link" => shop_route('latest_products'), 'link' => shop_route('latest_products'),
]; ];
return $data; return $data;
}, 0); }, 0);
} }
/** /**
* 修改前台全局 header 模板演示 * 修改前台全局 header 模板演示
*/ */
@ -66,11 +65,11 @@ class Bootstrap
add_hook_blade('header.menu.icon', function ($callback, $output, $data) { add_hook_blade('header.menu.icon', function ($callback, $output, $data) {
$view = view('LatestProducts::shop.header_icon')->render(); $view = view('LatestProducts::shop.header_icon')->render();
return $output . $view; return $output . $view;
}); });
} }
/** /**
* 修改产品详情页演示 * 修改产品详情页演示
* 1. 通过数据 hook 修改产品详情页产品名称 * 1. 通过数据 hook 修改产品详情页产品名称
@ -83,12 +82,14 @@ class Bootstrap
// 通过数据 hook 修改产品详情页产品名称 // 通过数据 hook 修改产品详情页产品名称
add_hook_filter('product.show.data', function ($product) { add_hook_filter('product.show.data', function ($product) {
$product['product']['name'] = '[疯狂热销]' . $product['product']['name']; $product['product']['name'] = '[疯狂热销]' . $product['product']['name'];
return $product; return $product;
}); });
// 通过模板 hook 在产品详情页名称上面添加 Hot 标签 // 通过模板 hook 在产品详情页名称上面添加 Hot 标签
add_hook_blade('product.detail.name', function ($callback, $output, $data) { add_hook_blade('product.detail.name', function ($callback, $output, $data) {
$badge = '<span class="badge" style="background-color: #FF4D00; color: #ffffff; border-color: #FF4D00">Hot</span>'; $badge = '<span class="badge" style="background-color: #FF4D00; color: #ffffff; border-color: #FF4D00">Hot</span>';
return $badge . $output; return $badge . $output;
}); });
@ -100,11 +101,11 @@ class Bootstrap
// 通过模板 hook 在产品详情页立即购买后添加按钮 // 通过模板 hook 在产品详情页立即购买后添加按钮
add_hook_blade('product.detail.buy.after', function ($callback, $output, $data) { add_hook_blade('product.detail.buy.after', function ($callback, $output, $data) {
$view = '<button class="btn btn-dark ms-3 fw-bold"><i class="bi bi-bag-fill me-1"></i>新增按钮</button>'; $view = '<button class="btn btn-dark ms-3 fw-bold"><i class="bi bi-bag-fill me-1"></i>新增按钮</button>';
return $output . $view; return $output . $view;
}); });
} }
/** /**
* 后台产品编辑页添加自定义字段演示 * 后台产品编辑页添加自定义字段演示
*/ */
@ -112,11 +113,11 @@ class Bootstrap
{ {
add_hook_blade('admin.product.edit.extra', function ($callback, $output, $data) { add_hook_blade('admin.product.edit.extra', function ($callback, $output, $data) {
$view = view('LatestProducts::admin.product.edit_extra_field', $data)->render(); $view = view('LatestProducts::admin.product.edit_extra_field', $data)->render();
return $output . $view; return $output . $view;
}, 1); }, 1);
} }
/** /**
* 系统设置添加新 tab * 系统设置添加新 tab
*/ */
@ -130,5 +131,4 @@ class Bootstrap
return view('LatestProducts::admin.setting.tab')->render(); return view('LatestProducts::admin.setting.tab')->render();
}); });
} }
} }

View File

@ -21,11 +21,10 @@ class MenusController extends Controller
{ {
return [ return [
'method' => __METHOD__, 'method' => __METHOD__,
'route_list' => [] 'route_list' => [],
]; ];
} }
public function latestProducts() public function latestProducts()
{ {
$products = ProductRepo::getBuilder(['active' => 1]) $products = ProductRepo::getBuilder(['active' => 1])
@ -37,6 +36,7 @@ class MenusController extends Controller
'products' => $products, 'products' => $products,
'items' => ProductSimple::collection($products)->jsonSerialize(), 'items' => ProductSimple::collection($products)->jsonSerialize(),
]; ];
return view('LatestProducts::shop.latest_products', $data); return view('LatestProducts::shop.latest_products', $data);
} }
} }

View File

@ -11,17 +11,16 @@
* https://www.zongscan.com/demo333/1311.html * https://www.zongscan.com/demo333/1311.html
* https://clickysoft.com/how-to-integrate-paypal-payment-gateway-in-laravel/ * https://clickysoft.com/how-to-integrate-paypal-payment-gateway-in-laravel/
* https://www.positronx.io/how-to-integrate-paypal-payment-gateway-in-laravel/ * https://www.positronx.io/how-to-integrate-paypal-payment-gateway-in-laravel/
*
*/ */
namespace Plugin\Paypal\Controllers; namespace Plugin\Paypal\Controllers;
use Illuminate\Http\Request;
use Beike\Repositories\OrderRepo; use Beike\Repositories\OrderRepo;
use Beike\Services\StateMachineService;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Srmklive\PayPal\Services\PayPal; use Srmklive\PayPal\Services\PayPal;
use Beike\Services\StateMachineService;
class PaypalController class PaypalController
{ {
@ -52,7 +51,6 @@ class PaypalController
$this->paypalClient->setAccessToken($token); $this->paypalClient->setAccessToken($token);
} }
/** /**
* 创建 paypal 订单 * 创建 paypal 订单
* @param Request $request * @param Request $request
@ -69,22 +67,21 @@ class PaypalController
$total = round($order->total, 2); $total = round($order->total, 2);
$paypalOrder = $this->paypalClient->createOrder([ $paypalOrder = $this->paypalClient->createOrder([
"intent" => "CAPTURE", 'intent' => 'CAPTURE',
"purchase_units" => [ 'purchase_units' => [
[ [
"amount" => [ 'amount' => [
"currency_code" => $order->currency_code, 'currency_code' => $order->currency_code,
"value" => $total, 'value' => $total,
],
'description' => 'test',
], ],
'description' => 'test'
]
], ],
]); ]);
return response()->json($paypalOrder); return response()->json($paypalOrder);
} }
/** /**
* 客户同意后扣款回调 * 客户同意后扣款回调
* @param Request $request * @param Request $request
@ -104,7 +101,7 @@ class PaypalController
try { try {
DB::beginTransaction(); DB::beginTransaction();
if ($result['status'] === "COMPLETED") { if ($result['status'] === 'COMPLETED') {
StateMachineService::getInstance($order)->changeStatus(StateMachineService::PAID); StateMachineService::getInstance($order)->changeStatus(StateMachineService::PAID);
DB::commit(); DB::commit();
} }
@ -112,6 +109,7 @@ class PaypalController
DB::rollBack(); DB::rollBack();
dd($e); dd($e);
} }
return response()->json($result); return response()->json($result);
} }
} }

View File

@ -48,9 +48,9 @@ return [
'type' => 'select', 'type' => 'select',
'options' => [ 'options' => [
['value' => '1', 'label_key' => 'setting.enabled'], ['value' => '1', 'label_key' => 'setting.enabled'],
['value' => '0', 'label' => '关闭'] ['value' => '0', 'label' => '关闭'],
], ],
'required' => true, 'required' => true,
'description' => '', 'description' => '',
] ],
]; ];

View File

@ -11,7 +11,6 @@
namespace Plugin\Social; namespace Plugin\Social;
class Bootstrap class Bootstrap
{ {
public function boot() public function boot()
@ -33,6 +32,7 @@ class Bootstrap
foreach ($providers as $provider) { foreach ($providers as $provider) {
$buttons[] = view('Social::shop/social_button', ['provider' => $provider])->render(); $buttons[] = view('Social::shop/social_button', ['provider' => $provider])->render();
} }
return $buttons; return $buttons;
}); });
} }

View File

@ -11,9 +11,9 @@
namespace Plugin\Social\Controllers; namespace Plugin\Social\Controllers;
use Illuminate\Http\Request;
use Beike\Repositories\SettingRepo;
use Beike\Admin\Http\Controllers\Controller; use Beike\Admin\Http\Controllers\Controller;
use Beike\Repositories\SettingRepo;
use Illuminate\Http\Request;
class AdminSocialController extends Controller class AdminSocialController extends Controller
{ {
@ -23,6 +23,7 @@ class AdminSocialController extends Controller
public function saveSetting(Request $request): array public function saveSetting(Request $request): array
{ {
SettingRepo::storeValue('setting', $request->all(), 'social', 'plugin'); SettingRepo::storeValue('setting', $request->all(), 'social', 'plugin');
return json_success('保存成功'); return json_success('保存成功');
} }
} }

View File

@ -11,11 +11,11 @@
namespace Plugin\Social\Controllers; namespace Plugin\Social\Controllers;
use Beike\Admin\Http\Controllers\Controller;
use Beike\Models\Customer; use Beike\Models\Customer;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
use Laravel\Socialite\Facades\Socialite; use Laravel\Socialite\Facades\Socialite;
use Beike\Admin\Http\Controllers\Controller;
use Plugin\Social\Repositories\CustomerRepo; use Plugin\Social\Repositories\CustomerRepo;
class ShopSocialController extends Controller class ShopSocialController extends Controller
@ -46,7 +46,7 @@ class ShopSocialController extends Controller
try { try {
return Socialite::driver($provider)->redirect(); return Socialite::driver($provider)->redirect();
} catch (\Exception $e) { } catch (\Exception $e) {
die($e->getMessage()); exit($e->getMessage());
} }
} }
@ -60,9 +60,10 @@ class ShopSocialController extends Controller
$userData = Socialite::driver($provider)->user(); $userData = Socialite::driver($provider)->user();
$customer = CustomerRepo::createCustomer($provider, $userData); $customer = CustomerRepo::createCustomer($provider, $userData);
Auth::guard(Customer::AUTH_GUARD)->login($customer); Auth::guard(Customer::AUTH_GUARD)->login($customer);
return view('Social::shop/callback'); return view('Social::shop/callback');
} catch (\Exception $e) { } catch (\Exception $e) {
die($e->getMessage()); exit($e->getMessage());
} }
} }
} }

View File

@ -10,7 +10,7 @@
*/ */
return [ return [
// Text // Text
'text_module' => 'Modules', 'text_module' => 'Modules',
'text_success' => 'Success: You have modified module OpenCart OmniAuth!', 'text_success' => 'Success: You have modified module OpenCart OmniAuth!',
'text_copyright' => 'OpenCart.cn <a href="http://www.opencart.cn" target="_blank">OmniAuth</a> &copy; %s', 'text_copyright' => 'OpenCart.cn <a href="http://www.opencart.cn" target="_blank">OmniAuth</a> &copy; %s',
@ -27,7 +27,7 @@ return [
'text_twitter_title' => 'Twitter login application address', 'text_twitter_title' => 'Twitter login application address',
'text_help_msg' => 'help information', 'text_help_msg' => 'help information',
// Entry // Entry
'entry_status' => 'Status', 'entry_status' => 'Status',
'entry_bind' => 'Force Bind', 'entry_bind' => 'Force Bind',
'entry_debug' => 'Debug Mode', 'entry_debug' => 'Debug Mode',
@ -39,13 +39,13 @@ return [
'entry_callback' => 'Callback URL', 'entry_callback' => 'Callback URL',
'entry_sort_order' => 'Sort Order', 'entry_sort_order' => 'Sort Order',
// Button // Button
'button_add_row' => 'Add Provider', 'button_add_row' => 'Add Provider',
// Error // Error
'error_permission' => 'Warning: You do not have permission to modify module OpenCart OmniAuth!', 'error_permission' => 'Warning: You do not have permission to modify module OpenCart OmniAuth!',
// Providers // Providers
'wechat' => 'WeChat', 'wechat' => 'WeChat',
'wechatofficial' => 'WeChatOfficial', 'wechatofficial' => 'WeChatOfficial',
'qq' => 'QQ', 'qq' => 'QQ',

View File

@ -10,7 +10,7 @@
*/ */
return [ return [
// Text // Text
'text_module' => '模块', 'text_module' => '模块',
'text_success' => '成功: 您成功修改第三方登录配置!', 'text_success' => '成功: 您成功修改第三方登录配置!',
'text_copyright' => 'OpenCart.cn <a href="http://www.opencart.cn" target="_blank">获取帮助</a> &copy; %s', 'text_copyright' => 'OpenCart.cn <a href="http://www.opencart.cn" target="_blank">获取帮助</a> &copy; %s',
@ -27,7 +27,7 @@ return [
'text_twitter_title' => 'Twitter登录申请地址', 'text_twitter_title' => 'Twitter登录申请地址',
'text_help_msg' => '帮助信息', 'text_help_msg' => '帮助信息',
// Entry // Entry
'entry_status' => '状态', 'entry_status' => '状态',
'entry_bind' => '强制绑定', 'entry_bind' => '强制绑定',
'entry_debug' => '调试模式', 'entry_debug' => '调试模式',
@ -39,9 +39,9 @@ return [
'entry_callback' => '回调地址', 'entry_callback' => '回调地址',
'entry_sort_order' => '排序', 'entry_sort_order' => '排序',
// Button // Button
'button_add_row' => '添加类型', 'button_add_row' => '添加类型',
// Error // Error
'error_permission' => '警告: 您没有权限修改此配置!', 'error_permission' => '警告: 您没有权限修改此配置!',
]; ];

View File

@ -20,7 +20,7 @@ class CustomerSocial extends Model
public $table = 'customer_socials'; public $table = 'customer_socials';
public $fillable = [ public $fillable = [
'customer_id', 'provider', 'user_id', 'union_id', 'access_token', 'extra' 'customer_id', 'provider', 'user_id', 'union_id', 'access_token', 'extra',
]; ];
public function customer(): BelongsTo public function customer(): BelongsTo

View File

@ -12,12 +12,12 @@
namespace Plugin\Social\Repositories; namespace Plugin\Social\Repositories;
use Beike\Models\Customer; use Beike\Models\Customer;
use Beike\Shop\Services\AccountService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Laravel\Socialite\Two\User; use Laravel\Socialite\Two\User;
use Beike\Shop\Services\AccountService;
use Illuminate\Database\Eloquent\Model;
use Plugin\Social\Models\CustomerSocial; use Plugin\Social\Models\CustomerSocial;
use Illuminate\Database\Eloquent\Builder;
class CustomerRepo class CustomerRepo
{ {
@ -36,9 +36,10 @@ class CustomerRepo
foreach (self::PROVIDERS as $provider) { foreach (self::PROVIDERS as $provider) {
$items[] = [ $items[] = [
'code' => $provider, 'code' => $provider,
'label' => trans("Social::providers.{$provider}") 'label' => trans("Social::providers.{$provider}"),
]; ];
} }
return $items; return $items;
} }
@ -73,10 +74,10 @@ class CustomerRepo
} }
self::createSocial($customer, $provider, $userData); self::createSocial($customer, $provider, $userData);
return $customer; return $customer;
} }
/** /**
* @param $customer * @param $customer
* @param $provider * @param $provider
@ -96,12 +97,12 @@ class CustomerRepo
'user_id' => $userData->getId(), 'user_id' => $userData->getId(),
'union_id' => '', 'union_id' => '',
'access_token' => $userData->token, 'access_token' => $userData->token,
'extra' => json_encode($userData->getRaw()) 'extra' => json_encode($userData->getRaw()),
]; ];
return CustomerSocial::query()->create($socialData); return CustomerSocial::query()->create($socialData);
} }
/** /**
* 通过 provider user_id 获取已存在 social * 通过 provider user_id 获取已存在 social
* @param $provider * @param $provider

View File

@ -11,10 +11,10 @@
namespace Plugin\Stripe\Controllers; namespace Plugin\Stripe\Controllers;
use Illuminate\Http\Request;
use Beike\Repositories\OrderRepo; use Beike\Repositories\OrderRepo;
use Beike\Services\StateMachineService; use Beike\Services\StateMachineService;
use Beike\Shop\Http\Controllers\Controller; use Beike\Shop\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Plugin\Stripe\Services\StripePaymentService; use Plugin\Stripe\Services\StripePaymentService;
class StripeController extends Controller class StripeController extends Controller
@ -35,10 +35,12 @@ class StripeController extends Controller
$result = (new StripePaymentService($order))->capture($creditCardData); $result = (new StripePaymentService($order))->capture($creditCardData);
if ($result) { if ($result) {
StateMachineService::getInstance($order)->changeStatus(StateMachineService::PAID); StateMachineService::getInstance($order)->changeStatus(StateMachineService::PAID);
return json_success(trans('Stripe::common.capture_success')); return json_success(trans('Stripe::common.capture_success'));
} else {
return json_success(trans('Stripe::common.capture_fail'));
} }
return json_success(trans('Stripe::common.capture_fail'));
} catch (\Exception $e) { } catch (\Exception $e) {
return json_fail($e->getMessage()); return json_fail($e->getMessage());
} }

View File

@ -11,15 +11,15 @@
namespace Plugin\Stripe\Services; namespace Plugin\Stripe\Services;
use Stripe\Token;
use Stripe\Stripe;
use Beike\Shop\Services\PaymentService; use Beike\Shop\Services\PaymentService;
use Stripe\Exception\ApiErrorException; use Stripe\Exception\ApiErrorException;
use Stripe\Stripe;
use Stripe\Token;
class StripePaymentService extends PaymentService class StripePaymentService extends PaymentService
{ {
// 零位十进制货币 https://stripe.com/docs/currencies#special-cases // 零位十进制货币 https://stripe.com/docs/currencies#special-cases
const ZERO_DECIMAL = [ public const ZERO_DECIMAL = [
'BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA',
'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF',
]; ];
@ -43,27 +43,27 @@ class StripePaymentService extends PaymentService
$tokenId = $token['id']; $tokenId = $token['id'];
$currency = $this->order->currency_code; $currency = $this->order->currency_code;
if (!in_array($currency, self::ZERO_DECIMAL)) { if (! in_array($currency, self::ZERO_DECIMAL)) {
$total = round($this->order->total, 2) * 100; $total = round($this->order->total, 2) * 100;
} else { } else {
$total = floor($this->order->total); $total = floor($this->order->total);
} }
$stripeChargeParameters = array( $stripeChargeParameters = [
'amount' => $total, 'amount' => $total,
'currency' => $currency, 'currency' => $currency,
'metadata' => array( 'metadata' => [
'orderId' => $this->order->id, 'orderId' => $this->order->id,
), ],
'source' => $tokenId, 'source' => $tokenId,
// 'customer' => $this->createCustomer(), // 'customer' => $this->createCustomer(),
); ];
$charge = \Stripe\Charge::create($stripeChargeParameters); $charge = \Stripe\Charge::create($stripeChargeParameters);
return $charge['paid'] && $charge['captured']; return $charge['paid'] && $charge['captured'];
} }
/** /**
* 创建 stripe customer * 创建 stripe customer
* @return mixed * @return mixed
@ -74,6 +74,7 @@ class StripePaymentService extends PaymentService
$customer = \Stripe\Customer::create([ $customer = \Stripe\Customer::create([
'email' => $this->order->email, 'email' => $this->order->email,
]); ]);
return $customer['id']; return $customer['id'];
} }
} }

View File

@ -32,9 +32,9 @@ return [
'type' => 'select', 'type' => 'select',
'options' => [ 'options' => [
['value' => '1', 'label' => '开启'], ['value' => '1', 'label' => '开启'],
['value' => '0', 'label' => '关闭'] ['value' => '0', 'label' => '关闭'],
], ],
'required' => true, 'required' => true,
'description' => '如开启测试模式请填写测试公钥和密钥, 关闭测试模式则填写正式公钥和密钥', 'description' => '如开启测试模式请填写测试公钥和密钥, 关闭测试模式则填写正式公钥和密钥',
] ],
]; ];