WxpayController.php 2.6 KB

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