OrderController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 1: $order->discount_amount = $order->total_amount * ($coupon->value / 100);
  109. }
  110. $order->amount_total = $order->amount_total - $order->discount_amount;
  111. }
  112. // free process
  113. if ($order->total_amount <= 0) {
  114. $order->status = 1;
  115. }
  116. if (!$order->save()) {
  117. abort(500, '订单创建失败');
  118. }
  119. return response([
  120. 'data' => $order->trade_no
  121. ]);
  122. }
  123. public function checkout (Request $request) {
  124. $tradeNo = $request->input('trade_no');
  125. $method = $request->input('method');
  126. $order = Order::where('trade_no', $tradeNo)
  127. ->where('user_id', $request->session()->get('id'))
  128. ->where('status', 0)
  129. ->first();
  130. if (!$order) {
  131. abort(500, '订单不存在或已支付');
  132. }
  133. switch ($method) {
  134. // return type => 0: QRCode / 1: URL
  135. case 0:
  136. // alipayF2F
  137. if (!(int)config('v2board.alipay_enable')) {
  138. abort(500, '支付方式不可用');
  139. }
  140. return response([
  141. 'type' => 0,
  142. 'data' => $this->alipayF2F($tradeNo, $order->total_amount)
  143. ]);
  144. case 2:
  145. // stripeAlipay
  146. if (!(int)config('v2board.stripe_alipay_enable')) {
  147. abort(500, '支付方式不可用');
  148. }
  149. return response([
  150. 'type' => 1,
  151. 'data' => $this->stripeAlipay($order)
  152. ]);
  153. case 3:
  154. // stripeWepay
  155. if (!(int)config('v2board.stripe_wepay_enable')) {
  156. abort(500, '支付方式不可用');
  157. }
  158. return response([
  159. 'type' => 0,
  160. 'data' => $this->stripeWepay($order)
  161. ]);
  162. case 4:
  163. // bitpayX
  164. if (!(int)config('v2board.bitpayx_enable')) {
  165. abort(500, '支付方式不可用');
  166. }
  167. return response([
  168. 'type' => 1,
  169. 'data' => $this->bitpayX($order)
  170. ]);
  171. default:
  172. abort(500, '支付方式不存在');
  173. }
  174. }
  175. public function check (Request $request) {
  176. $tradeNo = $request->input('trade_no');
  177. $order = Order::where('trade_no', $tradeNo)
  178. ->where('user_id', $request->session()->get('id'))
  179. ->first();
  180. if (!$order) {
  181. abort(500, '订单不存在');
  182. }
  183. return response([
  184. 'data' => $order->status
  185. ]);
  186. }
  187. public function getPaymentMethod () {
  188. $data = [];
  189. if ((int)config('v2board.alipay_enable')) {
  190. $alipayF2F = new \StdClass();
  191. $alipayF2F->name = '支付宝';
  192. $alipayF2F->method = 0;
  193. $alipayF2F->icon = 'alipay';
  194. array_push($data, $alipayF2F);
  195. }
  196. if ((int)config('v2board.stripe_alipay_enable')) {
  197. $stripeAlipay = new \StdClass();
  198. $stripeAlipay->name = '支付宝';
  199. $stripeAlipay->method = 2;
  200. $stripeAlipay->icon = 'alipay';
  201. array_push($data, $stripeAlipay);
  202. }
  203. if ((int)config('v2board.stripe_wepay_enable')) {
  204. $stripeWepay = new \StdClass();
  205. $stripeWepay->name = '微信';
  206. $stripeWepay->method = 3;
  207. $stripeWepay->icon = 'wechat';
  208. array_push($data, $stripeWepay);
  209. }
  210. if ((int)config('v2board.bitpayx_enable')) {
  211. $bitpayX = new \StdClass();
  212. $bitpayX->name = '虚拟货币';
  213. $bitpayX->method = 4;
  214. $bitpayX->icon = 'bitcoin';
  215. array_push($data, $bitpayX);
  216. }
  217. return response([
  218. 'data' => $data
  219. ]);
  220. }
  221. private function alipayF2F ($tradeNo, $totalAmount) {
  222. $gateway = Omnipay::create('Alipay_AopF2F');
  223. $gateway->setSignType('RSA2'); //RSA/RSA2
  224. $gateway->setAppId(config('v2board.alipay_appid'));
  225. $gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
  226. $gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
  227. $gateway->setNotifyUrl(url('/api/v1/guest/order/alipayNotify'));
  228. $request = $gateway->purchase();
  229. $request->setBizContent([
  230. 'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
  231. 'out_trade_no' => $tradeNo,
  232. 'total_amount' => $totalAmount / 100
  233. ]);
  234. /** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $response */
  235. $response = $request->send();
  236. $result = $response->getAlipayResponse();
  237. if ($result['code'] !== '10000') {
  238. abort(500, $result['sub_msg']);
  239. }
  240. // 获取收款二维码内容
  241. return $response->getQrCode();
  242. }
  243. private function stripeAlipay ($order) {
  244. $exchange = Helper::exchange('CNY', 'HKD');
  245. if (!$exchange) {
  246. abort(500, '货币转换超时,请稍后再试');
  247. }
  248. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  249. $source = Source::create([
  250. 'amount' => floor($order->total_amount * $exchange),
  251. 'currency' => 'hkd',
  252. 'type' => 'alipay',
  253. 'redirect' => [
  254. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
  255. ]
  256. ]);
  257. if (!$source['redirect']['url']) {
  258. abort(500, '支付网关请求失败');
  259. }
  260. if (!Redis::set($source['id'], $order->trade_no)) {
  261. abort(500, '订单创建失败');
  262. }
  263. Redis::expire($source['id'], 3600);
  264. return $source['redirect']['url'];
  265. }
  266. private function stripeWepay ($order) {
  267. $exchange = Helper::exchange('CNY', 'HKD');
  268. if (!$exchange) {
  269. abort(500, '货币转换超时,请稍后再试');
  270. }
  271. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  272. $source = Source::create([
  273. 'amount' => floor($order->total_amount * $exchange),
  274. 'currency' => 'hkd',
  275. 'type' => 'wechat',
  276. 'redirect' => [
  277. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
  278. ]
  279. ]);
  280. if (!$source['wechat']['qr_code_url']) {
  281. abort(500, '支付网关请求失败');
  282. }
  283. if (!Redis::set($source['id'], $order->trade_no)) {
  284. abort(500, '订单创建失败');
  285. }
  286. Redis::expire($source['id'], 3600);
  287. return $source['wechat']['qr_code_url'];
  288. }
  289. private function bitpayX ($order) {
  290. $bitpayX = new BitpayX(config('v2board.bitpayx_appsecret'));
  291. $params = [
  292. 'merchant_order_id' => 'V2Board_' . $order->trade_no,
  293. 'price_amount' => $order->total_amount / 100,
  294. 'price_currency' => 'CNY',
  295. 'title' => '支付单号:' . $order->trade_no,
  296. 'description' => '充值:' . $order->total_amount / 100 . ' 元',
  297. 'callback_url' => url('/api/v1/guest/order/bitpayXNotify'),
  298. 'success_url' => config('v2board.app_url', env('APP_URL')) . '/#/order',
  299. 'cancel_url' => config('v2board.app_url', env('APP_URL')) . '/#/order'
  300. ];
  301. $strToSign = $bitpayX->prepareSignId($params['merchant_order_id']);
  302. $params['token'] = $bitpayX->sign($strToSign);
  303. $result = $bitpayX->mprequest($params);
  304. Log::info('bitpayXSubmit: ' . json_encode($result));
  305. return isset($result['payment_url']) ? $result['payment_url'] : false;
  306. }
  307. }