JoinPayService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lts
  5. * Date: 2019-06-29
  6. * Time: 17:16
  7. */
  8. namespace app\main\service;
  9. use app\main\constants\ApiConstants;
  10. use GuzzleHttp\Client as Http;
  11. use GuzzleHttp\Exception\GuzzleException;
  12. use think\Exception;
  13. class JoinPayService
  14. {
  15. function _construct()
  16. {
  17. }
  18. protected static $self = null;
  19. public static function instance()
  20. {
  21. if (self::$self == null) {
  22. self::$self = new self();
  23. }
  24. return self::$self;
  25. }
  26. /**
  27. * 汇聚调用API接口
  28. * @param $data 加密前的明文参数
  29. * @param $payInfo 支付信息
  30. * @param $goodsInfo 商品信息
  31. * @param $orderInfo 订单信息
  32. * @param int $loopIndex 循环调用API的次数
  33. * @return mixed|\Psr\Http\Message\ResponseInterface
  34. * @throws Exception
  35. */
  36. public function payJoinApi($data, $payInfo, $goodsInfo, $orderInfo, $loopIndex = 1)
  37. {
  38. $payUrl = ApiConstants::JOIN_PAY_URL;
  39. $joinApiStartTime = microtime(true);
  40. try {
  41. $client = new Http([
  42. 'connect_timeout' => 10,
  43. 'timeout' => 30,
  44. 'http_errors' => true, //抛出异常 true是 false否
  45. 'verify' => false, //不验证ssl证书
  46. ]);
  47. $result = $client->post($payUrl, ['form_params' => $data]);
  48. $joinApiEndTime = microtime(true);
  49. $apiRunTime = round($joinApiEndTime - $joinApiStartTime, 3);
  50. if ($result->getStatusCode() != '200') {
  51. 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',
  52. $payInfo['id'], $payInfo['name'], $payInfo['quartet_merchant_id'], $orderInfo['admin_id'],
  53. $orderInfo['user_id'], $goodsInfo['money'], $orderInfo['goods_id'], $orderInfo['out_trade_no'],
  54. $apiRunTime, $loopIndex, json_encode(var_export($result, true), JSON_UNESCAPED_UNICODE)));
  55. LogService::error('汇聚平台订单创建失败!订单内容: ' . json_encode(var_export($data, true),
  56. JSON_UNESCAPED_UNICODE) . " 汇聚平台返回内容: " . json_encode(var_export($result, true),
  57. JSON_UNESCAPED_UNICODE));
  58. throw new Exception('订单创建失败,请重新下单!');
  59. } else {
  60. $result = json_decode($result->getBody()->getContents(), true);
  61. if ($result['ra_Code'] != '100') {
  62. 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',
  63. $payInfo['id'], $payInfo['name'], $payInfo['quartet_merchant_id'], $orderInfo['admin_id'],
  64. $orderInfo['user_id'], $goodsInfo['money'], $orderInfo['goods_id'], $orderInfo['out_trade_no'],
  65. $apiRunTime, $loopIndex, $result['rb_CodeMsg']));
  66. LogService::error('汇聚平台订单创建失败!订单内容:' . json_encode(var_export($data,
  67. true)) . " 汇聚平台返回错误码: " . $result['ra_Code']);
  68. throw new Exception('订单创建失败,请重新下单!');
  69. }
  70. }
  71. 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',
  72. $payInfo['id'], $payInfo['name'], $payInfo['quartet_merchant_id'], $orderInfo['admin_id'],
  73. $orderInfo['user_id'], $goodsInfo['money'], $orderInfo['goods_id'], $orderInfo['out_trade_no'],
  74. $apiRunTime, $loopIndex));
  75. return $result;
  76. } catch (GuzzleException $exception) {
  77. $joinApiEndTime = microtime(true);
  78. $apiRunTime = round($joinApiEndTime - $joinApiStartTime, 3);
  79. 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',
  80. $payInfo['id'], $payInfo['name'], $payInfo['quartet_merchant_id'], $orderInfo['admin_id'],
  81. $orderInfo['user_id'], $goodsInfo['money'], $orderInfo['goods_id'], $orderInfo['out_trade_no'],
  82. $apiRunTime, $loopIndex, $exception->getMessage()));
  83. LogService::error('汇聚平台订单创建失败!订单内容: ' . json_encode(var_export($data, true), JSON_UNESCAPED_UNICODE));
  84. LogService::error($exception->getMessage());
  85. LogService::error($exception->getTraceAsString());
  86. if ($loopIndex <= ApiConstants::LOOP_EXEC_API_COUNT) {
  87. $loopIndex++;
  88. return $this->payJoinApi($data, $payInfo, $goodsInfo, $orderInfo, $loopIndex);
  89. }
  90. throw new Exception('订单创建失败,请重新下单!');
  91. }
  92. }
  93. }