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

@ -21,26 +21,26 @@ class Bootstrap
* 获取固定运费方式
*
* @param CheckoutService $checkout
* @param Plugin $plugin
* @param Plugin $plugin
* @return array
* @throws \Exception
*/
public function getQuotes(CheckoutService $checkout, Plugin $plugin): array
{
$code = $plugin->code;
$code = $plugin->code;
$pluginResource = (new PluginResource($plugin))->jsonSerialize();
$quotes[] = [
'type' => 'shipping',
'code' => "{$code}.0",
'name' => $pluginResource['name'],
$quotes[] = [
'type' => 'shipping',
'code' => "{$code}.0",
'name' => $pluginResource['name'],
'description' => $pluginResource['description'],
'icon' => $pluginResource['icon'],
'cost' => $this->getShippingFee($checkout),
'icon' => $pluginResource['icon'],
'cost' => $this->getShippingFee($checkout),
];
return $quotes;
}
/**
* 计算固定运费
*
@ -49,16 +49,17 @@ class Bootstrap
*/
public function getShippingFee(CheckoutService $checkout): float|int
{
$totalService = $checkout->totalService;
$amount = $totalService->amount;
$shippingType = plugin_setting('flat_shipping.type', 'fixed');
$totalService = $checkout->totalService;
$amount = $totalService->amount;
$shippingType = plugin_setting('flat_shipping.type', 'fixed');
$shippingValue = plugin_setting('flat_shipping.value', 0);
if ($shippingType == 'fixed') {
return $shippingValue;
} elseif ($shippingType == 'percent') {
return $amount * $shippingValue / 100;
} else {
return 0;
}
return 0;
}
}

View File

@ -10,8 +10,8 @@
*/
return [
'way' => 'Way',
'flat_shipping' => 'Flat Shipping',
'percentage' => 'Percentage',
'way' => 'Way',
'flat_shipping' => 'Flat Shipping',
'percentage' => 'Percentage',
'shipping_value' => 'Shipping Value',
];

View File

@ -10,8 +10,8 @@
*/
return [
'way' => '方式',
'flat_shipping' => '固定运费',
'percentage' => '百分比',
'way' => '方式',
'flat_shipping' => '固定运费',
'percentage' => '百分比',
'shipping_value' => '运费值',
];

View File

