OrderController.php 8.3 KB

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