OrderController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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['upgrade_fee'] = config('v2board.upgrade_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. $order = new Order();
  60. $order->user_id = $request->session()->get('id');
  61. $order->plan_id = $plan->id;
  62. $order->cycle = $request->input('cycle');
  63. $order->trade_no = Helper::guid();
  64. $order->total_amount = $plan[$request->input('cycle')];
  65. if ($user->expired_at > time() && $order->plan_id !== $user->plan_id) {
  66. $order->type = 3;
  67. if ($plan->transfer_enable * 1073741824 < $user->transfer_enable) abort(500, '目前仅允许升级订阅计划');
  68. } else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) {
  69. $order->type = 2;
  70. } else {
  71. $order->type = 1;
  72. }
  73. if ($user->invite_user_id) {
  74. $order->invite_user_id = $user->invite_user_id;
  75. $order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
  76. }
  77. if (!$order->save()) {
  78. abort(500, '订单创建失败');
  79. }
  80. return response([
  81. 'data' => $order->trade_no
  82. ]);
  83. }
  84. public function checkout (Request $request) {
  85. $tradeNo = $request->input('trade_no');
  86. $method = $request->input('method');
  87. $order = Order::where('trade_no', $tradeNo)
  88. ->where('user_id', $request->session()->get('id'))
  89. ->where('status', 0)
  90. ->first();
  91. if (!$order) {
  92. abort(500, '订单不存在或以支付');
  93. }
  94. switch ($method) {
  95. // return type => 0: QRCode / 1: URL
  96. case 0:
  97. // alipayF2F
  98. if (!(int)config('v2board.alipay_enable')) {
  99. abort(500, '支付方式不可用');
  100. }
  101. return response([
  102. 'type' => 0,
  103. 'data' => $this->alipayF2F($tradeNo, $order->total_amount)
  104. ]);
  105. case 2:
  106. // stripeAlipay
  107. if (!(int)config('v2board.stripe_alipay_enable')) {
  108. abort(500, '支付方式不可用');
  109. }
  110. return response([
  111. 'type' => 1,
  112. 'data' => $this->stripeAlipay($order)
  113. ]);
  114. case 3:
  115. // stripeWepay
  116. if (!(int)config('v2board.stripe_wepay_enable')) {
  117. abort(500, '支付方式不可用');
  118. }
  119. return response([
  120. 'type' => 0,
  121. 'data' => $this->stripeWepay($order)
  122. ]);
  123. default:
  124. abort(500, '支付方式不存在');
  125. }
  126. }
  127. public function check (Request $request) {
  128. $tradeNo = $request->input('trade_no');
  129. $order = Order::where('trade_no', $tradeNo)
  130. ->where('user_id', $request->session()->get('id'))
  131. ->first();
  132. if (!$order) {
  133. abort(500, '订单不存在');
  134. }
  135. return response([
  136. 'data' => $order->status
  137. ]);
  138. }
  139. public function getPaymentMethod () {
  140. $data = [];
  141. if ((int)config('v2board.alipay_enable')) {
  142. $alipayF2F = new \StdClass();
  143. $alipayF2F->name = '支付宝';
  144. $alipayF2F->method = 0;
  145. $alipayF2F->icon = 'alipay';
  146. array_push($data, $alipayF2F);
  147. }
  148. if ((int)config('v2board.stripe_alipay_enable')) {
  149. $stripeAlipay = new \StdClass();
  150. $stripeAlipay->name = '支付宝';
  151. $stripeAlipay->method = 2;
  152. $stripeAlipay->icon = 'alipay';
  153. array_push($data, $stripeAlipay);
  154. }
  155. if ((int)config('v2board.stripe_wepay_enable')) {
  156. $stripeWepay = new \StdClass();
  157. $stripeWepay->name = '微信';
  158. $stripeWepay->method = 3;
  159. $stripeWepay->icon = 'wechat';
  160. array_push($data, $stripeWepay);
  161. }
  162. return response([
  163. 'data' => $data
  164. ]);
  165. }
  166. private function alipayF2F ($tradeNo, $totalAmount) {
  167. $gateway = Omnipay::create('Alipay_AopF2F');
  168. $gateway->setSignType('RSA2'); //RSA/RSA2
  169. $gateway->setAppId(config('v2board.alipay_appid'));
  170. $gateway->setPrivateKey(config('v2board.alipay_privkey')); // 可以是路径,也可以是密钥内容
  171. $gateway->setAlipayPublicKey(config('v2board.alipay_pubkey')); // 可以是路径,也可以是密钥内容
  172. $gateway->setNotifyUrl(config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/alipayNotify');
  173. $request = $gateway->purchase();
  174. $request->setBizContent([
  175. 'subject' => config('v2board.app_name', 'V2Board') . ' - 订阅',
  176. 'out_trade_no' => $tradeNo,
  177. 'total_amount' => $totalAmount / 100
  178. ]);
  179. /** @var \Omnipay\Alipay\Responses\AopTradePreCreateResponse $response */
  180. $response = $request->send();
  181. $result = $response->getAlipayResponse();
  182. if ($result['code'] !== '10000') {
  183. abort(500, $result['sub_msg']);
  184. }
  185. // 获取收款二维码内容
  186. return $response->getQrCode();
  187. }
  188. private function stripeAlipay ($order) {
  189. $exchange = Helper::exchange('CNY', 'HKD');
  190. if (!$exchange) {
  191. abort(500, '货币转换超时,请稍后再试');
  192. }
  193. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  194. $source = Source::create([
  195. 'amount' => floor($order->total_amount * $exchange),
  196. 'currency' => 'hkd',
  197. 'type' => 'alipay',
  198. 'redirect' => [
  199. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/stripeReturn'
  200. ]
  201. ]);
  202. if (!$source['redirect']['url']) {
  203. abort(500, '支付网关请求失败');
  204. }
  205. $order->callback_no = $source['id'];
  206. if (!$order->save()) {
  207. abort(500, '订单更新失败');
  208. }
  209. return $source['redirect']['url'];
  210. }
  211. private function stripeWepay ($order) {
  212. $exchange = Helper::exchange('CNY', 'HKD');
  213. if (!$exchange) {
  214. abort(500, '货币转换超时,请稍后再试');
  215. }
  216. Stripe::setApiKey(config('v2board.stripe_sk_live'));
  217. $source = Source::create([
  218. 'amount' => floor($order->total_amount * $exchange),
  219. 'currency' => 'hkd',
  220. 'type' => 'wechat',
  221. 'redirect' => [
  222. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/api/v1/guest/order/stripeReturn'
  223. ]
  224. ]);
  225. if (!$source['wechat']['qr_code_url']) {
  226. abort(500, '支付网关请求失败');
  227. }
  228. $order->callback_no = $source['id'];
  229. if (!$order->save()) {
  230. abort(500, '订单更新失败');
  231. }
  232. return $source['wechat']['qr_code_url'];
  233. }
  234. }