From cff5d205bcdc9661af185ebb7971e662f6ddaeb5 Mon Sep 17 00:00:00 2001 From: Tokumeikoi Date: Mon, 20 Jul 2020 19:56:39 +0800 Subject: [PATCH] feature: added epay & mGate payment --- .../Controllers/Admin/ConfigController.php | 12 ++--- .../Controllers/Guest/OrderController.php | 21 ++++++-- app/Http/Controllers/User/OrderController.php | 49 +++++++++++++++---- app/Http/Requests/Admin/ConfigSave.php | 12 ++--- app/Http/Routes/GuestRoute.php | 3 +- library/Epay.php | 42 ++++++++++++++++ library/{PayTaro.php => MGate.php} | 8 +-- 7 files changed, 116 insertions(+), 31 deletions(-) create mode 100644 library/Epay.php rename library/{PayTaro.php => MGate.php} (84%) diff --git a/app/Http/Controllers/Admin/ConfigController.php b/app/Http/Controllers/Admin/ConfigController.php index 28bfd9f1..1db0dd8d 100755 --- a/app/Http/Controllers/Admin/ConfigController.php +++ b/app/Http/Controllers/Admin/ConfigController.php @@ -85,12 +85,12 @@ class ConfigController extends Controller 'bitpayx_name' => config('v2board.bitpayx_name', '在线支付'), 'bitpayx_enable' => (int)config('v2board.bitpayx_enable', 0), 'bitpayx_appsecret' => config('v2board.bitpayx_appsecret'), - // vGate - 'vgate_name' => config('v2board.vgate_name', '在线支付'), - 'vgate_enable' => (int)config('v2board.vgate_enable', 0), - 'vgate_url' => config('v2board.vgate_url'), - 'vgate_app_id' => config('v2board.vgate_app_id'), - 'vgate_app_secret' => config('v2board.vgate_app_secret'), + // mGate + 'mgate_name' => config('v2board.mgate_name', '在线支付'), + 'mgate_enable' => (int)config('v2board.mgate_enable', 0), + 'mgate_url' => config('v2board.mgate_url'), + 'mgate_app_id' => config('v2board.mgate_app_id'), + 'mgate_app_secret' => config('v2board.mgate_app_secret'), // Epay 'epay_name' => config('v2board.epay_name', '在线支付'), 'epay_enable' => (int)config('v2board.epay_enable', 0), diff --git a/app/Http/Controllers/Guest/OrderController.php b/app/Http/Controllers/Guest/OrderController.php index cedfa429..967b294b 100644 --- a/app/Http/Controllers/Guest/OrderController.php +++ b/app/Http/Controllers/Guest/OrderController.php @@ -6,11 +6,12 @@ use App\Services\OrderService; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\Order; +use Library\Epay; use Omnipay\Omnipay; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Cache; use Library\BitpayX; -use Library\PayTaro; +use Library\MGate; class OrderController extends Controller { @@ -128,12 +129,22 @@ class OrderController extends Controller ])); } - public function payTaroNotify(Request $request) + public function mgateNotify(Request $request) { - // Log::info('payTaroNotify: ' . json_encode($request->input())); + $mgate = new MGate(config('v2board.mgate_url'), config('v2board.mgate_app_id'), config('v2board.mgate_app_secret')); + if (!$mgate->verify($request->input())) { + abort(500, 'fail'); + } + if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) { + abort(500, 'fail'); + } + die('success'); + } - $payTaro = new PayTaro(config('v2board.paytaro_app_id'), config('v2board.paytaro_app_secret')); - if (!$payTaro->verify($request->input())) { + public function epayNotify(Request $request) + { + $epay = new Epay(config('v2board.epay_url'), config('v2board.epay_pid'), config('v2board.epay_key')); + if (!$epay->verify($request->input())) { abort(500, 'fail'); } if (!$this->handle($request->input('out_trade_no'), $request->input('trade_no'))) { diff --git a/app/Http/Controllers/User/OrderController.php b/app/Http/Controllers/User/OrderController.php index 04c7db7d..f15f7d4f 100755 --- a/app/Http/Controllers/User/OrderController.php +++ b/app/Http/Controllers/User/OrderController.php @@ -18,7 +18,8 @@ use Omnipay\Omnipay; use Stripe\Stripe; use Stripe\Source; use Library\BitpayX; -use Library\PayTaro; +use Library\MGate; +use Library\Epay; class OrderController extends Controller { @@ -213,12 +214,12 @@ class OrderController extends Controller 'data' => $this->bitpayX($order) ]); case 5: - if (!(int)config('v2board.paytaro_enable')) { + if (!(int)config('v2board.mgate_enable')) { abort(500, '支付方式不可用'); } return response([ 'type' => 1, - 'data' => $this->payTaro($order) + 'data' => $this->mgate($order) ]); case 6: if (!(int)config('v2board.stripe_card_enable')) { @@ -228,6 +229,14 @@ class OrderController extends Controller 'type' => 2, 'data' => $this->stripeCard($order, $request->input('token')) ]); + case 7: + if (!(int)config('v2board.epay_enable')) { + abort(500, '支付方式不可用'); + } + return response([ + 'type' => 1, + 'data' => $this->epay($order) + ]); default: abort(500, '支付方式不存在'); } @@ -282,9 +291,9 @@ class OrderController extends Controller array_push($data, $bitpayX); } - if ((int)config('v2board.paytaro_enable')) { + if ((int)config('v2board.mgate_enable')) { $obj = new \StdClass(); - $obj->name = config('v2board.paytaro_name', '在线支付'); + $obj->name = config('v2board.mgate_name', '在线支付'); $obj->method = 5; $obj->icon = 'wallet'; array_push($data, $obj); @@ -298,6 +307,14 @@ class OrderController extends Controller array_push($data, $obj); } + if ((int)config('v2board.epay_enable')) { + $obj = new \StdClass(); + $obj->name = config('v2board.epay_name', '在线支付'); + $obj->method = 7; + $obj->icon = 'wallet'; + array_push($data, $obj); + } + return response([ 'data' => $data ]); @@ -454,16 +471,28 @@ class OrderController extends Controller return isset($result['payment_url']) ? $result['payment_url'] : false; } - private function payTaro($order) + private function mgate($order) { - $payTaro = new PayTaro(config('v2board.paytaro_app_id'), config('v2board.paytaro_app_secret')); - $result = $payTaro->pay([ - 'app_id' => config('v2board.paytaro_app_id'), + $mgate = new MGate(config('v2board_mgate_url'), config('v2board.mgate_app_id'), config('v2board.mgate_app_secret')); + $result = $mgate->pay([ + 'app_id' => config('v2board.mgate_app_id'), 'out_trade_no' => $order->trade_no, 'total_amount' => $order->total_amount, - 'notify_url' => url('/api/v1/guest/order/payTaroNotify'), + 'notify_url' => url('/api/v1/guest/order/mgateNotify'), 'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order' ]); return $result; } + + private function epay($order) + { + $epay = new Epay(config('v2board.epay_url'), config('v2board.epay_pid'), config('v2board.epay_key')); + return $epay->pay([ + 'money' => $order->total_amount / 100, + 'name' => $order->trade_no, + 'notify_url' => url('/api/v1/guest/order/epayNotify'), + 'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order', + 'out_trade_no' => $order->trade_no + ]); + } } diff --git a/app/Http/Requests/Admin/ConfigSave.php b/app/Http/Requests/Admin/ConfigSave.php index c4185270..4a8e9707 100755 --- a/app/Http/Requests/Admin/ConfigSave.php +++ b/app/Http/Requests/Admin/ConfigSave.php @@ -60,12 +60,12 @@ class ConfigSave extends FormRequest 'bitpayx_name' => '', 'bitpayx_enable' => 'in:0,1', 'bitpayx_appsecret' => '', - // vGate - 'vgate_name' => '', - 'vgate_enable' => 'in:0,1', - 'vgate_url' => 'nullable|url', - 'vgate_app_id' => '', - 'vgate_app_secret' => '', + // mGate + 'mgate_name' => '', + 'mgate_enable' => 'in:0,1', + 'mgate_url' => 'nullable|url', + 'mgate_app_id' => '', + 'mgate_app_secret' => '', // Epay 'epay_name' => '', 'epay_enable' => 'in:0,1', diff --git a/app/Http/Routes/GuestRoute.php b/app/Http/Routes/GuestRoute.php index 9cb62554..fa723036 100644 --- a/app/Http/Routes/GuestRoute.php +++ b/app/Http/Routes/GuestRoute.php @@ -16,7 +16,8 @@ class GuestRoute $router->post('/order/alipayNotify', 'Guest\\OrderController@alipayNotify'); $router->post('/order/stripeNotify', 'Guest\\OrderController@stripeNotify'); $router->post('/order/bitpayXNotify', 'Guest\\OrderController@bitpayXNotify'); - $router->post('/order/payTaroNotify', 'Guest\\OrderController@payTaroNotify'); + $router->post('/order/mgateNotify', 'Guest\\OrderController@mgateNotify'); + $router->post('/order/epayNotify', 'Guset\\OrderController@epayNotify'); // Telegram $router->post('/telegram/webhook', 'Guest\\TelegramController@webhook'); }); diff --git a/library/Epay.php b/library/Epay.php new file mode 100644 index 00000000..64c69242 --- /dev/null +++ b/library/Epay.php @@ -0,0 +1,42 @@ +pid = $pid; + $this->key = $key; + $this->url = $url; + } + + public function pay($params) + { + $params['pid'] = $this->pid; + ksort($params); + reset($params); + $str = stripslashes(urldecode(http_build_query($params))) . $this->key; + $params['sign'] = md5($str); + $params['sign_type'] = 'MD5'; + return $this->url . '/submit.php?' . http_build_query($params); + } + + public function verify($params) + { + $sign = $params['sign']; + unset($params['sign']); + unset($params['sign_type']); + ksort($params); + reset($params); + $str = stripslashes(urldecode(http_build_query($params))) . $this->key; + if ($sign !== md5($str)) { + return false; + } + return true; + } +} diff --git a/library/PayTaro.php b/library/MGate.php similarity index 84% rename from library/PayTaro.php rename to library/MGate.php index f586c51a..76bdba63 100644 --- a/library/PayTaro.php +++ b/library/MGate.php @@ -4,15 +4,17 @@ namespace Library; use \Curl\Curl; -class PayTaro +class MGate { private $appId; private $appSecret; + private $url; - public function __construct($appId, $appSecret) + public function __construct($url, $appId, $appSecret) { $this->appId = $appId; $this->appSecret = $appSecret; + $this->url = $url; } public function pay($params) @@ -21,7 +23,7 @@ class PayTaro $str = http_build_query($params) . $this->appSecret; $params['sign'] = md5($str); $curl = new Curl(); - $curl->post('https://api.paytaro.com/v1/gateway/fetch', http_build_query($params)); + $curl->post($this->url . '/v1/gateway/fetch', http_build_query($params)); $result = $curl->response; if (!$result) { abort(500, '网络异常');