Pay.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //$param参数
  48. $param = array
  49. (
  50. 'account_id' => $account_id,
  51. 'project_id' => 1,
  52. 'uid' => $uid,
  53. 'username' => $user['username'],
  54. 'name' => $order_data['name'],
  55. 'cash' => $order_data['cash'] * $order_data['num'],
  56. //'cash' => '0.01',
  57. 'openid' => isset($wechat['openid']) ? $wechat['openid'] : '',
  58. 'product_id' => $goods['id'],
  59. 'order_id' => $order_data['order_id'],
  60. );
  61. if ($method == 'apple') {
  62. $param['other'] = $receipt;
  63. }
  64. $result['pay'] = Dever::load('pay/api.' . $method, $param);
  65. $result['order_id'] = $order_data['order_id'];
  66. return $result;
  67. }
  68. # 生成订单号
  69. public function getOrderId()
  70. {
  71. $update['order_id'] = $this->createOrderId();
  72. $state = Dever::db('goods/order')->one($update);
  73. if (!$state) {
  74. return $update['order_id'];
  75. } else {
  76. return $this->getOrderId();
  77. }
  78. }
  79. # 生成订单号
  80. public function createOrderId()
  81. {
  82. if (function_exists('session_create_id')) {
  83. return strtoupper(session_create_id());
  84. } else {
  85. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  86. return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  87. }
  88. }
  89. # 临时订单号 无用
  90. public function createTmpOrderId($prefix = '')
  91. {
  92. return $prefix . (strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  93. }
  94. }