WxpayController.php 2.0 KB

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