WxpayController.php 2.5 KB

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