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.
 
 
 
 
 
 

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