PayController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use Omnipay\Omnipay;
  4. use App\Helper\LogHelper;
  5. use App\Helper\PayHelper;
  6. use App\Helper\JpushHelper;
  7. use App\Models\User;
  8. use App\Models\order;
  9. use App\Models\AccountLog;
  10. use App\Services\Base\ErrorCode;
  11. use Illuminate\Support\Facades\Request;
  12. use Illuminate\Support\Facades\Validator;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Config;
  15. use Illuminate\Support\Facades\Auth;
  16. use Illuminate\Support\Facades\Log;
  17. class PayController extends Controller
  18. {
  19. use PayHelper, LogHelper,JpushHelper;
  20. public function alipayNotify() {
  21. $rawInfo = Request::all();
  22. Log::info('aliplay callback');
  23. Log::info($rawInfo);
  24. $config = Config::get('laravel-omnipay.gateways.alipay');
  25. $gateway = Omnipay::create($config['driver']);
  26. print_r($gateway);exit;
  27. $gateway->setEnvironment($config['options']['environment']);
  28. $gateway->setAppId($config['options']['appid']);
  29. $gateway->setEncryptKey($config['options']['encrypt_key']);
  30. $gateway->setPrivateKey($config['options']['prikey']);
  31. $gateway->setAlipayPublicKey($config['options']['ali_pubkey']);
  32. $gateway->setNotifyUrl($config['options']['notify_url']);
  33. $request = $gateway->completePurchase();
  34. $request->setParams($rawInfo);//Optional
  35. try {
  36. $response = $request->send();
  37. if($response->isPaid()){
  38. echo 1;exit;
  39. // Log::info('支付成功');
  40. // $out_trade_no = $rawInfo['out_trade_no'];
  41. // $order = Order::where('transaction_id', '=', $out_trade_no)->first();
  42. // if (!$order) {
  43. // Log::error('找不到订单' . $out_trade_no);
  44. // return 'fail';
  45. // }
  46. // $master_amount = $order->number;
  47. // $slave_amount = 0;
  48. // if (!empty($order->ext_info)) {
  49. // $extInfo = json_decode($order->ext_info, true);
  50. // if ($extInfo !== null) {
  51. // $cc = $extInfo['cc_bonus'];
  52. // $master_amount = round(($cc / 100) * $order->number);
  53. // $slave_amount = $order->number - $master_amount;
  54. // }
  55. // }
  56. // $tp = '';
  57. // // $u = null;
  58. // if ($order->user_type == Order::USER_TYPE_MERCHANT) {
  59. // $tp = 'Merchant';
  60. // // $u = Merchant::find($order->user_id);
  61. // } elseif ($order->user_type == Order::USER_TYPE_MEMBER) {
  62. // $tp = 'Member';
  63. // //Not handled here
  64. // }
  65. // $u = User::find($order->user_id);
  66. // if (!$u) {
  67. // Log::error('用户不存在' . $order->user_type . ', ' . $order->user_id);
  68. // return 'success';
  69. // }
  70. // DB::beginTransaction();
  71. // if ($order->goods_type == Order::GOODS_TYPE_BALANCE||$order->goods_type == Order::GOODS_TYPE_COSUME) {
  72. // $cType = AccountLog::TYPE_BALANCE;
  73. // // $u->account_balance += $order->number;
  74. // $u->balance += $master_amount;
  75. // } elseif ($order->goods_type == Order::GOODS_TYPE_CREDIT) {
  76. // $cType = AccountLog::TYPE_CREDIT;
  77. // // $u->credit_balance += $order->number;
  78. // $u->credit += $master_amount;
  79. // } else {
  80. // Log::error('商品不存在' . $order->goods_type);
  81. // return 'success';
  82. // }
  83. // Log::info('支付金额 '.$master_amount);
  84. // if ($u->save()) {
  85. // //更新订单状态
  86. // $order->status = Order::STATUS_FINISHED;
  87. // $order->save();
  88. // //记日志
  89. // $amount = $rawInfo['total_amount'] * 100;
  90. // $_ops = AccountLog::getAllop();
  91. // $this->logAccount($tp, $u->id, $u->name,
  92. // AccountLog::OP_CHARGE, $cType, $master_amount,
  93. // AccountLog::DIRECTION_INC, $cType == AccountLog::TYPE_BALANCE ? $u->balance : $u->credit, AccountLog::CHANNEL_ALIPAY);
  94. // Log::info('支付完成');
  95. // } else {
  96. // DB::rollBack();
  97. // Log::error('保存数据失败');
  98. // }
  99. // DB::commit();
  100. // return 'success';
  101. }else{
  102. Log::error('支付失败');
  103. return 'fail';
  104. }
  105. } catch (\Exception $e) {
  106. Log::error('支付异常' . $e->getMessage());
  107. return 'fail';
  108. }
  109. }
  110. public function wechatpayNotify() {
  111. Log::debug('wechatpay callback');
  112. // Log::debug(Request::all());
  113. $config = Config::get('laravel-omnipay.gateways.wechatpay');
  114. $gateway = Omnipay::create("WechatPay");
  115. // $gateway->setEnvironment('sandbox');
  116. $gateway->setAppId($config['options']['appid']);
  117. $gateway->setMchId($config['options']['merchant_id']);
  118. $gateway->setApiKey($config['options']['apikey']);
  119. $response = $gateway->completePurchase([
  120. 'request_params' => file_get_contents('php://input')
  121. ])->send();
  122. try {
  123. if ($response->isPaid()) {
  124. //pay success
  125. // $rawInfo = $response->getRequestData();
  126. // Log::debug($rawInfo);
  127. // Log::info('支付成功');
  128. // $out_trade_no = $rawInfo['out_trade_no'];
  129. // $order = order::where('transaction_id', '=', $out_trade_no)->first();
  130. // if (!$order) {
  131. // Log::error('找不到订单' . $out_trade_no);
  132. // return 'fail';
  133. // }
  134. // $u = User::find($order->user_id);
  135. // if (!$u) {
  136. // Log::error('用户不存在' . $order->user_id . ', ' . $order->user_id);
  137. // return 'success';
  138. // }
  139. // DB::beginTransaction();
  140. // $u->coin += $order->number;
  141. // if ($u->save()) {
  142. // //更新订单状态
  143. // $order->status = order::STATUS_FINISHED;
  144. // $order->save();
  145. // //记日志
  146. // $_types = AccountLog::getAllType();
  147. // $_ops = AccountLog::getAllop();
  148. // $this->logAccount($_types[AccountLog::TYPE_CASH], $u->id, $u->name,$order->amount,
  149. // $_ops[AccountLog::OP_CHARGE],
  150. // $_types[AccountLog::TYPE_COIN], $u->id,$u->name,$order->amount,$out_trade_no,'http://miao.beiyuesi.com/base/img/wechat.png');
  151. // Log::info('支付完成');
  152. // } else {
  153. // DB::rollBack();
  154. // Log::error('保存数据失败');
  155. // }
  156. // DB::commit();
  157. // return 'success';
  158. }else{
  159. //pay fail
  160. Log::error($response->getData());
  161. return 'fail';
  162. }
  163. } catch (\Exception $e) {
  164. Log::error('支付异常' . $e->getMessage());
  165. return 'fail';
  166. }
  167. }
  168. /**
  169. * @api {post} /api/pay/charge 充值
  170. * @apiDescription 充值(向平台充值)
  171. * @apiGroup User
  172. * @apiPermission Passport
  173. * @apiVersion 0.1.0
  174. * @apiParam {int} [goods=1] 充值商品(1:梦想币)
  175. * @apiParam {int} number 充值数量,人民币,以分表示
  176. * @apiParam {int} type 支付类型(1:支付宝,2:微信支付)
  177. * @apiSuccessExample {json} Success-Response:
  178. * HTTP/1.1 200 OK
  179. * {
  180. * "state": true,
  181. * "code": 0,
  182. * "message": "",
  183. * "data": {
  184. * "id": 2,
  185. * "code": "ALIPAY_201610231314145719",
  186. * "transaction_id": "201610231314145719",
  187. * "user_type": 2,
  188. * "user_id": 1,
  189. * "goods_type": 1,
  190. * "price": 1,
  191. * "number": 1,
  192. * "amount": 1,
  193. * "pay_type": 1,
  194. * "status": 0,
  195. * "created_at": "2016-10-23 13:14:14",
  196. * "updated_at": "2016-10-23 13:14:14",
  197. * "orderString": "alipay_sdk=lokielse%2Fomnipay-alipay&app_id=2016091201894867&biz_content=%7B%22subject%22%3A%22%5Cu7532%5Cu8c61%5Cu8054%5Cu5408-%5Cu4f59%5Cu989d%5Cu5145%5Cu503c%22%2C%22out_trade_no%22%3A%22201610231314145719%22%2C%22total_amount%22%3A0.01%2C%22product_code%22%3A1%7D&charset=UTF-8&format=JSON&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fweb%2Fapi%2Fpay%2Falipay%2Fnotify&sign_type=RSA&timestamp=2016-10-23+13%3A14%3A14&version=1.0&sign=oxKM0qGMHLWDlMrXHIiy9%2Fk2BXJq3rC3RKdmcfFwkBJVRXvtG6cBoAYPll6VxJYOMQWeu78Ibfov%2FxIVCuN9yUfzEiokfQrzBoptc94bCQ5k0pNyJcSdgezOUKHB12P5Zmm3Hd6AAbGRDV9UCaLVz0wYkFJPrCyUv1ZfhrM%2BBqc%3D"
  198. * }
  199. * }
  200. * @apiErrorExample {json} Error-Response:
  201. * HTTP/1.1 400 Bad Request
  202. * {
  203. * "state": false,
  204. * "code": 1000,
  205. * "message": "传入参数不正确",
  206. * "data": null or []
  207. * }
  208. * 可能出现的错误代码:
  209. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  210. */
  211. public function charge(Request $request)
  212. {
  213. $goodsTypes = order::getAllGoodsTypes();
  214. $gTypes = array_keys($goodsTypes);
  215. $payTypes = order::getAllPayTypes();
  216. $pTypes = array_keys($payTypes);
  217. $validator = Validator::make($data = $request->all(),
  218. [
  219. 'number' => 'required|integer|min:1',
  220. 'type' => 'required|in:' . join(',', $pTypes),
  221. 'goods' => 'in:' . join(',', $gTypes),
  222. ],
  223. [
  224. 'number.required' => '请输入梦想币数量',
  225. 'number.integer' => '梦想币数量必须为整数',
  226. 'number.min' => '充值梦想币数量至少为1',
  227. 'type.required' => '支付类型必填',
  228. 'type.in' => '支付类型非法',
  229. 'goods.in' => '充值商品非法',
  230. ]
  231. );
  232. if ($validator->fails()) {
  233. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  234. }
  235. $user = Auth::guard('api')->user();
  236. $data['goods_id'] = 1;
  237. if (!isset($data['goods'])) {//默认充余额
  238. $data['goods'] = order::GOODS_TYPE_COIN;
  239. }
  240. $data['user_id'] = $user->id;
  241. if ($data['type'] == order::PAY_TYPE_ALIPAY) {
  242. $result = $this->createAlipayCharge($data);
  243. } else {
  244. $result = $this->createWechatpayCharge($data);
  245. }
  246. if ($result === false) {
  247. return $this->error(ErrorCode::ORDER_GENERATE_FAILED);
  248. }
  249. //log
  250. // $data['amount'] = $data['number'];
  251. // $this->chargeLog('Merchant', $merchant->id, $merchant->name,
  252. // 'balance', $data['amount'], 'balance', $data['amount'],
  253. // 'Merchant', $user->id, $merchant->name);
  254. return $this->api($result);
  255. }
  256. /**
  257. * @api {post} /api/pay/order_status 订单状态查询
  258. * @apiDescription 订单状态查询
  259. * @apiGroup User
  260. * @apiPermission Passport
  261. * @apiVersion 0.1.0
  262. * @apiParam {int} [goods=1] 充值商品(1:梦想币)
  263. * @apiParam {int} number 充值数量,人民币,以分表示
  264. * @apiParam {int} type 支付类型(1:支付宝,2:微信支付)
  265. * @apiSuccessExample {json} Success-Response:
  266. * HTTP/1.1 200 OK
  267. * {
  268. * "state": true,
  269. * "code": 0,
  270. * "message": "",
  271. * "data": {
  272. * "id": 2,
  273. * "code": "ALIPAY_201610231314145719",
  274. * "transaction_id": "201610231314145719",
  275. * "user_type": 2,
  276. * "user_id": 1,
  277. * "goods_type": 1,
  278. * "price": 1,
  279. * "number": 1,
  280. * "amount": 1,
  281. * "pay_type": 1,
  282. * "status": 0,
  283. * "created_at": "2016-10-23 13:14:14",
  284. * "updated_at": "2016-10-23 13:14:14",
  285. * "orderString": "alipay_sdk=lokielse%2Fomnipay-alipay&app_id=2016091201894867&biz_content=%7B%22subject%22%3A%22%5Cu7532%5Cu8c61%5Cu8054%5Cu5408-%5Cu4f59%5Cu989d%5Cu5145%5Cu503c%22%2C%22out_trade_no%22%3A%22201610231314145719%22%2C%22total_amount%22%3A0.01%2C%22product_code%22%3A1%7D&charset=UTF-8&format=JSON&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fweb%2Fapi%2Fpay%2Falipay%2Fnotify&sign_type=RSA&timestamp=2016-10-23+13%3A14%3A14&version=1.0&sign=oxKM0qGMHLWDlMrXHIiy9%2Fk2BXJq3rC3RKdmcfFwkBJVRXvtG6cBoAYPll6VxJYOMQWeu78Ibfov%2FxIVCuN9yUfzEiokfQrzBoptc94bCQ5k0pNyJcSdgezOUKHB12P5Zmm3Hd6AAbGRDV9UCaLVz0wYkFJPrCyUv1ZfhrM%2BBqc%3D"
  286. * }
  287. * }
  288. * @apiErrorExample {json} Error-Response:
  289. * HTTP/1.1 400 Bad Request
  290. * {
  291. * "state": false,
  292. * "code": 1000,
  293. * "message": "传入参数不正确",
  294. * "data": null or []
  295. * }
  296. * 可能出现的错误代码:
  297. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  298. */
  299. public function orderStatus(Request $request)
  300. {
  301. $validator = Validator::make($data = $request->all(),
  302. [
  303. 'number' => 'required|integer|min:1',
  304. 'type' => 'required|in:' . join(',', $pTypes),
  305. 'goods' => 'in:' . join(',', $gTypes),
  306. ],
  307. [
  308. 'number.required' => '请输入梦想币数量',
  309. 'number.integer' => '梦想币数量必须为整数',
  310. 'number.min' => '充值梦想币数量至少为1',
  311. 'type.required' => '支付类型必填',
  312. 'type.in' => '支付类型非法',
  313. 'goods.in' => '充值商品非法',
  314. ]
  315. );
  316. if ($validator->fails()) {
  317. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  318. }
  319. $order = new order();
  320. $order->code = $data['code'];
  321. $order->transaction_id = $data['transaction_id'];
  322. $order->user_id = $data['user_id'];
  323. $order->goods_id = $data['goods_id'];
  324. $order->price = $data['price'];
  325. $order->number = $data['number'];
  326. $order->amount = $data['amount'];
  327. $order->pay_type = $data['pay_type'];
  328. if (isset($data['ext_info'])) {
  329. $order->ext_info = $data['ext_info'];
  330. }
  331. if ($order->save()) {
  332. return $order->find($order->id);
  333. }
  334. return $this->api('');
  335. }
  336. public function orderIos(Request $request)
  337. {
  338. $validator = Validator::make($data = $request->all(),
  339. [
  340. 'number' => 'required',
  341. ],
  342. [
  343. 'number.required' => '请输入产品ID',
  344. ]
  345. );
  346. if ($validator->fails()) {
  347. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  348. }
  349. // $order = new order();
  350. Log::info($data['number']);
  351. $AppleProducts = order::getAllAppleProducts();
  352. $coin = 0 ;
  353. if(isset($AppleProducts[$data['number']])){
  354. $coin = $AppleProducts[$data['number']];
  355. }
  356. // $order->code = $data['code'];
  357. // $order->transaction_id = $data['transaction_id'];
  358. // $order->user_id = $data['user_id'];
  359. // $order->goods_id = $data['goods_id'];
  360. // $order->price = $data['price'];
  361. // $order->number = $data['number'];
  362. // $order->amount = $data['amount'];
  363. // $order->pay_type = $data['pay_type'];
  364. //更新订单状态
  365. // $order->status = order::STATUS_FINISHED;
  366. // $order->save();
  367. $u = Auth::guard('api')->user();
  368. $u->coin += $coin;
  369. $u->save();
  370. //记日志
  371. $_types = AccountLog::getAllType();
  372. $_ops = AccountLog::getAllop();
  373. $this->logAccount($_types[AccountLog::TYPE_CASH], $u->id, $u->name,$coin,
  374. $_ops[AccountLog::OP_CHARGE],
  375. $_types[AccountLog::TYPE_COIN], $u->id,$u->name,$coin,$data['number'],'http://miao.beiyuesi.com/base/img/apple.png');
  376. Log::info('支付完成');
  377. return $this->api('');
  378. }
  379. }