Pay.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. # 支付
  3. namespace Goods\Lib;
  4. use Dever;
  5. class Pay
  6. {
  7. # 发起支付
  8. public function action($uid, $id, $sku, $num, $source)
  9. {
  10. if (!$uid) {
  11. Dever::alert('错误的用户信息');
  12. }
  13. $source = 'android';
  14. $goods = Dever::load('goods/lib/info')->getPayInfo($id, $sku);
  15. $user = Dever::db('passport/user')->one($uid);
  16. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  17. if ($source == 'ios') {
  18. $method = 'apple';
  19. $account_id = 3;
  20. # 使用苹果内购支付
  21. $receipt = Dever::input('receipt');
  22. if (!$receipt) {
  23. Dever::alert('苹果内购支付失败,没有receipt参数');
  24. }
  25. } elseif ($source == 'android') {
  26. $method = 'app';
  27. $account_id = 3;
  28. } elseif ($source == 'applet') {
  29. # 小程序支付
  30. $method = 'applet';
  31. $account_id = 2;
  32. } else {
  33. # 默认是网页支付
  34. $method = 'page';
  35. $account_id = 1;
  36. }
  37. $order_data['uid'] = $uid;
  38. $order_data['status'] = 1;
  39. $order_data['info_id'] = $goods['id'];
  40. $order_data['name'] = $goods['name'];
  41. $order_data['cash'] = $goods['price'];
  42. $order_data['num'] = $num;
  43. $order_data['source'] = $source;
  44. $order_data['order_id'] = $this->getOrderId();
  45. $id = Dever::db('goods/order')->insert($order_data);
  46. if (!$id) {
  47. Dever::alert('支付失败');
  48. }
  49. $refer = 'test';
  50. //$param参数
  51. $param = array
  52. (
  53. 'account_id' => $account_id,
  54. 'project_id' => 1,
  55. 'uid' => $uid,
  56. 'username' => $user['username'],
  57. 'name' => $order_data['name'],
  58. 'cash' => $order_data['cash'] * $order_data['num'],
  59. //'cash' => '0.01',
  60. 'openid' => isset($wechat['openid']) ? $wechat['openid'] : '',
  61. 'product_id' => $goods['id'],
  62. 'order_id' => $order_data['order_id'],
  63. 'refer' => $refer
  64. );
  65. if ($method == 'apple') {
  66. $param['other'] = $receipt;
  67. }
  68. $result['pay'] = Dever::load('pay/api.' . $method, $param);
  69. $result['order_id'] = $order_data['order_id'];
  70. return $result;
  71. }
  72. # 支付成功回调 安全加密 设置token
  73. public function success_secure_api_token()
  74. {
  75. $project_id = Dever::input('pay_project_id');
  76. $info = Dever::db('pay/project')->one($project_id);
  77. if ($info) {
  78. return $info['key'];
  79. }
  80. return 'goods_dever_2020';
  81. }
  82. # 支付成功回调 安全加密
  83. public function success_secure_api($param = array())
  84. {
  85. $this->success($param);
  86. }
  87. # 支付成功回调
  88. public function success($param = array())
  89. {
  90. $send = $param ? $param : Dever::preInput('pay_');
  91. $order_id = $send['pay_order_id'];
  92. $status = $send['pay_status'];
  93. $msg = $send['pay_msg'];
  94. $order = Dever::db('goods/order')->one(array('order_id' => $order_id));
  95. if ($order && $order['status'] == 1) {
  96. if ($status == 2) {
  97. # 增加积分
  98. Dever::score($order['uid'], 'buy_goods', '购买商品');
  99. # 发消息
  100. if (Dever::project('message')) {
  101. Dever::load('message/lib/data')->push(-1, $order['uid'], '购买提醒', '购买成功', 11);
  102. }
  103. }
  104. $update['status'] = $status;
  105. $update['where_id'] = $order['id'];
  106. Dever::db('goods/order')->update($update);
  107. }
  108. return 'ok';
  109. }
  110. # 生成订单号
  111. public function getOrderId()
  112. {
  113. $where['order_id'] = $this->createOrderId();
  114. $state = Dever::db('goods/order')->one($where);
  115. if (!$state) {
  116. return $where['order_id'];
  117. } else {
  118. return $this->getOrderId();
  119. }
  120. }
  121. # 生成订单号
  122. public function createOrderId()
  123. {
  124. if (function_exists('session_create_id')) {
  125. return strtoupper(session_create_id());
  126. } else {
  127. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  128. return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  129. }
  130. }
  131. # 临时订单号 无用
  132. public function createTmpOrderId($prefix = '')
  133. {
  134. return $prefix . (strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  135. }
  136. }