payRequest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * 统一的预创建订单请求对象
  4. * @version
  5. * 1.0.0 Created at 2017-12-21. by --gwang
  6. * @author gwang (mail@wanggangzero.cn)
  7. * @copyright ? 2017-12-21, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  8. */
  9. class PayRequest extends loyalsoft\Object_ext {
  10. const orderTab = 'tpl_order_tab';
  11. /**
  12. * @var string 充值回调透传参数(len=250)
  13. */
  14. public $callbackInfo;
  15. /**
  16. * 各平台取自己的配置信息
  17. * @var string 充值回调地址(len=100)
  18. */
  19. public $notifyUrl;
  20. /**
  21. * @var string 订单ID (len=30)
  22. */
  23. public $cpOrderId;
  24. /**
  25. * @var float 充值金额 (单位:元)
  26. */
  27. public $amount;
  28. /**
  29. * @var string 平台UID
  30. */
  31. public $accountId;
  32. public $zoneid;
  33. public $channel;
  34. public $product_id;
  35. public $product_name;
  36. public $product_price;
  37. public $product_count;
  38. /**
  39. * @var 防沉迷年龄段 ( -1 未实名, 0(0~7) 8(8~16) 16(16~18) 18(18+成年)
  40. * #[optional]
  41. */
  42. public $ageRange;
  43. /**
  44. * @var string 公会id
  45. */
  46. public $party_id;
  47. public $party_name;
  48. /**
  49. * @var string 签名算法, 目前固定为MD5
  50. */
  51. public $signType = "MD5";
  52. /**
  53. *
  54. * @var string MD5(签名内容+apiKey)
  55. */
  56. public $sign;
  57. /**
  58. * @var int 订单编号
  59. */
  60. private $order_id;
  61. /**
  62. *
  63. * @return array 订单写入数据库
  64. */
  65. public function InserDataBase() {
  66. // return false; # 2019.10.9 关闭充值
  67. $arr = get_object_vars($this); # 转为数组
  68. $arr["uid"] = $arr["accountId"]; # 转换下变量名称
  69. // $arr["amount"] = $arr["amount"]; # 拉起平台时传的单位是元,比如UC还有易接,而游戏后台存储的是分
  70. unset($arr["signType"]); # 毙掉后台不需要的数据
  71. unset($arr["sign"]);
  72. unset($arr["accountId"]);
  73. unset($arr['order_id']);
  74. $n = loyalsoft\daoInst()->update(self::orderTab)
  75. ->data($arr)
  76. ->where('id')->eq($this->order_id)
  77. ->exec();
  78. return $n == 1;
  79. }
  80. /**
  81. * @return array 拉取签名参数数组
  82. */
  83. public function uc_GetData2Sign() {
  84. $sid = array('callbackInfo', 'amount', 'notifyUrl', 'cpOrderId', 'accountId');
  85. $arr = array();
  86. foreach (get_object_vars($this) as $key => $value) {
  87. if (in_array($key, $sid)) {
  88. $arr[$key] = $value;
  89. }
  90. }
  91. return $arr;
  92. }
  93. /**
  94. * @return array 普通返回值数组
  95. */
  96. public function retData() {
  97. $sid = array('callbackInfo', 'amount', 'product_price', 'product_count', 'notifyUrl', 'cpOrderId');
  98. $arr = array();
  99. foreach (get_object_vars($this) as $key => $value) {
  100. if (in_array($key, $sid)) {
  101. $arr[$key] = $value;
  102. }
  103. }
  104. return $arr;
  105. }
  106. /**
  107. * @return array 小七手游订单结构(未签名)
  108. */
  109. public function x7sy_retData() {
  110. $ret = array(
  111. 'extends_info_data' => $this->callbackInfo, // // 透传参数
  112. 'game_area' => $this->zoneid,
  113. 'game_guid' => $this->accountId,
  114. 'game_level' => 0,
  115. 'game_orderid' => $this->cpOrderId,
  116. 'game_price' => $this->product_price, // // 对方要求单位:元
  117. 'game_role_id' => "no",
  118. 'game_role_name' => "no",
  119. 'notify_id' => "3788", // // 如果只有一个可以设置为-1,但不可以设置为0
  120. 'subject' => $this->product_name
  121. );
  122. return $ret;
  123. }
  124. /**
  125. * @return array 易接返回值数组
  126. */
  127. public function yijie_retData() {
  128. return $this->retData();
  129. }
  130. public function __construct($arg = null) {
  131. parent::__construct($arg);
  132. if (!$this->accountId || !$this->amount || !$this->channel || !$this->product_id) {
  133. \loyalsoft\CLog::pay('参数缺失');
  134. exit('{"err":1,"msg":"参数缺失!"}');
  135. }
  136. if ($this->amount) {
  137. // $this->amount = number_format($this->amount, 2, '.', '');
  138. // if ('soft' == $this->channel) { # 2018年1月15号以前固定为1分钱测试
  139. // $this->amount = number_format(0.01, 2, '.', '');
  140. // $this->product_price = 0.01;
  141. // } else //
  142. // if ($this->amount != loyalsoft\GameConfig::shop_getItem($this->product_id)->price) {
  143. // var_dump($this->product_id);
  144. // var_dump(loyalsoft\GameConfig::shop_getItem($this->product_id)->price);
  145. // \loyalsoft\CLog::pay('金额不符' . $this->amount . " ");
  146. // exit('{"err":1,"msg":"金额不符!"}');
  147. // }
  148. }
  149. loyalsoft\daoInst()->insert(self::orderTab)
  150. ->data(array('order_ts' => loyalsoft\now()))
  151. ->exec();
  152. $this->order_id = loyalsoft\daoInst()->select('last_insert_id() as id')->fetch('id'); # 订单自增id
  153. $this->cpOrderId = LoyalsoftAPPID . \date('ymdHi') . substr(sprintf("%1$06d", $this->order_id), -6); # 商户唯一id(尾部附加自增id后6位数)
  154. $this->callbackInfo .= "," . $this->cpOrderId; # 将cpOrderId追加到透传参数中
  155. $this->notifyUrl = self::get_notify_url(); # 自动提取支付回调地址
  156. }
  157. /**
  158. * PS. 仅限pay.php作为入口使用才行
  159. * @return string 获取支付回调地址,
  160. */
  161. public static function get_notify_url() {
  162. $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
  163. $nurl = substr($url, 0, strpos($url, "pay.php")) . "notify.php";
  164. return $nurl; # 回调地址
  165. }
  166. public function AntiAdditionCheck() {
  167. $uid = $this->accountId;
  168. $ageRange = $this->ageRange;
  169. $amt = $this->amount;
  170. if (isset($uid) && isset($ageRange)) {
  171. $monthTS = loyalsoft\TimeUtil::tsMonthBegin(); # 本月初起始时间戳
  172. $dayTS = loyalsoft\TimeUtil::tsDayBegin(); # 当天起始时间戳
  173. $monthTotal = loyalsoft\daoInst()
  174. ->select("sum(`amount`)/100 as total") # 总金额(单位:分转元)
  175. ->from("tpl_order_tab")
  176. ->where("uid")->eq($uid) # 该玩家
  177. ->andWhere("status")->eq(1) # 成交订单
  178. ->andWhere("order_ts")->ge($monthTS) # 本月
  179. ->fetch('total');
  180. if (!is_numeric($monthTotal)) {
  181. $monthTotal = 0;
  182. }
  183. $dayTotal = loyalsoft\daoInst()
  184. ->select("sum(`amount`)/100 as total") # 总金额(单位:分转元)
  185. ->from("tpl_order_tab")
  186. ->where("uid")->eq($uid) # 该玩家
  187. ->andWhere("status")->eq(1) # 成交订单
  188. ->andWhere("order_ts")->ge($dayTS) # 今日
  189. ->fetch('total');
  190. if (!is_numeric($dayTotal)) {
  191. $dayTotal = 0;
  192. }
  193. if ($ageRange < 0 || !is_numeric($ageRange)) {
  194. return \Resp::err(1, "未完成实名认证的用户暂不提供充值.");
  195. } else if ($ageRange < 8) {
  196. return \Resp::err(1, "未慢8周岁的用户暂不提供充值.");
  197. } else if ($ageRange < 16) { # 单笔/每月: 50,200
  198. if ($amt > 50) {
  199. return \Resp::err(1, "单笔充值金额不高于50¥");
  200. }
  201. // if (($amt + $dayTotal) > 100) {
  202. // return \Resp::err(1, "单日累计充值金额不高于100¥");
  203. // }
  204. if (($amt + $monthTotal) > 200) {
  205. return \Resp::err(1, "当月累计充值金额不高于200¥");
  206. }
  207. } else if ($ageRange < 18) { # 单笔/每月: 100,400
  208. if ($amt > 100) {
  209. return \Resp::err(1, "单笔充值金额不高于100¥");
  210. }
  211. // if (($amt + $dayTotal) > 100) {
  212. // return \Resp::err(1, "单日累计充值金额不高于100¥");
  213. // }
  214. if (($amt + $monthTotal) > 400) {
  215. return \Resp::err(1, "当月累计充值金额不高于400¥");
  216. }
  217. }
  218. return \Resp::ok("ok");
  219. }
  220. return \Resp::err(-1, "参数不足"); # 其他情况
  221. }
  222. }