@ -11,19 +11,19 @@
return [
[
'name' => 'type',
'name' => 'type',
'label_key' => 'common.flat_shipping',
'type' => 'select',
'options' => [
'type' => 'select',
'options' => [
['value' => 'fixed', 'label_key' => 'common.flat_shipping'],
['value' => 'percent', 'label_key' => 'common.percentage']
['value' => 'percent', 'label_key' => 'common.percentage'],
],
'required' => true,
'required' => true,
],
[
'name' => 'value',
'name' => 'value',
'label_key' => 'common.shipping_value',
'type' => 'string',
'required' => true,
]
'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

@ -20,12 +20,11 @@ class MenusController extends Controller
public function getRoutes(): array
{
return [
'method' => __METHOD__,
'route_list' => []
'method' => __METHOD__,
'route_list' => [],
];
}
public function latestProducts()
{
$products = ProductRepo::getBuilder(['active' => 1])
@ -35,8 +34,9 @@ class MenusController extends Controller
$data = [
'products' => $products,
'items' => ProductSimple::collection($products)->jsonSerialize(),
'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
{
@ -30,29 +29,28 @@ class PaypalController
private function initPaypal()
{
$paypalSetting = plugin_setting('paypal');
$config = [
'mode' => $paypalSetting['sandbox_mode'] ? 'sandbox' : 'live',
'sandbox' => [
'client_id' => $paypalSetting['sandbox_client_id'],
$config = [
'mode' => $paypalSetting['sandbox_mode'] ? 'sandbox' : 'live',
'sandbox' => [
'client_id' => $paypalSetting['sandbox_client_id'],
'client_secret' => $paypalSetting['sandbox_secret'],
],
'live' => [
'client_id' => $paypalSetting['live_client_id'],
'live' => [
'client_id' => $paypalSetting['live_client_id'],
'client_secret' => $paypalSetting['live_secret'],
],
'payment_action' => 'Sale',
'currency' => 'USD',
'notify_url' => '',
'locale' => 'en_US',
'validate_ssl' => false,
'currency' => 'USD',
'notify_url' => '',
'locale' => 'en_US',
'validate_ssl' => false,
];
config(['paypal' => null]);
$this->paypalClient = new PayPal($config);
$token = $this->paypalClient->getAccessToken();
$token = $this->paypalClient->getAccessToken();
$this->paypalClient->setAccessToken($token);
}
/**
* 创建 paypal 订单
* @param Request $request
@ -62,29 +60,28 @@ class PaypalController
public function create(Request $request): JsonResponse
{
$this->initPaypal();
$data = \json_decode($request->getContent(), true);
$data = \json_decode($request->getContent(), true);
$orderNumber = $data['orderNumber'];
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($orderNumber, $customer);
$total = round($order->total, 2);
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($orderNumber, $customer);
$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
@ -94,17 +91,17 @@ class PaypalController
public function capture(Request $request): JsonResponse
{
$this->initPaypal();
$data = \json_decode($request->getContent(), true);
$data = \json_decode($request->getContent(), true);
$orderNumber = $data['orderNumber'];
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($orderNumber, $customer);
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($orderNumber, $customer);
$paypalOrderId = $data['paypalOrderId'];
$result = $this->paypalClient->capturePaymentOrder($paypalOrderId);
$result = $this->paypalClient->capturePaymentOrder($paypalOrderId);
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

@ -11,5 +11,5 @@
return [
'sandbox_mode' => 'Sandbox Mode',
'enabled' => 'Enabled',
'enabled' => 'Enabled',
];

View File

@ -11,5 +11,5 @@
return [
'sandbox_mode' => '沙盒模式',
'enabled' => '开启',
'enabled' => '开启',
];

View File

@ -11,46 +11,46 @@
return [
[
'name' => 'sandbox_client_id',
'label' => 'Sandbox Client ID',
'type' => 'string',
'required' => true,
'rules' => 'required|size:80',
'name' => 'sandbox_client_id',
'label' => 'Sandbox Client ID',
'type' => 'string',
'required' => true,
'rules' => 'required|size:80',
'description' => '沙盒模式 Client ID',
],
[
'name' => 'sandbox_secret',
'label' => 'Sandbox Secret',
'type' => 'string',
'required' => true,
'rules' => 'required|size:80',
'name' => 'sandbox_secret',
'label' => 'Sandbox Secret',
'type' => 'string',
'required' => true,
'rules' => 'required|size:80',
'description' => '沙盒模式 Secret',
],
[
'name' => 'live_client_id',
'label' => 'Live Client ID',
'type' => 'string',
'required' => true,
'rules' => 'required|size:80',
'name' => 'live_client_id',
'label' => 'Live Client ID',
'type' => 'string',
'required' => true,
'rules' => 'required|size:80',
'description' => '正式环境 Client ID',
],
[
'name' => 'live_secret',
'label' => 'Live Secret',
'type' => 'string',
'required' => true,
'rules' => 'required|size:80',
'name' => 'live_secret',
'label' => 'Live Secret',
'type' => 'string',
'required' => true,
'rules' => 'required|size:80',
'description' => '正式环境 Secret',
],
[
'name' => 'sandbox_mode',
'label_key' => 'setting.sandbox_mode',
'type' => 'select',
'options' => [
'name' => 'sandbox_mode',
'label_key' => 'setting.sandbox_mode',
'type' => 'select',
'options' => [
['value' => '1', 'label_key' => 'setting.enabled'],
['value' => '0', 'label' => '关闭']
['value' => '0', 'label' => '关闭'],
],
'required' => true,
'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
@ -29,9 +29,9 @@ class ShopSocialController extends Controller
continue;
}
$config = [
'client_id' => $providerSetting['key'],
'client_id' => $providerSetting['key'],
'client_secret' => $providerSetting['secret'],
'redirect' => shop_route('social.callback', $provider),
'redirect' => shop_route('social.callback', $provider),
];
Config::set("services.{$provider}", $config);
}
@ -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,27 +10,27 @@
*/
return [
'alipay' => 'Alipay',
'azure' => 'Azure',
'dingtalk' => 'Dingtalk',
'douyin' => 'Douyin',
'douban' => 'Douban',
'facebook' => 'Facebook',
'feishu' => 'Feishu',
'figma' => 'Figma',
'github' => 'Github',
'gitee' => 'Gitee',
'google' => 'Google',
'line' => 'Line',
'linkedin' => 'Linkedin',
'alipay' => 'Alipay',
'azure' => 'Azure',
'dingtalk' => 'Dingtalk',
'douyin' => 'Douyin',
'douban' => 'Douban',
'facebook' => 'Facebook',
'feishu' => 'Feishu',
'figma' => 'Figma',
'github' => 'Github',
'gitee' => 'Gitee',
'google' => 'Google',
'line' => 'Line',
'linkedin' => 'Linkedin',
'open-wework' => 'Open-wework',
'outlook' => 'Outlook',
'qcloud' => 'Qcloud',
'qq' => 'QQ',
'taobao' => 'Taobao',
'tapd' => 'Tapd',
'twitter' => 'Twitter',
'wechat' => 'Wechat',
'wework' => 'Wework',
'weibo' => 'Weibo',
'outlook' => 'Outlook',
'qcloud' => 'Qcloud',
'qq' => 'QQ',
'taobao' => 'Taobao',
'tapd' => 'Tapd',
'twitter' => 'Twitter',
'wechat' => 'Wechat',
'wework' => 'Wework',
'weibo' => 'Weibo',
];

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',
@ -54,5 +54,5 @@ return [
'google' => 'Google',
'twitter' => 'Twitter',
'instagram' => 'Instagram',
'instagram' => 'Instagram',
];

View File

@ -10,27 +10,27 @@
*/
return [
'alipay' => '支付宝',
'azure' => 'Azure',
'dingtalk' => '钉钉',
'douyin' => '抖音',
'douban' => '豆瓣',
'facebook' => 'Facebook',
'feishu' => '飞书',
'figma' => 'Figma',
'github' => 'GitHub',
'gitee' => 'Gitee',
'google' => 'Google',
'line' => 'Line',
'linkedin' => 'Linkedin',
'alipay' => '支付宝',
'azure' => 'Azure',
'dingtalk' => '钉钉',
'douyin' => '抖音',
'douban' => '豆瓣',
'facebook' => 'Facebook',
'feishu' => '飞书',
'figma' => 'Figma',
'github' => 'GitHub',
'gitee' => 'Gitee',
'google' => 'Google',
'line' => 'Line',
'linkedin' => 'Linkedin',
'open-wework' => 'open-wework',
'outlook' => 'Outlook',
'qcloud' => '腾讯云',
'qq' => 'QQ',
'taobao' => '淘宝',
'tapd' => 'Tapd',
'twitter' => 'Twitter',
'wechat' => '微信',
'wework' => '企业微信',
'weibo' => '微博',
'outlook' => 'Outlook',
'qcloud' => '腾讯云',
'qq' => 'QQ',
'taobao' => '淘宝',
'tapd' => 'Tapd',
'twitter' => 'Twitter',
'wechat' => '微信',
'wework' => '企业微信',
'weibo' => '微博',
];

View File

@ -10,38 +10,38 @@
*/
return [
// Text
'text_module' => '模块',
'text_success' => '成功: 您成功修改第三方登录配置!',
'text_copyright' => 'OpenCart.cn <a href="http://www.opencart.cn" target="_blank">获取帮助</a> &copy; %s',
'text_omni_explain' => '本模块支持微Facebook, Twitter, Google等第三方登录',
// Text
'text_module' => '模块',
'text_success' => '成功: 您成功修改第三方登录配置!',
'text_copyright' => 'OpenCart.cn <a href="http://www.opencart.cn" target="_blank">获取帮助</a> &copy; %s',
'text_omni_explain' => '本模块支持微Facebook, Twitter, Google等第三方登录',
'text_omni_explain_2' => '要使用第三方登录, 需要到对应平台申请开通, 并把获取到的 ID 和密钥填写到上面对应的输入框',
'text_wechat_title' => '微信扫码登录申请地址',
'text_wechat_info' => '微信开放平台',
'text_qq_title' => 'QQ登录申请地址',
'text_qq_info' => 'QQ互联',
'text_weibo_title' => '微博登录申请地址',
'text_weibo_info' => '微博开放平台',
'text_wechat_title' => '微信扫码登录申请地址',
'text_wechat_info' => '微信开放平台',
'text_qq_title' => 'QQ登录申请地址',
'text_qq_info' => 'QQ互联',
'text_weibo_title' => '微博登录申请地址',
'text_weibo_info' => '微博开放平台',
'text_facebook_title' => 'Facebook登录申请地址',
'text_google_title' => 'Google登录申请地址',
'text_twitter_title' => 'Twitter登录申请地址',
'text_google_title' => 'Google登录申请地址',
'text_twitter_title' => 'Twitter登录申请地址',
'text_help_msg' => '帮助信息',
// Entry
'entry_status' => '状态',
'entry_bind' => '强制绑定',
'entry_debug' => '调试模式',
// Entry
'entry_status' => '状态',
'entry_bind' => '强制绑定',
'entry_debug' => '调试模式',
'entry_provider' => '类型',
'entry_key' => 'Client ID',
'entry_secret' => 'Client Secret',
'entry_scope' => 'Flags (可选项)',
'entry_callback' => '回调地址',
'entry_sort_order' => '排序',
'entry_provider' => '类型',
'entry_key' => 'Client ID',
'entry_secret' => 'Client Secret',
'entry_scope' => 'Flags (可选项)',
'entry_callback' => '回调地址',
'entry_sort_order' => '排序',
// Button
'button_add_row' => '添加类型',
// Button
'button_add_row' => '添加类型',
// Error
'error_permission' => '警告: 您没有权限修改此配置!',
// 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
{
@ -35,10 +35,11 @@ class CustomerRepo
$items = [];
foreach (self::PROVIDERS as $provider) {
$items[] = [
'code' => $provider,
'label' => trans("Social::providers.{$provider}")
'code' => $provider,
'label' => trans("Social::providers.{$provider}"),
];
}
return $items;
}
@ -51,7 +52,7 @@ class CustomerRepo
*/
public static function createCustomer($provider, User $userData): Customer
{
$social = self::getCustomerByProvider($provider, $userData->getId());
$social = self::getCustomerByProvider($provider, $userData->getId());
$customer = $social->customer ?? null;
if ($customer) {
return $customer;
@ -64,19 +65,19 @@ class CustomerRepo
$customer = Customer::query()->where('email', $email)->first();
if (empty($customer)) {
$customerData = [
'from' => $provider,
'email' => $email,
'name' => $userData->getName(),
'from' => $provider,
'email' => $email,
'name' => $userData->getName(),
'avatar' => $userData->getAvatar(),
];
$customer = AccountService::register($customerData);
}
self::createSocial($customer, $provider, $userData);
return $customer;
}
/**
* @param $customer
* @param $provider
@ -91,17 +92,17 @@ class CustomerRepo
}
$socialData = [
'customer_id' => $customer->id,
'provider' => $provider,
'user_id' => $userData->getId(),
'union_id' => '',
'customer_id' => $customer->id,
'provider' => $provider,
'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
@ -28,17 +28,19 @@ class StripeController extends Controller
public function capture(Request $request): array
{
try {
$number = request('order_number');
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($number, $customer);
$number = request('order_number');
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($number, $customer);
$creditCardData = $request->all();
$result = (new StripePaymentService($order))->capture($creditCardData);
$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

@ -12,20 +12,20 @@
return [
'publishable_key' => 'Publishable Key',
'title_info' => 'Card information',
'cardnum' => 'Cardnum',
'title_info' => 'Card information',
'cardnum' => 'Cardnum',
'expiration_date' => 'Expiration Date',
'year' => 'Year',
'month' => 'Month',
'cvv' => 'Cvv',
'remenber' => 'Keep this card in mind for future use',
'btn_submit' => 'Submit',
'year' => 'Year',
'month' => 'Month',
'cvv' => 'Cvv',
'remenber' => 'Keep this card in mind for future use',
'btn_submit' => 'Submit',
'error_cardnum' => 'Please enter the card number',
'error_cvv' => 'Please enter the security code',
'error_year' => 'Please select the year',
'error_month' => 'Please select a month',
'error_cardnum' => 'Please enter the card number',
'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',
'capture_fail' => 'Capture Failed',
];

View File

@ -12,20 +12,20 @@
return [
'publishable_key' => '公钥',
'title_info' => '卡信息',
'cardnum' => '卡号',
'title_info' => '卡信息',
'cardnum' => '卡号',
'expiration_date' => '截止日期',
'year' => '选择年',
'month' => '选择月',
'cvv' => '安全码',
'remenber' => '记住这张卡以便将来使用',
'btn_submit' => '提交支付',
'year' => '选择年',
'month' => '选择月',
'cvv' => '安全码',
'remenber' => '记住这张卡以便将来使用',
'btn_submit' => '提交支付',
'error_cardnum' => '请输入卡号',
'error_cvv' => '请输入安全码',
'error_year' => '请选择年',
'error_month' => '请选择月',
'error_cardnum' => '请输入卡号',
'error_cvv' => '请输入安全码',
'error_year' => '请选择年',
'error_month' => '请选择月',
'capture_success' => '支付成功',
'capture_fail' => '支付失败',
'capture_fail' => '支付失败',
];

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',
];
@ -33,37 +33,37 @@ class StripePaymentService extends PaymentService
Stripe::setApiKey($apiKey);
$token = Token::create([
'card' => [
'number' => $creditCardData['cardnum'],
'exp_year' => $creditCardData['year'],
'number' => $creditCardData['cardnum'],
'exp_year' => $creditCardData['year'],
'exp_month' => $creditCardData['month'],
'cvc' => $creditCardData['cvv'],
'cvc' => $creditCardData['cvv'],
],
]);
$tokenId = $token['id'];
$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(
'amount' => $total,
$stripeChargeParameters = [
'amount' => $total,
'currency' => $currency,
'metadata' => array(
'metadata' => [
'orderId' => $this->order->id,
),
'source' => $tokenId,
],
'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

@ -11,30 +11,30 @@
return [
[
'name' => 'publishable_key',
'label_key' => 'common.publishable_key',
'type' => 'string',
'required' => true,
'rules' => 'required|min:32',
'name' => 'publishable_key',
'label_key' => 'common.publishable_key',
'type' => 'string',
'required' => true,
'rules' => 'required|min:32',
'description' => '公钥(Publishable key)',
],
[
'name' => 'secret_key',
'label' => '密钥',
'type' => 'string',
'required' => true,
'rules' => 'required|min:32',
'name' => 'secret_key',
'label' => '密钥',
'type' => 'string',
'required' => true,
'rules' => 'required|min:32',
'description' => '密钥(Secret key)',
],
[
'name' => 'test_mode',
'label' => '测试模式',
'type' => 'select',
'options' => [
'name' => 'test_mode',
'label' => '测试模式',
'type' => 'select',
'options' => [
['value' => '1', 'label' => '开启'],
['value' => '0', 'label' => '关闭']
['value' => '0', 'label' => '关闭'],
],
'required' => true,
'required' => true,
'description' => '如开启测试模式请填写测试公钥和密钥, 关闭测试模式则填写正式公钥和密钥',
]
],
];