payRequest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. unset($arr['ageRange']);
  75. $n = loyalsoft\daoInst()->update(self::orderTab)
  76. ->data($arr)
  77. ->where('id')->eq($this->order_id)
  78. ->exec();
  79. return $n == 1;
  80. }
  81. /**
  82. * @return array 拉取签名参数数组
  83. */
  84. public function uc_GetData2Sign() {
  85. $sid = array('callbackInfo', 'amount', 'notifyUrl', 'cpOrderId', 'accountId');
  86. $arr = array();
  87. foreach (get_object_vars($this) as $key => $value) {
  88. if (in_array($key, $sid)) {
  89. $arr[$key] = $value;
  90. }
  91. }
  92. return $arr;
  93. }
  94. /**
  95. * @return array 普通返回值数组
  96. */
  97. public function retData() {
  98. $sid = array('callbackInfo', 'amount', 'product_price', 'product_count', 'notifyUrl', 'cpOrderId');
  99. $arr = array();
  100. foreach (get_object_vars($this) as $key => $value) {
  101. if (in_array($key, $sid)) {
  102. $arr[$key] = $value;
  103. }
  104. }
  105. return $arr;
  106. }
  107. /**
  108. * @return array 小七手游订单结构(未签名)
  109. */
  110. public function x7sy_retData() {
  111. $ret = array(
  112. 'extends_info_data' => $this->callbackInfo, // // 透传参数
  113. 'game_area' => $this->zoneid,
  114. 'game_guid' => $this->accountId,
  115. 'game_level' => 0,
  116. 'game_orderid' => $this->cpOrderId,
  117. 'game_price' => $this->product_price, // // 对方要求单位:元
  118. 'game_role_id' => "no",
  119. 'game_role_name' => "no",
  120. 'notify_id' => "3788", // // 如果只有一个可以设置为-1,但不可以设置为0
  121. 'subject' => $this->product_name
  122. );
  123. return $ret;
  124. }
  125. /**
  126. * @return array 易接返回值数组
  127. */
  128. public function yijie_retData() {
  129. return $this->retData();
  130. }
  131. public function __construct($arg = null) {
  132. parent::__construct($arg);
  133. if (!$this->accountId || !$this->amount || !$this->channel || !$this->product_id) {
  134. \loyalsoft\CLog::pay('参数缺失');
  135. exit('{"err":1,"msg":"参数缺失!"}');
  136. }
  137. if ($this->amount) {
  138. // $this->amount = number_format($this->amount, 2, '.', '');
  139. // if ('soft' == $this->channel) { # 2018年1月15号以前固定为1分钱测试
  140. // $this->amount = number_format(0.01, 2, '.', '');
  141. // $this->product_price = 0.01;
  142. // } else //
  143. // if ($this->amount != loyalsoft\GameConfig::shop_getItem($this->product_id)->price) {
  144. // var_dump($this->product_id);
  145. // var_dump(loyalsoft\GameConfig::shop_getItem($this->product_id)->price);
  146. // \loyalsoft\CLog::pay('金额不符' . $this->amount . " ");
  147. // exit('{"err":1,"msg":"金额不符!"}');
  148. // }
  149. }
  150. loyalsoft\daoInst()->insert(self::orderTab)
  151. ->data(array('order_ts' => loyalsoft\now()))
  152. ->exec();
  153. $this->order_id = loyalsoft\daoInst()->select('last_insert_id() as id')->fetch('id'); # 订单自增id
  154. $this->cpOrderId = LoyalsoftAPPID . \date('ymdHi') . substr(sprintf("%1$06d", $this->order_id), -6); # 商户唯一id(尾部附加自增id后6位数)
  155. $this->callbackInfo .= "," . $this->cpOrderId; # 将cpOrderId追加到透传参数中
  156. $this->notifyUrl = self::get_notify_url(); # 自动提取支付回调地址
  157. }
  158. /**
  159. * PS. 仅限pay.php作为入口使用才行
  160. * @return string 获取支付回调地址,
  161. */
  162. public static function get_notify_url() {
  163. $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
  164. $nurl = substr($url, 0, strpos($url, "pay.php")) . "notify.php";
  165. return $nurl; # 回调地址
  166. }
  167. public function AntiAdditionCheck() {
  168. $uid = $this->accountId;
  169. $ageRange = $this->ageRange;
  170. $amt = $this->amount;
  171. if (isset($uid) && isset($ageRange)) {
  172. $monthTS = loyalsoft\TimeUtil::tsMonthBegin(); # 本月初起始时间戳
  173. $dayTS = loyalsoft\TimeUtil::tsDayBegin(); # 当天起始时间戳
  174. $monthTotal = loyalsoft\daoInst()
  175. ->select("sum(`amount`)/100 as total") # 总金额(单位:分转元)
  176. ->from("tpl_order_tab")
  177. ->where("uid")->eq($uid) # 该玩家
  178. ->andWhere("status")->eq(1) # 成交订单
  179. ->andWhere("order_ts")->ge($monthTS) # 本月
  180. ->fetch('total');
  181. if (!is_numeric($monthTotal)) {
  182. $monthTotal = 0;
  183. }
  184. $dayTotal = loyalsoft\daoInst()
  185. ->select("sum(`amount`)/100 as total") # 总金额(单位:分转元)
  186. ->from("tpl_order_tab")
  187. ->where("uid")->eq($uid) # 该玩家
  188. ->andWhere("status")->eq(1) # 成交订单
  189. ->andWhere("order_ts")->ge($dayTS) # 今日
  190. ->fetch('total');
  191. if (!is_numeric($dayTotal)) {
  192. $dayTotal = 0;
  193. }
  194. if ($ageRange < 0 || !is_numeric($ageRange)) {
  195. return \Resp::err(1, "未完成实名认证的用户暂不提供充值.");
  196. } else if ($ageRange < 8) {
  197. return \Resp::err(1, "未满8周岁的用户暂不提供充值.");
  198. } else if ($ageRange < 16) { # 单笔/每月: 50,200
  199. if ($amt > 50) {
  200. return \Resp::err(1, "单笔充值金额不高于50¥");
  201. }
  202. // if (($amt + $dayTotal) > 100) {
  203. // return \Resp::err(1, "单日累计充值金额不高于100¥");
  204. // }
  205. if (($amt + $monthTotal) > 200) {
  206. return \Resp::err(1, "当月累计充值金额不高于200¥");
  207. }
  208. } else if ($ageRange < 18) { # 单笔/每月: 100,400
  209. if ($amt > 100) {
  210. return \Resp::err(1, "单笔充值金额不高于100¥");
  211. }
  212. // if (($amt + $dayTotal) > 100) {
  213. // return \Resp::err(1, "单日累计充值金额不高于100¥");
  214. // }
  215. if (($amt + $monthTotal) > 400) {
  216. return \Resp::err(1, "当月累计充值金额不高于400¥");
  217. }
  218. }
  219. return \Resp::ok("ok");
  220. }
  221. return \Resp::err(-1, "参数不足"); # 其他情况
  222. }
  223. }