WxpayController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\api\controller\pay;
  3. use app\model\Order;
  4. use support\Request;
  5. use Yansongda\Pay\Exceptions\BusinessException;
  6. use Yansongda\Pay\Pay;
  7. class WxpayController
  8. {
  9. public function index(Request $request)
  10. {
  11. $params = $request->all();
  12. if (!isset($params['order_id'])) {
  13. return json_fail('参数异常');
  14. }
  15. $orderId = $params['order_id'];
  16. $order = Order::where('order_id', $orderId)->where('order_status_payment', 'PENDING')->first();
  17. if (!$order) {
  18. return json_fail('订单异常');
  19. }
  20. $payData = [
  21. 'out_trade_no' => $orderId,
  22. 'body' => '万悦康养订单',
  23. 'total_fee' => $order->order_amount_pay * 100,
  24. ];
  25. try {
  26. $alipay = Pay::wechat($this->getConfig())->app($payData)->getContent();
  27. return json_success('', json_decode($alipay, true));
  28. } catch (BusinessException $e) {
  29. return json_fail($e->getMessage());
  30. } catch (\Exception $e) {
  31. return json_fail('下单失败');
  32. }
  33. }
  34. public function notify()
  35. {
  36. $pay = Pay::wechat($this->getConfig());
  37. try {
  38. $data = $pay->verify(); // 是的,验签就这么简单!
  39. Log::debug('Wechat notify', $data->all());
  40. } catch (\Exception $e) {
  41. // $e->getMessage();
  42. }
  43. return $pay->success()->send();// laravel 框架中请直接 `return $pay->success()`
  44. }
  45. private function getConfig()
  46. {
  47. return [
  48. 'appid' => 'wx089c26eaf96c3d51', // APP APPID
  49. 'mch_id' => '1680393367',
  50. 'key' => 'c451cedbab8058e3502a35c6dacf0919',
  51. 'notify_url' => 'https://api.wanyuewellness.com.cn/api.pay.notify.php',
  52. 'cert_client' => config_path('cert/wxpay/apiclient_cert.pem'), // optional,退款等情况时用到
  53. 'cert_key' => config_path('cert/wxpay/apiclient_key.pem'),// optional,退款等情况时用到
  54. 'log' => [ // optional
  55. 'file' => runtime_path('logs/wechat.log'),
  56. 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  57. 'type' => 'single', // optional, 可选 daily.
  58. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  59. ],
  60. 'http' => [ // optional
  61. 'timeout' => 5.0,
  62. 'connect_timeout' => 5.0,
  63. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  64. ],
  65. // 'mode' => 'dev', // optional, dev/hk;当为 `hk` 时,为中国香港 gateway。
  66. ];
  67. }
  68. }