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();
$order = OrderRepo::getOrderByNumber($number, $customer);
hook_action('account.order.pay.before', ['order' => $order]);
return (new PaymentService($order))->pay();
} catch (\Exception $e) {
return redirect(shop_route('account.order.show', $number))->withErrors($e->getMessage());

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,7 +10,7 @@
*/
return [
// Text
// Text
'text_module' => 'Modules',
'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',
@ -27,7 +27,7 @@ return [
'text_twitter_title' => 'Twitter login application address',
'text_help_msg' => 'help information',
// Entry
// Entry
'entry_status' => 'Status',
'entry_bind' => 'Force Bind',
'entry_debug' => 'Debug Mode',
@ -39,13 +39,13 @@ return [
'entry_callback' => 'Callback URL',
'entry_sort_order' => 'Sort Order',
// Button
// Button
'button_add_row' => 'Add Provider',
// Error
// Error
'error_permission' => 'Warning: You do not have permission to modify module OpenCart OmniAuth!',
// Providers
// Providers
'wechat' => 'WeChat',
'wechatofficial' => 'WeChatOfficial',
'qq' => 'QQ',

View File

@ -10,7 +10,7 @@
*/
return [
// Text
// Text
'text_module' => '模块',
'text_success' => '成功: 您成功修改第三方登录配置!',
'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_help_msg' => '帮助信息',
// Entry
// Entry
'entry_status' => '状态',
'entry_bind' => '强制绑定',
'entry_debug' => '调试模式',
@ -39,9 +39,9 @@ return [
'entry_callback' => '回调地址',
'entry_sort_order' => '排序',
// Button
// Button
'button_add_row' => '添加类型',
// Error
// Error
'error_permission' => '警告: 您没有权限修改此配置!',
];

View File

@ -20,7 +20,7 @@ class CustomerSocial extends Model
public $table = 'customer_socials';
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

View File

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

View File

@ -11,10 +11,10 @@
namespace Plugin\Stripe\Controllers;
use Illuminate\Http\Request;
use Beike\Repositories\OrderRepo;
use Beike\Services\StateMachineService;
use Beike\Shop\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Plugin\Stripe\Services\StripePaymentService;
class StripeController extends Controller
@ -35,10 +35,12 @@ class StripeController extends Controller
$result = (new StripePaymentService($order))->capture($creditCardData);
if ($result) {
StateMachineService::getInstance($order)->changeStatus(StateMachineService::PAID);
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) {
return json_fail($e->getMessage());
}

View File

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

View File

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