OrderController.php 9.3 KB

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