Pay.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. # 充值
  3. namespace Account\Lib;
  4. use Dever;
  5. class Pay
  6. {
  7. # 支付项目
  8. private $project_id = 1;
  9. # 支付渠道
  10. private $channel_id = 1;
  11. # 支付类型:5是小程序
  12. private $system_source = 5;
  13. # 生成订单
  14. public function create($data)
  15. {
  16. $order = [
  17. 'order_num' => $this->createOrderNum(),
  18. 'config_id' => $data['config_id'],
  19. 'info_id' => $data['info_id'],
  20. 'card_id' => $data['card_id'],
  21. 'uid' => $data['uid'],
  22. 'username' => $data['username'],
  23. 'name' => $data['name'],
  24. 'cash' => $data['cash'],
  25. 'price' => $data['price'],
  26. 'yue' => $data['yue'],
  27. ];
  28. $order['id'] = Dever::db('account/pay')->insert($order);
  29. if ($order['id']) {
  30. # 需要支付
  31. if ($data['pay_channel_id']) {
  32. $this->channel_id = $data['pay_channel_id'];
  33. }
  34. if ($data['pay_project_id']) {
  35. $this->project_id = $data['pay_project_id'];
  36. }
  37. if ($order['price'] > 0) {
  38. return $this->yes($order);
  39. } else {
  40. return $this->no($order);
  41. }
  42. }
  43. Dever::alert('支付失败');
  44. }
  45. # 生成订单号
  46. private function createOrderNum()
  47. {
  48. $where['order_num'] = Dever::order('P');
  49. $state = Dever::db('account/pay')->one($where);
  50. if (!$state) {
  51. return $where['order_num'];
  52. } else {
  53. return $this->createOrderNum();
  54. }
  55. }
  56. # 支付
  57. private function yes($order)
  58. {
  59. $openid = Dever::load('user/lib/applet')->getOpenid($order['uid'], $this->system_source, $this->project_id);
  60. $param = array(
  61. 'project_id' => $this->project_id,
  62. 'channel_id' => $this->channel_id,
  63. 'system_source' => $this->system_source,
  64. 'uid' => $order['uid'],
  65. 'username' => isset($order['username']) ? $order['username'] : '',
  66. 'name' => $order['name'],
  67. 'cash' => $order['price'],
  68. 'product_id' => $order['id'],
  69. 'order_id' => $order['order_num'],
  70. 'openid' => $openid,
  71. );
  72. $data = Dever::load('pay/api.pay', $param);
  73. $data['order_cash'] = Dever::number($order['cash']);
  74. $data['order_yue'] = Dever::number($order['yue']);
  75. $data['order_num'] = $order['order_num'];
  76. $data['order_id'] = $order['id'];
  77. return $data;
  78. }
  79. # 无需支付
  80. private function no($order)
  81. {
  82. $param['pay_product_id'] = $order['id'];
  83. $param['pay_order_id'] = $order['order_num'];
  84. $param['pay_status'] = 2;
  85. $param['pay_cash'] = $order['price'];
  86. $param['pay_msg'] = '';
  87. $this->success($param);
  88. return array(
  89. 'order' => '',
  90. 'order_cash' => Dever::number($order['cash']),
  91. 'order_yue' => Dever::number($order['yue']),
  92. 'order_num' => $order['order_num'],
  93. 'order_id' => $order['id'],
  94. 'type' => 'test',
  95. );
  96. }
  97. # 支付成功回调 安全加密 设置token
  98. public function success_secure_api_token()
  99. {
  100. $project_id = Dever::input('pay_project_id');
  101. $info = Dever::db('pay/project')->one($project_id);
  102. if ($info) {
  103. return $info['key'];
  104. }
  105. return 'account_dever';
  106. }
  107. # 支付成功回调 安全加密
  108. public function success_secure_api($param = array())
  109. {
  110. $this->success($param);
  111. }
  112. # 支付成功回调
  113. public function success($param = array())
  114. {
  115. $send = $param ? $param : Dever::preInput('pay_');
  116. $product_id = $send['pay_product_id'];
  117. $order_id = $send['pay_order_id'];
  118. $status = $send['pay_status'];
  119. $cash = $send['pay_cash'];
  120. $msg = $send['pay_msg'];
  121. $order = Dever::db('account/pay')->one(array('id' => $product_id, 'clear' => true));
  122. if ($order && $order['status'] == 1) {
  123. $update = array();
  124. if ($status == 2) {
  125. $update['status'] = $status;
  126. $update['where_id'] = $order['id'];
  127. Dever::db('account/pay')->update($update);
  128. $desc = '支付充值';
  129. Dever::load('account/lib/info.up_commit', $order['uid'], $order['cash'], 'chongzhi', 'qianbao', $desc, 'account/pay', $order['id']);
  130. if (Dever::project('wechat_applet')) {
  131. //Dever::load('order/lib/applet')->add($order['id'], 2);
  132. }
  133. # 发消息
  134. if ($order['uid'] && $order['uid'] > 0) {
  135. $this->payNotice($order);
  136. }
  137. }
  138. }
  139. return 'ok';
  140. }
  141. public function payNotice($order)
  142. {
  143. if ($order['card_id'] > 0 && Dever::project('duihuan')) {
  144. Dever::load('duihuan/lib/give')->act($order['uid'], 1, $order['card_id'], $order['order_num']);
  145. }
  146. }
  147. }