WxpayController.php 2.5 KB

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