payRequest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 string 公会id
  40. */
  41. public $party_id;
  42. public $party_name;
  43. /**
  44. * @var string 签名算法, 目前固定为MD5
  45. */
  46. public $signType = "MD5";
  47. /**
  48. *
  49. * @var string MD5(签名内容+apiKey)
  50. */
  51. public $sign;
  52. /**
  53. * @var int 订单编号
  54. */
  55. private $order_id;
  56. /**
  57. *
  58. * @return array 订单写入数据库
  59. */
  60. public function InserDataBase() {
  61. // return false; # 2019.10.9 关闭充值
  62. $arr = get_object_vars($this); # 转为数组
  63. $arr["uid"] = $arr["accountId"]; # 转换下变量名称
  64. // $arr["amount"] = $arr["amount"]; # 拉起平台时传的单位是元,比如UC还有易接,而游戏后台存储的是分
  65. unset($arr["signType"]); # 毙掉后台不需要的数据
  66. unset($arr["sign"]);
  67. unset($arr["accountId"]);
  68. unset($arr['order_id']);
  69. $n = loyalsoft\daoInst()->update(self::orderTab)
  70. ->data($arr)
  71. ->where('id')->eq($this->order_id)
  72. ->exec();
  73. return $n == 1;
  74. }
  75. /**
  76. * @return array 拉取签名参数数组
  77. */
  78. public function uc_GetData2Sign() {
  79. $sid = array('callbackInfo', 'amount', 'notifyUrl', 'cpOrderId', 'accountId');
  80. $arr = array();
  81. foreach (get_object_vars($this) as $key => $value) {
  82. if (in_array($key, $sid)) {
  83. $arr[$key] = $value;
  84. }
  85. }
  86. return $arr;
  87. }
  88. /**
  89. * @return array 普通返回值数组
  90. */
  91. public function retData() {
  92. $sid = array('callbackInfo', 'amount', 'product_price', 'product_count', 'notifyUrl', 'cpOrderId');
  93. $arr = array();
  94. foreach (get_object_vars($this) as $key => $value) {
  95. if (in_array($key, $sid)) {
  96. $arr[$key] = $value;
  97. }
  98. }
  99. return $arr;
  100. }
  101. /**
  102. * @return array 小七手游订单结构(未签名)
  103. */
  104. public function x7sy_retData() {
  105. $ret = array(
  106. 'extends_info_data' => $this->callbackInfo, // // 透传参数
  107. 'game_area' => $this->zoneid,
  108. 'game_guid' => $this->accountId,
  109. 'game_level' => 0,
  110. 'game_orderid' => $this->cpOrderId,
  111. 'game_price' => $this->product_price, // // 对方要求单位:元
  112. 'game_role_id' => "no",
  113. 'game_role_name' => "no",
  114. 'notify_id' => "3788", // // 如果只有一个可以设置为-1,但不可以设置为0
  115. 'subject' => $this->product_name
  116. );
  117. return $ret;
  118. }
  119. /**
  120. * @return array 易接返回值数组
  121. */
  122. public function yijie_retData() {
  123. return $this->retData();
  124. }
  125. public function __construct($arg = null) {
  126. parent::__construct($arg);
  127. if (!$this->accountId || !$this->amount || !$this->channel || !$this->product_id) {
  128. \loyalsoft\CLog::pay('参数缺失');
  129. exit('{"err":1,"msg":"参数缺失!"}');
  130. }
  131. if ($this->amount) {
  132. // $this->amount = number_format($this->amount, 2, '.', '');
  133. // if ('soft' == $this->channel) { # 2018年1月15号以前固定为1分钱测试
  134. // $this->amount = number_format(0.01, 2, '.', '');
  135. // $this->product_price = 0.01;
  136. // } else //
  137. // if ($this->amount != loyalsoft\GameConfig::shop_getItem($this->product_id)->price) {
  138. // var_dump($this->product_id);
  139. // var_dump(loyalsoft\GameConfig::shop_getItem($this->product_id)->price);
  140. // \loyalsoft\CLog::pay('金额不符' . $this->amount . " ");
  141. // exit('{"err":1,"msg":"金额不符!"}');
  142. // }
  143. }
  144. loyalsoft\daoInst()->insert(self::orderTab)
  145. ->data(array('order_ts' => loyalsoft\now()))
  146. ->exec();
  147. $this->order_id = loyalsoft\daoInst()->select('last_insert_id() as id')->fetch('id'); # 订单自增id
  148. $this->cpOrderId = LoyalsoftAPPID . \date('ymdHi') . substr(sprintf("%1$06d", $this->order_id), -6); # 商户唯一id(尾部附加自增id后6位数)
  149. $this->callbackInfo .= "," . $this->cpOrderId; # 将cpOrderId追加到透传参数中
  150. $this->notifyUrl = self::get_notify_url(); # 自动提取支付回调地址
  151. }
  152. /**
  153. * PS. 仅限pay.php作为入口使用才行
  154. * @return string 获取支付回调地址,
  155. */
  156. public static function get_notify_url() {
  157. $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
  158. $nurl = substr($url, 0, strpos($url, "pay.php")) . "notify.php";
  159. return $nurl; # 回调地址
  160. }
  161. }