Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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