WxpayController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\api\controller\pay;
  3. use app\model\PayDetail;
  4. use support\Db;
  5. use support\Request;
  6. use Yansongda\Pay\Pay;
  7. class WxpayController
  8. {
  9. public function index(Request $request)
  10. {
  11. $params = $request->all();
  12. \support\Log::info("开始获取微信支付参数",$params);
  13. if (!isset($params['order_id'])) {
  14. return json_fail('参数异常');
  15. }
  16. if (!isset($params['platform'])){
  17. return json_fail('缺少平台参数');
  18. }
  19. $orderId = $params['order_id'];
  20. $payDetail = PayDetail::where('join_pay_order_id',$orderId)->where('pay_status','WAITING')->first();
  21. if (!$payDetail) {
  22. return json_fail('订单异常');
  23. }
  24. if (!empty($params['pay_amount']) && $params['pay_amount'] != $payDetail->pay_amount) {
  25. return json_fail("支付金额与订单金额不符");
  26. }
  27. if ($payDetail->pay_category == 'MEALS'){
  28. $payObjectJson = [];
  29. if (!empty($payDetail->join_pay_object_json)) {
  30. $payObjectJson = json_decode($payDetail->join_pay_object_json, true);
  31. }
  32. // 标记发生过支付请求
  33. $payObjectJson['pay_marking'] = 'Y';
  34. $payDetail->join_pay_object_json = json_encode($payObjectJson);
  35. }
  36. $payData = [
  37. 'out_trade_no' => $orderId,
  38. 'body' => '万悦康养订单',
  39. 'total_fee' => $payDetail->pay_amount * 100,
  40. ];
  41. Db::beginTransaction();
  42. try {
  43. $payDetail->pay_prepayid = 'WXPAY';
  44. $payDetail->save();
  45. if ($params['platform'] == 'android'){ // APP
  46. $wxpay = Pay::wechat(config('payment.wxpay'))->app($payData)->getContent();
  47. }elseif ($params['platform'] == 'WeChat'){ // 公众号
  48. if (empty($params['openid'])){
  49. throw new \support\exception\BusinessException("缺少OpenID 参数");
  50. }
  51. $payData['openid'] = $params['openid'];
  52. $config = config('payment.wxpay');
  53. $wxpay = Pay::wechat($config)->mp($payData);
  54. }elseif ($params['platform'] == 'mp-weixin'){ // 小程序
  55. if (empty($params['openid'])){
  56. throw new \support\exception\BusinessException("缺少OpenID 参数");
  57. }
  58. $payData['openid'] = $params['openid'];
  59. $wxpay = Pay::wechat(config('payment.wxpay'))->miniapp($payData);
  60. }else{
  61. throw new \support\exception\BusinessException("平台参数无效");
  62. }
  63. Db::commit();
  64. \support\Log::info("微信支付参数获取成功",['msg'=>$wxpay]);
  65. return json_success('', json_decode($wxpay, true));
  66. } catch (\support\exception\BusinessException $e) {
  67. Db::rollBack();
  68. \support\Log::error("微信支付参数获取失败",['msg'=>$e->getMessage()]);
  69. return json_fail($e->getMessage());
  70. } catch (\Exception $e) {
  71. Db::rollBack();
  72. \support\Log::error("微信支付参数获取失败",['msg'=>$e->getMessage()]);
  73. return json_fail('下单失败:'.$e->getMessage());
  74. }
  75. }
  76. private function getConfig()
  77. {
  78. return [
  79. 'appid' => 'wx089c26eaf96c3d51', // APP APPID
  80. 'mch_id' => '1680393367',
  81. 'key' => 'c451cedbab8058e3502a35c6dacf0919',
  82. 'notify_url' => 'https://api.wanyuewellness.com.cn/api.pay.notify.php',
  83. 'cert_client' => config_path('cert/wxpay/apiclient_cert.pem'), // optional,退款等情况时用到
  84. 'cert_key' => config_path('cert/wxpay/apiclient_key.pem'),// optional,退款等情况时用到
  85. 'log' => [ // optional
  86. 'file' => runtime_path('logs/wechat.log'),
  87. 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  88. 'type' => 'single', // optional, 可选 daily.
  89. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  90. ],
  91. 'http' => [ // optional
  92. 'timeout' => 5.0,
  93. 'connect_timeout' => 5.0,
  94. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  95. ],
  96. // 'mode' => 'dev', // optional, dev/hk;当为 `hk` 时,为中国香港 gateway。
  97. ];
  98. }
  99. }