PayHandle.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\common\logic;
  3. use think\Exception;
  4. class PayHandle
  5. {
  6. // 获取支付类型的支付渠道
  7. public function getPayChannel($pay_scene, $game_id, $version)
  8. {
  9. try {
  10. $versionList = model('AndroidSdk')->getAllVersion()->toArray();
  11. $handleVersionList = array_column($versionList, 'id', 'version');
  12. if (empty($handleVersionList[$version])) {
  13. return ['code' => -112, 'msg' => '支付版本有误!', 'data' => []];
  14. }
  15. $where = [
  16. 'sdk_versions' => [['like', '%,' . $handleVersionList[$version] . ',%'], ['eq', 'all'], 'or'],
  17. 'game_ids' => [['like', '%,' . $game_id . ',%'], ['eq', 'all'], 'or'],
  18. 'pay_scene' => $pay_scene,
  19. 'status' => 1,
  20. ];
  21. $paySupconList = (new \app\common\model\PaySupconModel())->getCacheList($where);
  22. $payList = getRandomPaymentType($paySupconList);
  23. $newPayList = [
  24. 'paytype' => $payList['pay_mark'],
  25. 'payname' => $payList['paytype_name'],
  26. 'pay_scene' => $payList['pay_scene'],
  27. 'sort' => $payList['sort'],
  28. ];
  29. return ['code' => 200, 'msg' => 'success', 'data' => $newPayList];
  30. } catch (Exception $e) {
  31. return ['code' => -100, 'msg' => '支付渠道获取异常:' . $e->getMessage(), 'data' => []];
  32. }
  33. }
  34. // 获取支付渠道列表
  35. public function getPayChannelList($game_id, $version)
  36. {
  37. /**
  38. * ## 随机支付
  39. *
  40. * 1. 适配不同的支付方式
  41. * 2. 适配不同的游戏
  42. * 3. 适配不同的版本
  43. *
  44. */
  45. try {
  46. $versionList = model('AndroidSdk')->getAllVersion()->toArray();
  47. $handleVersionList = array_column($versionList, 'id', 'version');
  48. if (empty($handleVersionList[$version])) {
  49. $this->jsonResult('', 0, '当前版本号有误');
  50. }
  51. $where = [
  52. 'sdk_versions' => [['like', '%,' . $handleVersionList[$version] . ',%'], ['eq', 'all'], 'or'],
  53. 'game_ids' => [['like', '%,' . $game_id . ',%'], ['eq', 'all'], 'or'],
  54. ];
  55. $paySupconList = (new \app\common\model\PaySupconModel())->where($where)->field('id,paytype_name,probability,pay_scene,pay_mark,sort')->select()->toArray();
  56. // 支付类型设置权重
  57. $payScene = ['wx_h5', 'wx_app', 'ali_h5', 'ali_app'];
  58. // 初始化新的支付类型数组
  59. $organizedPayChannels = [];
  60. // 遍历支付渠道数组
  61. foreach ($paySupconList as $channel) {
  62. $scene = $channel['pay_scene'];
  63. if (in_array($scene, $payScene)) {
  64. // 如果支付类型数组中不存在该支付场景,则初始化
  65. if (!isset($organizedPayChannels[$scene])) {
  66. $organizedPayChannels[$scene] = [];
  67. }
  68. // 将支付渠道添加到相应的支付场景中
  69. $organizedPayChannels[$scene][] = $channel;
  70. }
  71. }
  72. $payList = [];
  73. if (!empty($organizedPayChannels['wx_h5'])) {
  74. $payList[] = getRandomPaymentType($organizedPayChannels['wx_h5']);
  75. }
  76. if (!empty($organizedPayChannels['ali_h5'])) {
  77. $payList[] = getRandomPaymentType($organizedPayChannels['ali_h5']);
  78. }
  79. if (!empty($organizedPayChannels['ali_app'])) {
  80. $payList[] = getRandomPaymentType($organizedPayChannels['ali_app']);
  81. }
  82. if ($payList) {
  83. usort($payList, function ($a, $b) {
  84. return $a['sort'] <=> $b['sort'];
  85. });
  86. }
  87. $newPayList = [];
  88. foreach ($payList as $key => $value) {
  89. $newPayList[] = [
  90. 'paytype' => $value['pay_mark'],
  91. 'payname' => $value['paytype_name'],
  92. 'pay_scene' => $value['pay_scene'],
  93. 'sort' => $value['sort'],
  94. ];
  95. }
  96. // dump($payList);
  97. // 如果没有查询到配置的游戏,则走默认
  98. if ($newPayList) {
  99. $this->jsonResult($newPayList, 1, '成功');
  100. }
  101. } catch (Exception $e) {
  102. $this->jsonResult('', 0, '支付获取异常:' . $e->getMessage());
  103. }
  104. }
  105. }