WxpayController.php 3.2 KB

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