OrderController.php 9.3 KB

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