WxpayController.php 2.5 KB

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