No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

258 líneas
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\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. file_put_contents(ROOT_PATH . '/runtime/log/' . date('Ym') ."/".date("d"). '.log', $paytype . PHP_EOL, FILE_APPEND);
  114. $pay = Service::checkNotify($paytype);
  115. if (!$pay) {
  116. echo '签名错误';
  117. return;
  118. }
  119. $data = $pay->verify();
  120. try {
  121. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  122. $out_trade_no = $data['out_trade_no'];
  123. // $paytype="alipay";
  124. // $payamount =11;
  125. // $out_trade_no = "202009205f6707697421b23";
  126. //你可以在此编写订单逻辑
  127. //Log::record('Alipay notify ,支付成功');
  128. // 条件一
  129. $orderModel = new \addons\unishop\model\Order(); //($message['out_trade_no']);
  130. $order = $orderModel->where(['out_trade_no' => $out_trade_no])->find();
  131. if (!$order || $order->have_paid != \addons\unishop\model\Order::PAID_NO) {
  132. throw new Exception('订单不存在或已完成');
  133. }
  134. // 条件二
  135. if ($order->total_price > $payamount || $order->total_price < $payamount) {
  136. throw new Exception('金额不一');
  137. }
  138. // 添加行为
  139. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  140. $payTypeString = $paytype == 'alipay' ? \addons\unishop\model\Order::PAY_ALIPAY : \addons\unishop\model\Order::PAY_WXPAY;
  141. Hook::listen('paid_success', $order, ['pay_type' => $payTypeString]);
  142. } catch (Exception $e) {
  143. }
  144. $pay = new \Yansongda\Pay\Pay();
  145. echo $pay->success();
  146. }
  147. /**
  148. * 在线支付
  149. */
  150. public function offline()
  151. {
  152. $orderId = $this->request->get('order_id', 0);
  153. $orderId = Hashids::decodeHex($orderId);
  154. $orderModel = new \addons\unishop\model\Order();
  155. $order = $orderModel->where(['id' => $orderId])->find();
  156. if (!$order) {
  157. $this->error(__('Order does not exist'));
  158. }
  159. try {
  160. Db::startTrans();
  161. Hook::add('paid_success', 'addons\\unishop\\behavior\\Order');
  162. Hook::listen('paid_success', $order, ['pay_type' => \addons\unishop\model\Order::PAY_OFFLINE]);
  163. Db::commit();
  164. } catch (Exception $e) {
  165. Db::rollback();
  166. $this->error($e->getMessage());
  167. }
  168. $this->success('', true);
  169. }
  170. /**
  171. * 微信内H5-JSAPI支付
  172. */
  173. public function jssdkBuildConfig()
  174. {
  175. $app = Wechat::initEasyWechat('payment');
  176. $configData = $app->jssdk->buildConfig(['chooseWXPay'], false, true, false);
  177. $this->success('', $configData);
  178. }
  179. /**
  180. * 支付宝支付
  181. */
  182. public function alipay()
  183. {
  184. $orderId = $this->request->request('order_id', 0);
  185. $orderId = Hashids::decodeHex($orderId);
  186. $orderModel = new \addons\unishop\model\Order();
  187. $order = $orderModel->where(['id' => $orderId])->find();
  188. try {
  189. if (!$order) {
  190. $this->error(__('Order does not exist'));
  191. }
  192. $products = $order->products()->select();
  193. $body = Config::getByName('name')['value'];
  194. foreach ($products as $product) {
  195. $body .= '_' . $product['title'];
  196. }
  197. $params = [
  198. 'amount' => $order->total_price,
  199. 'orderid' => $order->out_trade_no,
  200. 'type' => "alipay",
  201. 'title' => $body,
  202. 'notify_url' => AliPayConfig::getByName('ali_notify_url'),
  203. 'return_url' => AliPayConfig::getByName('ali_return_url'),
  204. 'method' => "wap",
  205. ];
  206. return Service::submitOrder($params);
  207. } catch (Exception $e) {
  208. $this->error($e->getMessage());
  209. }
  210. }
  211. // public function notify(){
  212. // $order = [
  213. // "order_id"=>"202009275f7076e5c6f2f23",
  214. // "total_fee"=>11,
  215. // "memo"=>'取消订单',
  216. // "refund_fee"=>11,
  217. //
  218. // ];//order_id:订单ID name:订单名称 total_fee:总金额-元 refund_fee退款金额
  219. // AliPay::cancelOrder($order);
  220. //
  221. // }
  222. }