You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

244 regels
7.5 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/1/7
  6. * Time: 10:01 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\epay\library\Service;
  10. use addons\unishop\extend\Ali;
  11. use addons\unishop\extend\Hashids;
  12. use addons\unishop\extend\Wechat;
  13. use addons\unishop\model\Config;
  14. use think\Db;
  15. use think\Exception;
  16. use think\Hook;
  17. use think\Log;
  18. class Pay extends Base
  19. {
  20. protected $noNeedLogin = ['getPayType', 'notify', 'authRedirect', 'alipay', 'alinotify'];
  21. /**
  22. * 获取支付类型
  23. */
  24. public function getPayType()
  25. {
  26. $platfrom = $this->request->header('platform');
  27. $type = [];
  28. $offline = Config::getByName('offline_pay')['value'] == 1 ? true : false;
  29. switch ($platfrom) {
  30. case 'APP-PLUS';
  31. $type = ['alipay' => true, 'wxpay' => true, 'offline' => $offline];
  32. break;
  33. case 'H5':
  34. $type = ['alipay' => true, 'wxpay' => true, 'offline' => $offline];
  35. // 如果是微信内访问 公众号等
  36. if (Wechat::h5InWechat()) {
  37. $type['alipay'] = false;
  38. }
  39. break;
  40. case 'MP-WEIXIN':
  41. $type = ['alipay' => false, 'wxpay' => true, 'offline' => $offline];
  42. break;
  43. case 'MP-ALIPAY':
  44. $type = ['alipay' => true, 'wxpay' => false, 'offline' => $offline];
  45. break;
  46. case 'MP-BAIDU':
  47. $type = ['alipay' => false, 'wxpay' => false, 'offline' => $offline];
  48. break;
  49. case 'MP-TOUTIAO':
  50. $type = ['alipay' => false, 'wxpay' => false, 'offline' => $offline];
  51. break;
  52. }
  53. $this->success('', $type);
  54. }
  55. /**
  56. * 微信统一下单接口
  57. */
  58. public function unify()
  59. {
  60. $orderId = $this->request->request('order_id', 0);
  61. $orderId = Hashids::decodeHex($orderId);
  62. $orderModel = new \addons\unishop\model\Order();
  63. $order = $orderModel->where(['id' => $orderId])->find();
  64. try {
  65. if (!$order) {
  66. $this->error(__('Order does not exist'));
  67. }
  68. $products = $order->products()->select();
  69. $body = Config::getByName('name')['value'];
  70. foreach ($products as $product) {
  71. $body .= '_' . $product['title'];
  72. }
  73. //MWEB
  74. $platfrom = $this->request->header('platform', 'MP-WEIXIN');
  75. switch ($platfrom) {
  76. case 'MP-WEIXIN':
  77. $trade_type = 'JSAPI';
  78. break;
  79. case 'H5':
  80. case 'APP-PLUS':
  81. $trade_type = 'MWEB';
  82. break;
  83. }
  84. // 如果是微信内访问 公众号等
  85. if (Wechat::h5InWechat()) {
  86. $trade_type = 'wap';
  87. }
  88. $params = [
  89. 'amount' => bcmul($order['total_price'], 100),
  90. 'orderid' => $order->out_trade_no,
  91. 'type' => "wechat",
  92. 'title' => $body,
  93. 'notifyurl' => Config::getByName('notify_url')['value'],
  94. 'trade_type' => $trade_type,
  95. ];
  96. return Service::submitOrder($params);
  97. } catch (Exception $e) {
  98. $this->error($e->getMessage());
  99. }
  100. }
  101. /**
  102. * 支付通知回调
  103. */
  104. public function notify()
  105. {
  106. // 添加行为
  107. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  108. Hook::add('paid_fail', 'addons\\unishop\\behavior\\Order');
  109. $paytype = $this->request->param('type');
  110. file_put_contents(ROOT_PATH . '/runtime/log/' . date('Ym') ."/".date("d"). '.log', $paytype . PHP_EOL, FILE_APPEND);
  111. $pay = Service::checkNotify($paytype);
  112. if (!$pay) {
  113. echo '签名错误';
  114. return;
  115. }
  116. $data = $pay->verify();
  117. try {
  118. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  119. $out_trade_no = $data['out_trade_no'];
  120. // $paytype="alipay";
  121. // $payamount =11;
  122. // $out_trade_no = "202009205f6707697421b23";
  123. //你可以在此编写订单逻辑
  124. //Log::record('Alipay notify ,支付成功');
  125. // 条件一
  126. $orderModel = new \addons\unishop\model\Order(); //($message['out_trade_no']);
  127. $order = $orderModel->where(['out_trade_no' => $out_trade_no])->find();
  128. if (!$order || $order->have_paid != \addons\unishop\model\Order::PAID_NO) {
  129. throw new Exception('订单不存在或已完成');
  130. }
  131. // 条件二
  132. if ($order->total_price > $payamount || $order->total_price < $payamount) {
  133. throw new Exception('金额不一');
  134. }
  135. // 添加行为
  136. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  137. $payTypeString = $paytype == 'alipay' ? \addons\unishop\model\Order::PAY_ALIPAY : \addons\unishop\model\Order::PAY_WXPAY;
  138. Hook::listen('paid_success', $order, ['pay_type' => $payTypeString]);
  139. } catch (Exception $e) {
  140. }
  141. $pay = new \Yansongda\Pay\Pay();
  142. echo $pay->success();
  143. }
  144. /**
  145. * 在线支付
  146. */
  147. public function offline()
  148. {
  149. $orderId = $this->request->get('order_id', 0);
  150. $orderId = Hashids::decodeHex($orderId);
  151. $orderModel = new \addons\unishop\model\Order();
  152. $order = $orderModel->where(['id' => $orderId])->find();
  153. if (!$order) {
  154. $this->error(__('Order does not exist'));
  155. }
  156. try {
  157. Db::startTrans();
  158. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  159. Hook::listen('paid_success', $order, ['pay_type' => \addons\unishop\model\Order::PAY_OFFLINE]);
  160. Db::commit();
  161. } catch (Exception $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. }
  165. $this->success('', true);
  166. }
  167. /**
  168. * 微信内H5-JSAPI支付
  169. */
  170. public function jssdkBuildConfig()
  171. {
  172. $app = Wechat::initEasyWechat('payment');
  173. $configData = $app->jssdk->buildConfig(['chooseWXPay'], false, true, false);
  174. $this->success('', $configData);
  175. }
  176. /**
  177. * 支付宝支付
  178. */
  179. public function alipay()
  180. {
  181. $orderId = $this->request->request('order_id', 0);
  182. $orderId = Hashids::decodeHex($orderId);
  183. $orderModel = new \addons\unishop\model\Order();
  184. $order = $orderModel->where(['id' => $orderId])->find();
  185. try {
  186. if (!$order) {
  187. $this->error(__('Order does not exist'));
  188. }
  189. $products = $order->products()->select();
  190. $body = Config::getByName('name')['value'];
  191. foreach ($products as $product) {
  192. $body .= '_' . $product['title'];
  193. }
  194. $params = [
  195. 'amount' => $order->total_price,
  196. 'orderid' => $order->out_trade_no,
  197. 'type' => "alipay",
  198. 'title' => $body,
  199. 'notifyurl' => Config::getByName('ali_notify_url')['value'],
  200. 'returnurl' => Config::getByName('ali_return_url')['value'],
  201. 'method' => "wap",
  202. ];
  203. return Service::submitOrder($params);
  204. } catch (Exception $e) {
  205. $this->error($e->getMessage());
  206. }
  207. }
  208. }