AlipayController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\api\controller\pay;
  3. use app\api\controller\Log;
  4. use app\model\Order;
  5. use app\model\PayDetail;
  6. use Payment\Common\PayException;
  7. use support\Db;
  8. use support\Request;
  9. use Yansongda\Pay\Exceptions\BusinessException;
  10. use Yansongda\Pay\Log as PayLog;
  11. use Yansongda\Pay\Pay;
  12. class AlipayController
  13. {
  14. public function index(Request $request)
  15. {
  16. $params = $request->all();
  17. \support\Log::info("开始获取支付宝支付参数", $params);
  18. if (!isset($params['order_id'])) {
  19. return json_fail('参数异常');
  20. }
  21. if (!isset($params['platform'])) {
  22. return json_fail('缺少平台参数');
  23. }
  24. $orderId = $params['order_id'];
  25. $payDetail = PayDetail::where('join_pay_order_id', $orderId)->where('pay_status', 'WAITING')->first();
  26. if (!$payDetail) {
  27. return json_fail('订单异常');
  28. }
  29. if ($payDetail->pay_category == 'MEALS') {
  30. if (!empty($params['pay_amount']) && $params['pay_amount'] != $payDetail->pay_amount) {
  31. return json_fail("支付金额与订单金额不符");
  32. }
  33. $payObjectJson = [];
  34. if (!empty($payDetail->join_pay_object_json)) {
  35. $payObjectJson = json_decode($payDetail->join_pay_object_json, true);
  36. }
  37. // 标记发生过支付请求
  38. $payObjectJson['pay_marking'] = 'Y';
  39. $payDetail->join_pay_object_json = json_encode($payObjectJson);
  40. }
  41. $payData = [
  42. 'out_trade_no' => $orderId,
  43. 'subject' => '万悦康养订单',
  44. 'total_amount' => $payDetail->pay_amount,
  45. ];
  46. Db::beginTransaction();
  47. try {
  48. $payDetail->pay_prepayid = 'ALIPAY';
  49. $payDetail->save();
  50. if ($params['platform'] == 'android') { // APP支付
  51. $result = Pay::alipay(config('payment.alipay'))->app($payData)->getContent();
  52. } elseif ($params['platform'] == 'WeChat') { // 网页支付
  53. $result = Pay::alipay(config('payment.alipay'))->wap($payData)->getContent();
  54. } else {
  55. throw new \support\exception\BusinessException("平台参数无效");
  56. }
  57. Db::commit();
  58. \support\Log::info("支付宝支付参数获取成功", ['result' => $result]);
  59. return json_success('', $result);
  60. } catch (\support\exception\BusinessException $e) {
  61. Db::rollBack();
  62. \support\Log::error("支付宝支付参数获取失败", ['msg' => $e->getMessage()]);
  63. return json_fail($e->getMessage());
  64. } catch (PayException $e) {
  65. Db::rollBack();
  66. \support\Log::error("支付宝支付参数获取失败", ['msg' => $e->getMessage()]);
  67. return json_fail($e->getMessage());
  68. } catch (\Exception $e) {
  69. Db::rollBack();
  70. \support\Log::error("支付宝支付参数获取失败", ['msg' => $e->getMessage()]);
  71. return json_fail('下单失败:' . $e->getMessage());
  72. }
  73. }
  74. public function return()
  75. {
  76. $data = Pay::alipay($this->config)->verify(); // 是的,验签就这么简单!
  77. // 订单号:$data->out_trade_no
  78. // 支付宝交易号:$data->trade_no
  79. // 订单总金额:$data->total_amount
  80. }
  81. public function notify()
  82. {
  83. $alipay = Pay::alipay($this->config);
  84. try {
  85. $data = $alipay->verify(); // 是的,验签就这么简单!
  86. // 请自行对 trade_status 进行判断及其它逻辑进行判断,在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时,支付宝才会认定为买家付款成功。
  87. // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号;
  88. // 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额);
  89. // 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email);
  90. // 4、验证app_id是否为该商户本身。
  91. // 5、其它业务逻辑情况
  92. PayLog::debug('Alipay notify', $data->all());
  93. } catch (\Exception $e) {
  94. // $e->getMessage();
  95. }
  96. return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
  97. }
  98. private function getConfig()
  99. {
  100. return [
  101. 'app_id' => '2021004155613207',
  102. 'notify_url' => 'https://api.wanyuewellness.com.cn/api.pay.notify.php',
  103. 'return_url' => 'https://api.wanyuewellness.com.cn/api.pay.notify.php',
  104. 'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmlAp3QIQuDVJqQP5C+33LO6P5D9OOGXszClHNH/5Iep+/A+00vhLaSc9izgxV8XTWpqHcss/gGYfmjNtpDwF0RS1po6J7dTg8d6UJtqaNGCEMUc/r/+Jo+zrmSeoFRdwb5a+tIPC2xb58qrRuL4wYpvzSAwr9DNveKO30RJT3cXX98sYMs6gWhXOz5jBGFUlYQZ5WB1Nx8HM39spW7/QNINX0y4Wqqp0reWieMRE1aKrV9Fb4ehrJ3nDm64KHWfRw/g3Me9FvqkP03dkfPT4hykeHfWTUbuh+ifTjGMcVLxBnBX74vnNzk9zwn9l+UUuEg/1RbMcEF5ppMnt/Kog6wIDAQAB',
  105. // 加密方式: **RSA2**
  106. 'private_key' => 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaUCndAhC4NUmpA/kL7fcs7o/kP044ZezMKUc0f/kh6n78D7TS+EtpJz2LODFXxdNamodyyz+AZh+aM22kPAXRFLWmjont1ODx3pQm2po0YIQxRz+v/4mj7OuZJ6gVF3Bvlr60g8LbFvnyqtG4vjBim/NIDCv0M294o7fRElPdxdf3yxgyzqBaFc7PmMEYVSVhBnlYHU3Hwczf2ylbv9A0g1fTLhaqqnSt5aJ4xETVoqtX0Vvh6GsnecObrgodZ9HD+Dcx70W+qQ/Td2R89PiHKR4d9ZNRu6H6J9OMYxxUvEGcFfvi+c3OT3PCf2X5RS4SD/VFsxwQXmmkye38qiDrAgMBAAECggEAE3EgJSE6fAryseG3QgKETtvrsyUqtL5avEpDF2E8QUKrJxTPgnkX/zKbdWvqA5YAD/1CLXtgD8xOFP5/xN5vGS2TyV1SEW3tvWt+nPqTzR+P6CmC46i9bbLO6Cfv7GBcoSKL+nBT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E4EE3E471GadQOhy3EQ=',
  107. 'log' => [ // optional
  108. 'file' => runtime_path('logs/alipay.log'),
  109. 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  110. 'type' => 'single', // optional, 可选 daily.
  111. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  112. ],
  113. 'http' => [ // optional
  114. 'timeout' => 5.0,
  115. 'connect_timeout' => 5.0,
  116. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  117. ],
  118. 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
  119. ];
  120. }
  121. }