OrderController.php 12 KB

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