From 6f3c6bcd8dbc5290044bf6a107a604508a4803dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=B8=80=E5=B3=B0?= <1feng.0595@gmail.com> Date: Mon, 21 Aug 2017 19:14:45 +0800 Subject: [PATCH] payment --- app/Common/Payment.php | 78 +++++++++++++++++++ .../Api/PaymentNotifyController.php | 71 +++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 app/Common/Payment.php create mode 100644 app/Http/Controllers/Api/PaymentNotifyController.php diff --git a/app/Common/Payment.php b/app/Common/Payment.php new file mode 100644 index 0000000..3edc1ea --- /dev/null +++ b/app/Common/Payment.php @@ -0,0 +1,78 @@ +payment; + + //2.创建订单 + $attributes = array( + 'trade_type' => 'APP', // JSAPI,NATIVE,APP... + 'body' => '订单支付', + 'detail' => '订单支付', + 'out_trade_no' => 'fghfdgfg789', + 'total_fee' => '123.00', // 单位:分 + 'notify_url' => 'http://www.baidu.com/aaa.php', // 支付结果通知网址,如果不设置则会使用配置里的默认地址 + //'openid' => '当前用户的 openid', // trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识, + // ... + ); + + $order = new \EasyWeChat\Payment\Order(array_merge($attributes, $attributes2)); + + //3.统一下单 + $result = $payment->prepare($order); + + if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') + { + $prepayId = $result->prepay_id; + $res = $payment->configForAppPayment($prepayId); + + return $res; + } + + return false; + } + + /** + * 支付宝APP支付 + * bizcontent + * 支付宝APP支付 + */ + public function AlipayApp($bizcontent,$notify_url,$config = config('alipay.app_alipay')) + { + require_once base_path('resources/org/alipay_app').'/AopClient.php'; + require_once base_path('resources/org/alipay_app').'/AlipayTradeAppPayRequest.php'; + + $aop = new \AopClient; + $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do"; + $aop->appId = $config['appId']; + $aop->rsaPrivateKey = $config['rsaPrivateKey']; + $aop->format = "json"; + $aop->charset = "UTF-8"; + $aop->signType = "RSA2"; + $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey']; + //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay + $request = new \AlipayTradeAppPayRequest(); + //SDK已经封装掉了公共参数,这里只需要传入业务参数 + /* $bizcontent = "{\"body\":\"订单支付\"," + . "\"subject\": \"订单支付\"," + . "\"out_trade_no\": \""."jklkjlkj54654165"."\"," + . "\"total_amount\": \""."123.00"."\"," + . "\"timeout_express\": \"30m\"," + . "\"product_code\":\"QUICK_MSECURITY_PAY\"" + . "}"; */ + $request->setNotifyUrl($notify_url); + $request->setBizContent($bizcontent); + //这里和普通的接口调用不同,使用的是sdkExecute + $response = $aop->sdkExecute($request); + //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题 + return $response;//就是orderString 可以直接给客户端请求,无需再做处理。 + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Api/PaymentNotifyController.php b/app/Http/Controllers/Api/PaymentNotifyController.php new file mode 100644 index 0000000..f81c711 --- /dev/null +++ b/app/Http/Controllers/Api/PaymentNotifyController.php @@ -0,0 +1,71 @@ +payment->handleNotify(function($notify, $successful) + { + // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单 + $order = 查询订单($notify->out_trade_no); + if (!$order) { // 如果订单不存在 + return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了 + } + + // 如果订单存在 + // 检查订单是否已经更新过支付状态 + if ($order->paid_at) // 假设订单字段“支付时间”不为空代表已经支付 + { + return true; // 已经支付成功了就不再更新了 + } + + // 用户是否支付成功 + if ($successful) + { + // 不是已经支付状态则修改为已经支付状态 + $order->paid_at = time(); // 更新支付时间为当前时间 + $order->status = 'paid'; + } + else + { + // 用户支付失败 + $order->status = 'paid_fail'; + } + + $order->save(); // 保存订单 + + return true; // 返回处理完成 + }); + + return $response; + } + + public function AlipayAppNotify($config('alipay.app_alipay')) + { + require_once base_path('resources/org/alipay_app').'/AopClient.php'; + $aop = new \AopClient; + $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey']; + $flag = $aop->rsaCheckV1($_REQUEST, NULL, "RSA2"); + + //支付成功 + if($flag) + { + $out_trade_no = $_REQUEST['out_trade_no']; + } + } +} \ No newline at end of file