123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lts
- * Date: 2019-06-29
- * Time: 17:16
- */
- namespace app\main\service;
- use app\main\constants\ApiConstants;
- use GuzzleHttp\Client as Http;
- use GuzzleHttp\Exception\GuzzleException;
- use think\Exception;
- class JoinPayService
- {
- function _construct()
- {
- }
- protected static $self = null;
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * 汇聚调用API接口
- * @param $data 加密前的明文参数
- * @param $payInfo 支付信息
- * @param $goodsInfo 商品信息
- * @param $orderInfo 订单信息
- * @param int $loopIndex 循环调用API的次数
- * @return mixed|\Psr\Http\Message\ResponseInterface
- * @throws Exception
- */
- public function payJoinApi($data, $payInfo, $goodsInfo, $orderInfo, $loopIndex = 1)
- {
- $payUrl = ApiConstants::JOIN_PAY_URL;
- $joinApiStartTime = microtime(true);
- try {
- $client = new Http([
- 'connect_timeout' => 10,
- 'timeout' => 30,
- 'http_errors' => true, //抛出异常 true是 false否
- 'verify' => false, //不验证ssl证书
- ]);
- $result = $client->post($payUrl, ['form_params' => $data]);
- $joinApiEndTime = microtime(true);
- $apiRunTime = round($joinApiEndTime - $joinApiStartTime, 3);
- if ($result->getStatusCode() != '200') {
- LogService::error(sprintf('joinpay_create_order_fail!wxpay_id:%s,wxpay_name:%s,mch_id:%s,channel_id:%s,user_id:%s,money:%s,good_id:%s,out_trade_no:%s,api_run_time:%s s,loop_index:%s,errmessage:%s',
- $payInfo['id'], $payInfo['name'], $payInfo['quartet_merchant_id'], $orderInfo['admin_id'],
- $orderInfo['user_id'], $goodsInfo['money'], $orderInfo['goods_id'], $orderInfo['out_trade_no'],
- $apiRunTime, $loopIndex, json_encode(var_export($result, true), JSON_UNESCAPED_UNICODE)));
- LogService::error('汇聚平台订单创建失败!订单内容: ' . json_encode(var_export($data, true),
- JSON_UNESCAPED_UNICODE) . " 汇聚平台返回内容: " . json_encode(var_export($result, true),
- JSON_UNESCAPED_UNICODE));
- throw new Exception('订单创建失败,请重新下单!');
- } else {
- $result = json_decode($result->getBody()->getContents(), true);
- if ($result['ra_Code'] != '100') {
- LogService::error(sprintf('joinpay_create_order_fail!wxpay_id:%s,wxpay_name:%s,mch_id:%s,channel_id:%s,user_id:%s,money:%s,good_id:%s,out_trade_no:%s,api_run_time:%s s,loop_index:%s,errmessage:%s',
- $payInfo['id'], $payInfo['name'], $payInfo['quartet_merchant_id'], $orderInfo['admin_id'],
- $orderInfo['user_id'], $goodsInfo['money'], $orderInfo['goods_id'], $orderInfo['out_trade_no'],
- $apiRunTime, $loopIndex, $result['rb_CodeMsg']));
- LogService::error('汇聚平台订单创建失败!订单内容:' . json_encode(var_export($data,
- true)) . " 汇聚平台返回错误码: " . $result['ra_Code']);
- throw new Exception('订单创建失败,请重新下单!');
- }
- }
- LogService::info(sprintf('joinpay_create_order_success!wxpay_id:%s,wxpay_name:%s,mch_id:%s,channel_id:%s,user_id:%s,money:%s,good_id:%s,out_trade_no:%s,api_run_time:%s s,loop_index:%s',
- $payInfo['id'], $payInfo['name'], $payInfo['quartet_merchant_id'], $orderInfo['admin_id'],
- $orderInfo['user_id'], $goodsInfo['money'], $orderInfo['goods_id'], $orderInfo['out_trade_no'],
- $apiRunTime, $loopIndex));
- return $result;
- } catch (GuzzleException $exception) {
- $joinApiEndTime = microtime(true);
- $apiRunTime = round($joinApiEndTime - $joinApiStartTime, 3);
- LogService::error(sprintf('joinpay_create_order_fail!wxpay_id:%s,wxpay_name:%s,mch_id:%s,channel_id:%s,user_id:%s,money:%s,good_id:%s,out_trade_no:%s,api_run_time:%s s,loop_index:%s,errmessage:%s',
- $payInfo['id'], $payInfo['name'], $payInfo['quartet_merchant_id'], $orderInfo['admin_id'],
- $orderInfo['user_id'], $goodsInfo['money'], $orderInfo['goods_id'], $orderInfo['out_trade_no'],
- $apiRunTime, $loopIndex, $exception->getMessage()));
- LogService::error('汇聚平台订单创建失败!订单内容: ' . json_encode(var_export($data, true), JSON_UNESCAPED_UNICODE));
- LogService::error($exception->getMessage());
- LogService::error($exception->getTraceAsString());
- if ($loopIndex <= ApiConstants::LOOP_EXEC_API_COUNT) {
- $loopIndex++;
- return $this->payJoinApi($data, $payInfo, $goodsInfo, $orderInfo, $loopIndex);
- }
- throw new Exception('订单创建失败,请重新下单!');
- }
- }
- }
|