123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace app\api\controller\pay;
- 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\Pay;
- class WxpayController
- {
- public function index(Request $request)
- {
- $params = $request->all();
- 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('订单异常');
- }
- $payData = [
- 'out_trade_no' => $orderId,
- 'body' => '万悦康养订单',
- 'total_fee' => $payDetail->pay_amount * 100,
- ];
- Db::beginTransaction();
- try {
- $payDetail->pay_prepayid = 'WXPAY';
- $payDetail->save();
- if ($params['platform'] == 'android'){
- $wxpay = Pay::wechat(config('payment.wxpay'))->app($payData)->getContent();
- }elseif ($params['platform'] == 'WeChat'){
- if (empty($params['openid'])){
- Db::rollBack();
- return json_fail("缺少OpenID 参数");
- }
- $payData['openid'] = $params['openid'];
- $config = config('payment.wxpay');
- $wxpay = Pay::wechat($config)->mp($payData);
- }elseif ($params['platform'] == 'mp-weixin'){
- if (empty($params['openid'])){
- Db::rollBack();
- return json_fail("缺少OpenID 参数");
- }
- $payData['openid'] = $params['openid'];
- $wxpay = Pay::wechat(config('payment.wxpay'))->miniapp($payData);
- }else{
- throw new PayException("平台参数无效");
- }
- Db::commit();
- return json_success('', json_decode($wxpay, true));
- } catch (\support\exception\BusinessException $e) {
- Db::rollBack();
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getMessage());
- return json_fail('下单失败:'.$e->getMessage());
- }
- }
- private function getConfig()
- {
- return [
- 'appid' => 'wx089c26eaf96c3d51',
- 'mch_id' => '1680393367',
- 'key' => 'c451cedbab8058e3502a35c6dacf0919',
- 'notify_url' => 'https://api.wanyuewellness.com.cn/api.pay.notify.php',
- 'cert_client' => config_path('cert/wxpay/apiclient_cert.pem'),
- 'cert_key' => config_path('cert/wxpay/apiclient_key.pem'),
- 'log' => [
- 'file' => runtime_path('logs/wechat.log'),
- 'level' => 'info',
- 'type' => 'single',
- 'max_file' => 30,
- ],
- 'http' => [
- 'timeout' => 5.0,
- 'connect_timeout' => 5.0,
-
- ],
- ];
- }
- }
|