Account.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php namespace Place\Lib;
  2. use Dever;
  3. class Account
  4. {
  5. # 下单操作
  6. public function pay($order)
  7. {
  8. $account = Dever::input('platform', 'is_string', '账户', 'weixin');
  9. $env = Dever::input('env', 'is_numeric', '运行环境', 3);
  10. $result = $this->openid(['key' => $account], $env, $order['uid']);
  11. if (isset($result['link'])) {
  12. return $result;
  13. }
  14. $account = $this->getInfo(['key' => 'pay_' . $account]);
  15. if (!$account) {
  16. Dever::error('支付账户配置错误');
  17. }
  18. $cert = Dever::db('place/account_cert')->find(['account_id' => $account['id'], 'platform_cert_id' => 2]);
  19. if (!$cert || ($cert && $cert['edate'] <= time())) {
  20. Dever::load(\Api\Lib\Account::class)->run($account, 'task', [], 1, 'run', 'place');
  21. }
  22. $order['openid'] = $result['openid'];
  23. $order['cash'] = $order['pay_money_cash']*100;
  24. $order['time_expire'] = time() + 4000;
  25. return Dever::load(\Api\Lib\Account::class)->run($account, 'order', $order, $env, 'run', 'place');
  26. }
  27. # 退款操作
  28. public function refund($order, $pay_cash, $refund_order_num, $sector_id, $place_id, $type)
  29. {
  30. $account = $order['env_platform'];
  31. $env = $order['env_type'];
  32. $account = $this->getInfo(['key' => 'pay_' . $account]);
  33. if (!$account) {
  34. Dever::error('支付账户配置错误');
  35. }
  36. $param['order_num'] = $order['order_num'];
  37. $param['refund_order_num'] = $refund_order_num;
  38. $param['total'] = $order['pay_money_cash']*100;
  39. $param['cash'] = $pay_cash*100;
  40. $param['notify'] = 'Porder/Lib/Notify.refund|' . $sector_id . '|' . $place_id . '|' . $type . '|' . $refund_order_num;
  41. return Dever::load(\Api\Lib\Account::class)->run($account, 'refund', $param, 1, 'run', 'place');
  42. }
  43. # 微信小程序发货操作
  44. public function delivery($order, $detail, $data, $mode = 1, $delivered = true)
  45. {
  46. if ($order['env_platform'] == 'weixin' && $order['env_type'] == 5) {
  47. # 虚拟商品
  48. $method = 3;
  49. if ($data && isset($order['method'])) {
  50. if ($order['method'] == 3) {
  51. # 自提
  52. $method = 4;
  53. } elseif ($detail['source_type'] == 1) {
  54. # 快递
  55. $method = 1;
  56. }
  57. }
  58. $goods = $detail['name'];
  59. $delivery = [];
  60. if ($method == 1) {
  61. $address = Dever::db('puser/address')->find($order['address_id']);
  62. $contact = '';
  63. if ($address) {
  64. $contact = ['receiver_contact' => \Dever\Helper\Str::hide($address['phone'])];
  65. }
  66. foreach ($data as $v) {
  67. $content = explode(',', $v['content']);
  68. $code = $v['code'];
  69. foreach ($content as $v1) {
  70. $delivery[] = [
  71. 'tracking_no' => $v1,
  72. 'express_company' => $code,
  73. 'item_desc' => $goods,
  74. 'contact' => $contact,
  75. ];
  76. }
  77. }
  78. } else {
  79. $delivery[] = [
  80. 'item_desc' => $goods,
  81. ];
  82. }
  83. $result = $this->openid(['key' => $order['env_platform']], $order['env_type'], $order['uid']);
  84. if (!$result['openid']) {
  85. return;
  86. }
  87. $account = $this->getInfo(['key' => $order['env_platform']]);
  88. if (!$account) {
  89. Dever::error('账户配置错误');
  90. }
  91. $param = [];
  92. $param['order_num'] = $order['order_num'];
  93. $param['method'] = $method;
  94. $param['delivery'] = $delivery;
  95. $param['mode'] = $mode;
  96. $param['delivered'] = $delivered;
  97. $param['time'] = time();
  98. $param['openid'] = $result['openid'];
  99. return Dever::load(\Api\Lib\Account::class)->run($account, 'delivery', $param, $env, 'run', 'place');
  100. }
  101. }
  102. # 获取openid
  103. public function openid($account, $env, $uid, $result = [])
  104. {
  105. $account = $this->getInfo($account);
  106. return Dever::load(\Api\Lib\Util::class)->openid($account, $env, $uid, $result, 'place');
  107. }
  108. # 获取小程序码
  109. # version = develop/release/trial
  110. public function getAppletCode($account, $env, $scene, $page, $version = 'develop')
  111. {
  112. $token = Dever::load(\Api\Lib\Util::class)->token($account, $env, 'place');
  113. if ($token) {
  114. $param['access_token'] = $token;
  115. $param['scene'] = $scene;
  116. $param['page'] = $page;
  117. $param['env_version'] = $version;
  118. $data = Dever::load(\Api\Lib\Account::class)->run($account, 'applet_code', $param, 1, 'run', 'place');
  119. return $data;
  120. }
  121. }
  122. # 获取账户信息
  123. public function getInfo($where)
  124. {
  125. $info = Dever::db('place/account')->find($where);
  126. if ($info) {
  127. $account = Dever::db('api/account')->find(['key' => $info['key']]);
  128. $info['app_id'] = $account['app_id'];
  129. $info['platform_id'] = $account['platform_id'];
  130. $info['desc'] = $account['desc'];
  131. return $info;
  132. }
  133. }
  134. }