Pay.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. # 支付
  3. namespace Goods\Lib;
  4. use Dever;
  5. class Pay
  6. {
  7. public function action($uid, $id, $sku, $num, $source)
  8. {
  9. if (!$uid) {
  10. Dever::alert('错误的用户信息');
  11. }
  12. $goods = Dever::load('goods/lib/info')->getPayInfo($id, $sku);
  13. $user = Dever::db('passport/user')->one($uid);
  14. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  15. if ($source == 'ios') {
  16. $method = 'apple';
  17. $account_id = 3;
  18. # 使用苹果内购支付
  19. $receipt = Dever::input('receipt');
  20. if (!$receipt) {
  21. Dever::alert('苹果内购支付失败,没有receipt参数');
  22. }
  23. } elseif ($source == 'android') {
  24. $method = 'app';
  25. $account_id = 3;
  26. } elseif ($source == 'applet') {
  27. # 小程序支付
  28. $method = 'applet';
  29. $account_id = 2;
  30. } else {
  31. # 默认是网页支付
  32. $method = 'page';
  33. $account_id = 1;
  34. }
  35. $order_data['uid'] = $uid;
  36. $order_data['status'] = 1;
  37. $order_data['info_id'] = $goods['id'];
  38. $order_data['name'] = $goods['name'];
  39. $order_data['cash'] = $goods['price'];
  40. $order_data['num'] = $num;
  41. $order_data['source'] = $source;
  42. $order_data['order_id'] = $this->getOrderId();
  43. $id = Dever::db('goods/order')->insert($order_data);
  44. if (!$id) {
  45. Dever::alert('支付失败');
  46. }
  47. $refer = 'test';
  48. //$param参数
  49. $param = array
  50. (
  51. 'account_id' => $account_id,
  52. 'project_id' => 1,
  53. 'uid' => $uid,
  54. 'username' => $user['username'],
  55. 'name' => $order_data['name'],
  56. 'cash' => $order_data['cash'] * $order_data['num'],
  57. //'cash' => '0.01',
  58. 'openid' => isset($wechat['openid']) ? $wechat['openid'] : '',
  59. 'product_id' => $goods['id'],
  60. 'order_id' => $order_data['order_id'],
  61. 'refer' => $refer
  62. );
  63. if ($method == 'apple') {
  64. $param['other'] = $receipt;
  65. }
  66. $result['pay'] = Dever::load('pay/api.' . $method, $param);
  67. $result['order_id'] = $order_data['order_id'];
  68. return $result;
  69. }
  70. # 生成订单号
  71. public function getOrderId()
  72. {
  73. $where['order_id'] = $this->createOrderId();
  74. $state = Dever::db('goods/order')->one($where);
  75. if (!$state) {
  76. return $where['order_id'];
  77. } else {
  78. return $this->getOrderId();
  79. }
  80. }
  81. # 生成订单号
  82. public function createOrderId()
  83. {
  84. if (function_exists('session_create_id')) {
  85. return strtoupper(session_create_id());
  86. } else {
  87. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  88. return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  89. }
  90. }
  91. # 临时订单号 无用
  92. public function createTmpOrderId($prefix = '')
  93. {
  94. return $prefix . (strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  95. }
  96. }