OrderController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\OrderSave;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Support\Facades\Redis;
  7. use Illuminate\Support\Facades\Log;
  8. use App\Models\Order;
  9. use App\Models\Plan;
  10. use App\Models\User;
  11. use App\Utils\Helper;
  12. use Omnipay\Omnipay;
  13. use Stripe\Stripe;
  14. use Stripe\Source;
  15. use Library\BitpayX;
  16. class OrderController extends Controller
  17. {
  18. public function fetch (Request $request) {
  19. $order = Order::where('user_id', $request->session()->get('id'))
  20. ->orderBy('created_at', 'DESC')
  21. ->get();
  22. $plan = Plan::get();
  23. for($i = 0; $i < count($order); $i++) {
  24. for($x = 0; $x < count($plan); $x++) {
  25. if ($order[$i]['plan_id'] === $plan[$x]['id']) {
  26. $order[$i]['plan'] = $plan[$x];
  27. }
  28. }
  29. }
  30. return response([
  31. 'data' => $order
  32. ]);
  33. }
  34. public function details (Request $request) {
  35. $order = Order::where('user_id', $request->session()->get('id'))
  36. ->where('trade_no', $request->input('trade_no'))
  37. ->first();
  38. if (!$order) {
  39. abort(500, '订单不存在');
  40. }
  41. $order['plan'] = Plan::find($order->plan_id);
  42. $order['update_fee'] = config('v2board.plan_update_fee', 0.5);
  43. if (!$order['plan']) {
  44. abort(500, '订阅不存在');
  45. }
  46. return response([
  47. 'data' => $order
  48. ]);
  49. }
  50. public function save (OrderSave $request) {
  51. $plan = Plan::find($request->input('plan_id'));
  52. $user = User::find($request->session()->get('id'));
  53. if (!$plan) {
  54. abort(500, '该订阅不存在');
  55. }
  56. if (!($plan->show || $user->plan_id == $plan->id)) {
  57. abort(500, '该订阅已售罄');
  58. }
  59. if (!$plan->show && !$plan->renew) {
  60. abort(500, '该订阅无法续费,请更换其他订阅');
  61. }
  62. if (empty($plan[$request->input('cycle')])) {
  63. abort(500, '该订阅周期无法进行购买,请选择其他周期');
  64. }
  65. $order = new Order();
  66. $order->user_id = $request->session()->get('id');
  67. $order->plan_id = $plan->id;
  68. $order->cycle = $request->input('cycle');
  69. $order->trade_no = Helper::guid();
  70. $order->total_amount = $plan[$request->input('cycle')];
  71. if ($order->total_amount == 0) {
  72. $order->status = 1;
  73. }
  74. if ($user->expired_at > time() && $order->plan_id !== $user->plan_id) {
  75. $order->type = 3;
  76. if (!(int)config('v2board.plan_is_update', 1)) abort(500, '目前不允许更改订阅,请联系管理员');
  77. $order->total_amount = $order->total_amount + (ceil(($user->expired_at - time()) / 86400) * config('v2board.plan_update_fee', 0.5) * 100);
  78. } else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) {
  79. $order->type = 2;
  80. } else {
  81. $order->type = 1;
  82. }
  83. if ($user->invite_user_id) {
  84. $order->invite_user_id = $user->invite_user_id;
  85. $inviter = User::find($user->invite_user_id);
  86. if ($inviter && $inviter->commission_rate) {
  87. $order->commission_balance = $order->total_amount * ($inviter->commission_rate / 100);
  88. } else {
  89. $order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
  90. }
  91. }
  92. if (!$order->save()) {
  93. abort(500, '订单创建失败');
  94. }
  95. return response([
  96. 'data' => $order->trade_no
  97. ]);
  98. }
  99. public function checkout (Request $request) {
  100. $tradeNo = $request->input('trade_no');
  101. $method = $request->input('method');
  102. $order = Order::where('trade_no', $tradeNo)
  103. ->where('user_id', $request->session()->get('id'))
  104. ->where('status', 0)
  105. ->first();
  106. if (!$order) {
  107. abort(500, '订单不存在或已支付');
  108. }
  109. switch ($method) {
  110. // return type => 0: QRCode / 1: URL
  111. case 0:
  112. // alipayF2F
  113. if (!(int)config('v2board.alipay_enable')) {
  114. abort(500, '支付方式不可用');
  115. }
  116. return response([
  117. 'type' => 0,
  118. 'data' => $this->alipayF2F($tradeNo, $order->total_amount)
  119. ]);
  120. case 2:
  121. // stripeAlipay
  122. if (!(int)config('v2board.stripe_alipay_enable')) {
  123. abort(500, '支付方式不可用');
  124. }
  125. return response([
  126. 'type' => 1,
  127. 'data' => $this->stripeAlipay($order)
  128. ]);
  129. case 3:
  130. // stripeWepay
  131. if (!(int)config('v2board.stripe_wepay_enable')) {
  132. abort(500, '支付方式不可用');
  133. }
  134. return response([
  135. 'type' => 0,
  136. 'data' => $this->stripeWepay($order)
  137. ]);
  138. case 4:
  139. // bitpayX
  140. if (!(int)config('v2board.bitpayx_enable')) {
  141. abort(500, '支付方式不可用');
  142. }
  143. return response([
  144. 'type' => 1,
  145. 'data' => $this->bitpayX($order)
  146. ]);
  147. default:
  148. abort(500, '支付方式不存在');
  149. }
  150. }
  151. public function check (Request $request) {
  152. $tradeNo = $request->input('trade_no');
  153. $order = Order::where('trade_no', $tradeNo)
  154. ->where('user_id', $request->session()->get('id'))
  155. ->first();
  156. if (!$order) {
  157. abort(500, '订单不存在');
  158. }
  159. return response([
  160. 'data' => $order->status
  161. ]);
  162. }
  163. public function getPaymentMethod () {
  164. $data = [];
  165. if ((int)config('v2board.alipay_enable')) {
  166. $alipayF2F = new \StdClass();
  167. $alipayF2F->name = '支付宝';
  168. $alipayF2F->method = 0;
  169. $alipayF2F->icon = 'alipay';
  170. array_push($data, $alipayF2F);
  171. }
  172. if ((int)config('v2board.stripe_alipay_enable')) {
  173. $stripeAlipay = new \StdClass();
  174. $stripeAlipay->name = '支付宝';
  175. $stripeAlipay->method = 2;
  176. $stripeAlipay->icon = 'alipay';
  177. array_push($data, $stripeAlipay);
  178. }
  179. if ((int)config('v2board.stripe_wepay_enable')) {
  180. $stripeWepay = new \StdClass();
  181. $stripeWepay->name = '微信';
  182. $stripeWepay->method = 3;
  183. $stripeWepay->icon = 'wechat';
  184. array_push($data, $stripeWepay);
  185. }
  186. if ((int)config('v2board.bitpayx_enable')) {
  187. $bitpayX = new \StdClass();
  188. $bitpayX->name = '虚拟货币';
  189. $bitpayX->method = 4;
  190. $bitpayX->icon = 'bitcoin';
  191. array_push($data, $bitpayX);
  192. }
  193. return response([
  194. 'data' => $data
  195. ]);
  196. }
  197. private function alipayF2F ($tradeNo, $totalAmount) {
  198. $gateway = Omnipay::create('Alipay_AopF2F');
  199. $gateway->setSignType('RSA2'); //RSA/RSA2
  200. $gateway->setAppId(config('v2board.alipay_appid'));
  201. $gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
  202. $gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
  203. $gateway->setNotifyUrl(url('/api/v1/guest/order/alipayNotify'));
  204. $request = $gateway->purchase();
  205. $request->setBizContent([
  206. 'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
  207. 'out_trade_no' => $tradeNo,
  208. 'total_amount' => $totalAmount / 100
  209. ]);
  210. /** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $response */
  211. $response = $request->send();
  212. $result = $response->getAlipayResponse();
  213. if ($result['code'] !== '10000') {
  214. abort(500, $result['sub_msg']);
  215. }
  216. // 获取收款二维码内容
  217. return $response->getQrCode();
  218. }
  219. private function stripeAlipay ($order) {
  220. $exchange = Helper::exchange('CNY', 'HKD');
  221. if (!$exchange) {
  222. abort(500, '货币转换超时,请稍后再试');
  223. }
  224. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  225. $source = Source::create([
  226. 'amount' => floor($order->total_amount * $exchange),
  227. 'currency' => 'hkd',
  228. 'type' => 'alipay',
  229. 'redirect' => [
  230. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
  231. ]
  232. ]);
  233. if (!$source['redirect']['url']) {
  234. abort(500, '支付网关请求失败');
  235. }
  236. if (!Redis::set($source['id'], $order->trade_no)) {
  237. abort(500, '订单创建失败');
  238. }
  239. Redis::expire($source['id'], 3600);
  240. return $source['redirect']['url'];
  241. }
  242. private function stripeWepay ($order) {
  243. $exchange = Helper::exchange('CNY', 'HKD');
  244. if (!$exchange) {
  245. abort(500, '货币转换超时,请稍后再试');
  246. }
  247. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  248. $source = Source::create([
  249. 'amount' => floor($order->total_amount * $exchange),
  250. 'currency' => 'hkd',
  251. 'type' => 'wechat',
  252. 'redirect' => [
  253. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
  254. ]
  255. ]);
  256. if (!$source['wechat']['qr_code_url']) {
  257. abort(500, '支付网关请求失败');
  258. }
  259. if (!Redis::set($source['id'], $order->trade_no)) {
  260. abort(500, '订单创建失败');
  261. }
  262. Redis::expire($source['id'], 3600);
  263. return $source['wechat']['qr_code_url'];
  264. }
  265. private function bitpayX ($order) {
  266. $bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
  267. $params = [
  268. 'merchant_order_id' => 'V2Board_' . $order->trade_no,
  269. 'price_amount' => $order->total_amount / 100,
  270. 'price_currency' => 'CNY',
  271. 'title' => '支付单号:' . $order->trade_no,
  272. 'description' => '充值:' . $order->total_amount / 100 . ' 元',
  273. 'callback_url' => url('/api/v1/guest/order/bitpayXNotify'),
  274. 'success_url' => config('v2board.app_url', env('APP_URL')) . '/#/order',
  275. 'cancel_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
  276. ];
  277. $strToSign = $bitpayX->prepareSignId($params['merchant_order_id']);
  278. $params['token'] = $bitpayX->sign($strToSign);
  279. $result = $bitpayX->mprequest($params);
  280. Log::info('bitpayXSubmit: ' . json_encode($result));
  281. return isset($result['payment_url']) ? $result['payment_url'] : false;
  282. }
  283. }