123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace app\api\controller\pay;
- use app\api\controller\Log;
- use app\model\Order;
- use app\model\PayDetail;
- use Payment\Common\PayException;
- use support\Db;
- use support\Request;
- use Yansongda\Pay\Exceptions\BusinessException;
- use Yansongda\Pay\Log as PayLog;
- use Yansongda\Pay\Pay;
- class AlipayController
- {
- public function index(Request $request)
- {
- $params = $request->all();
- \support\Log::info("开始获取支付宝支付参数", $params);
- if (!isset($params['order_id'])) {
- return json_fail('参数异常');
- }
- if (!isset($params['platform'])) {
- return json_fail('缺少平台参数');
- }
- $orderId = $params['order_id'];
- $payDetail = PayDetail::where('join_pay_order_id', $orderId)->where('pay_status', 'WAITING')->first();
- if (!$payDetail) {
- return json_fail('订单异常');
- }
- if ($payDetail->pay_category == 'MEALS') {
- if (!empty($params['pay_amount']) && $params['pay_amount'] != $payDetail->pay_amount) {
- return json_fail("支付金额与订单金额不符");
- }
- $payObjectJson = [];
- if (!empty($payDetail->join_pay_object_json)) {
- $payObjectJson = json_decode($payDetail->join_pay_object_json, true);
- }
- // 标记发生过支付请求
- $payObjectJson['pay_marking'] = 'Y';
- $payDetail->join_pay_object_json = json_encode($payObjectJson);
- }
- $payData = [
- 'out_trade_no' => $orderId,
- 'subject' => '万悦康养订单',
- 'total_amount' => $payDetail->pay_amount,
- ];
- Db::beginTransaction();
- try {
- $payDetail->pay_prepayid = 'ALIPAY';
- $payDetail->save();
- if ($params['platform'] == 'android') { // APP支付
- $result = Pay::alipay(config('payment.alipay'))->app($payData)->getContent();
- } elseif ($params['platform'] == 'WeChat') { // 网页支付
- $result = Pay::alipay(config('payment.alipay'))->wap($payData)->getContent();
- } else {
- throw new \support\exception\BusinessException("平台参数无效");
- }
- Db::commit();
- \support\Log::info("支付宝支付参数获取成功", ['result' => $result]);
- return json_success('', $result);
- } catch (\support\exception\BusinessException $e) {
- Db::rollBack();
- \support\Log::error("支付宝支付参数获取失败", ['msg' => $e->getMessage()]);
- return json_fail($e->getMessage());
- } catch (PayException $e) {
- Db::rollBack();
- \support\Log::error("支付宝支付参数获取失败", ['msg' => $e->getMessage()]);
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- \support\Log::error("支付宝支付参数获取失败", ['msg' => $e->getMessage()]);
- return json_fail('下单失败:' . $e->getMessage());
- }
- }
- public function return()
- {
- $data = Pay::alipay($this->config)->verify(); // 是的,验签就这么简单!
- // 订单号:$data->out_trade_no
- // 支付宝交易号:$data->trade_no
- // 订单总金额:$data->total_amount
- }
- public function notify()
- {
- $alipay = Pay::alipay($this->config);
- try {
- $data = $alipay->verify(); // 是的,验签就这么简单!
- // 请自行对 trade_status 进行判断及其它逻辑进行判断,在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时,支付宝才会认定为买家付款成功。
- // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号;
- // 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额);
- // 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email);
- // 4、验证app_id是否为该商户本身。
- // 5、其它业务逻辑情况
- PayLog::debug('Alipay notify', $data->all());
- } catch (\Exception $e) {
- // $e->getMessage();
- }
- return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
- }
- private function getConfig()
- {
- return [
- 'app_id' => '2021004155613207',
- 'notify_url' => 'https://api.wanyuewellness.com.cn/api.pay.notify.php',
- 'return_url' => 'https://api.wanyuewellness.com.cn/api.pay.notify.php',
- 'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmlAp3QIQuDVJqQP5C+33LO6P5D9OOGXszClHNH/5Iep+/A+00vhLaSc9izgxV8XTWpqHcss/gGYfmjNtpD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FvmfafJy1G55feFK9k+YmX7UXtcbw/bc59CqjIL/bpkYy1t+0SrB0eywGvMFBD3G3EZQnQmFmHW7bAba3pTcEoVlyhm9Oz9igvbpI1fZmaeqE+9DOVlWwtcLjhpBh/wuHP5KgyfWKBqwrBQKBgQCyb8PgKklTNFS7iHKBKTIx/ES4xZBSej/Q/lLY0ZGc2LTTGn2HxmmbcJIH5fR4+opOGSGsXc/vfp2pGk1yq42rRVVjUG7Y68J2VpIjELQ8SNbSK+4YqS8VI+Kez0m7ZDEWTwxMRbQTiCaBRUwGhjkReLyLrfXmz5Syxaf7rBg/LwKBgQDE4eQCDI2jFSf1B9ZnGyT9DuyUcLHjEUN2cB9EiH09VdnvMhoYwkBgB44WPuu1JEjOrY8xnRsMlz52LX3i+K7chvwuCbgNIBrrFwNbNo2hQlLl2o2lFoBfcuKF6h0yRcfmgjEQPt0Kyo6VEP4/mkTo5oB8Rb5Q1t5lE3XC+9AcqQKBgEJDENkguuQEDecqL2NZegNn69k1vHd0YFhq6TcM8+br7pJqRu1xt+v6LLB0K4n1eACygnewyrPkMAYI93O60zwj3iaoStGRE0DAuzn8TQIKBoQ7vgo4YXyXO+O/lGDbPwNKzYVRaSLLr4jXw/i0RWMAk/vG2QciVNTiY5Qo/0QPAoGANqq2RacHmcevwp3S3iXfHGxb8kfG90aGDtTr9AY965IGmy4ckKEZJ5+XkhH8Yi+YYWyIkTpgsSQUCDoBSvQ7FBSFnYn5CcX8MIpsx833DpGEK4PMeje0q/rbQ4P1/gOgvgICGWJsOYkzooFhX+TVGXCXE4EE3E471GadQOhy3EQ=',
- 'log' => [ // optional
- 'file' => runtime_path('logs/alipay.log'),
- 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
- 'type' => 'single', // optional, 可选 daily.
- 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
- ],
- 'http' => [ // optional
- 'timeout' => 5.0,
- 'connect_timeout' => 5.0,
- // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
- ],
- 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
- ];
- }
- }
|