WxpayController.php 3.7 KB

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