diff --git a/app/api/controller/PayController.php b/app/api/controller/PayController.php index aebcd32..576452c 100644 --- a/app/api/controller/PayController.php +++ b/app/api/controller/PayController.php @@ -31,11 +31,6 @@ class PayController extends BaseApiController $attach = $ciphertext['attach']; switch ($attach) { case 'cashierclass': - $order = Cashierclass::where(['number' => $ciphertext['out_trade_no']])->findOrEmpty(); - - if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) { - return true; - } PayNotifyLogic::handle('cashierclass', $ciphertext['out_trade_no'], $ciphertext); $app->wechat->success(); break; diff --git a/app/api/controller/order/RetailOrderController.php b/app/api/controller/order/RetailOrderController.php index eec903f..411dd09 100644 --- a/app/api/controller/order/RetailOrderController.php +++ b/app/api/controller/order/RetailOrderController.php @@ -24,35 +24,38 @@ class RetailOrderController extends BaseApiController /** * 订单列表 */ - public function order_list(){ + public function order_list() + { return $this->dataLists(new RetailOrderList()); } - public function order_count(){ - $userId=$this->request->userId; - $where=['uid'=>$userId,'paid'=>0]; - $no_pay=Cashierclass::where($where)->count(); - $where['paid']=1; - $where['status']=0; - $waiting=Cashierclass::where($where)->count(); - $where['status']=1; - $receiving=Cashierclass::where($where)->count(); - return $this->success('ok',['no_pay'=>$no_pay,'waiting'=>$waiting,'receiving'=>$receiving]); + public function order_count() + { + $userId = $this->request->userId; + $where = ['uid' => $userId, 'paid' => 0]; + $no_pay = Cashierclass::where($where)->count(); + $where['paid'] = 1; + $where['status'] = 0; + $waiting = Cashierclass::where($where)->count(); + $where['status'] = 1; + $receiving = Cashierclass::where($where)->count(); + return $this->success('ok', ['no_pay' => $no_pay, 'waiting' => $waiting, 'receiving' => $receiving]); } /** * @notes 检测零售订单 */ - public function checkOrder(){ - $cartId = (Array)$this->request->post('cart_id', []); + public function checkOrder() + { + $cartId = (array)$this->request->post('cart_id', []); $addressId = (int)$this->request->post('address_id'); - $pay_type = (int)$this->request->post('pay_type'); - $auth_code = $this->request->post('auth_code'); //微信支付条码 - $params=$this->request->post(); - $res=OrderLogic::cartIdByOrderInfo($cartId,$addressId,null,$params); - if($res==false){ - $msg=OrderLogic::getError(); - if($msg=='购物车为空'){ + // $pay_type = (int)$this->request->post('pay_type'); + // $auth_code = $this->request->post('auth_code'); //微信支付条码 + $params = $this->request->post(); + $res = OrderLogic::cartIdByOrderInfo($cartId, $addressId, null, $params); + if ($res == false) { + $msg = OrderLogic::getError(); + if ($msg == '购物车为空') { return $this->data([]); } return $this->fail(OrderLogic::getError()); @@ -63,46 +66,59 @@ class RetailOrderController extends BaseApiController /** * @notes 创建零售订单 */ - public function createOrder(){ + public function createOrder() + { // d(WeChatConfigService::getPayConfigByTerminal(1)); - $user=User::where('id',$this->request->userId)->find(); - $cartId = (Array)$this->request->post('cart_id', []); - $mer_id = (Array)$this->request->post('mer_id', 0); - if($mer_id<=0){ + $user = User::where('id', $this->request->userId)->find(); + $cartId = (array)$this->request->post('cart_id', []); + $mer_id = (array)$this->request->post('mer_id', 0); + if ($mer_id <= 0) { return $this->fail('自提点不能为空'); } - if(count($cartId)>100){ + if (count($cartId) > 100) { return $this->fail('购物车商品不能超过100个'); } $addressId = (int)$this->request->post('address_id'); $pay_type = (int)$this->request->post('pay_type'); $auth_code = $this->request->post('auth_code'); //微信支付条码 - $params=$this->request->post(); - $order=OrderLogic::createOrder($cartId,$addressId,null,$params); - if($order!=false){ - if($pay_type==PayEnum::BALANCE_PAY){ - $user=User::where('id',$this->request->userId)->find(); - $res=RetailOrderLogic::payBalance($user,$order); - if($res==true){ - $res=RetailOrderLogic::paySuccess($order, ['money'=>$order['actual']]); - if($res){ - }else{ + $params = $this->request->post(); + $order = OrderLogic::createOrder($cartId, $addressId, null, $params); + if ($order != false) { + switch ($pay_type) { + case PayEnum::WECHAT_PAY: + $user = User::where('id', $this->request->userId)->find(); + $res = RetailOrderLogic::payBalance($user, $order); + if (RetailOrderLogic::hasError()) { + $res = RetailOrderLogic::paySuccess($order, ['money' => $order['actual']]); + if (RetailOrderLogic::hasError()) { + } else { + return $this->fail(RetailOrderLogic::getError()); + } + } else { return $this->fail(RetailOrderLogic::getError()); } - }else{ - return $this->fail(RetailOrderLogic::getError()); - } - }elseif($pay_type==PayEnum::WECHAT_PAY){ - $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; - $result = PaymentLogic::pay($pay_type,'cashierclass', $order, $this->userInfo['terminal'], $redirectUrl); - if (false === $result) { - return $this->fail(PaymentLogic::getError(), $params); - } - return $this->success('', $result); + break; + case PayEnum::WECHAT_PAY: + $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; + $result = PaymentLogic::pay($pay_type, 'cashierclass', $order, $this->userInfo['terminal'], $redirectUrl); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + return $this->success('', $result); + break; + case PayEnum::WECHAT_PAY_BARCODE: + $result = PaymentLogic::codepay($auth_code, $order); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + return $this->success('', $result); + break; + default: + return $this->fail('支付方式错误'); } - return $this->data(['order_id'=>$order->id]); - }else{ + return $this->data(['order_id' => $order->id]); + } else { return $this->fail(OrderLogic::getError()); } } @@ -110,61 +126,75 @@ class RetailOrderController extends BaseApiController /** * 重新支付 */ - public function Repayment(){ + public function Repayment() + { $order_id = (int)$this->request->post('order_id'); $addressId = (int)$this->request->post('address_id'); $pay_type = (int)$this->request->post('pay_type'); - $params=$this->request->post(); - $where=[ - 'id'=>$order_id, - 'uid'=>$this->userId, - 'paid'=>0, + $auth_code = $this->request->post('auth_code'); //微信支付条码 + $params = $this->request->post(); + $where = [ + 'id' => $order_id, + 'uid' => $this->userId, + 'paid' => 0, ]; - $order=Cashierclass::where($where)->find(); - if($pay_type==PayEnum::BALANCE_PAY){ - $user=User::where('id',$this->request->userId)->find(); - $res=RetailOrderLogic::payBalance($user,$order); - if($res==true){ - $res=RetailOrderLogic::paySuccess($order, ['money'=>$order['actual']]); - if($res){ + $order = Cashierclass::where($where)->find(); + switch ($pay_type) { + case PayEnum::BALANCE_PAY: + $user = User::where('id', $this->request->userId)->find(); + $res = RetailOrderLogic::payBalance($user, $order); + if (!RetailOrderLogic::hasError()) { + $res = RetailOrderLogic::paySuccess($order, ['money' => $order['actual']]); + if(RetailOrderLogic::hasError()){ + return $this->fail(RetailOrderLogic::getError()); + } + return $this->success('余额支付成功'); }else{ return $this->fail(RetailOrderLogic::getError()); } - }else{ - return $this->fail(RetailOrderLogic::getError()); - } - }elseif($pay_type==PayEnum::WECHAT_PAY){ - $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; - if($addressId!=$order['address_id']){ - $address=UserAddress::where(['address_id'=>$addressId,'uid'=>Request()->userId])->find(); - if($address){ - $_order['real_name'] = $address['real_name']; - $_order['user_phone'] = $address['phone']; - $_order['user_address'] = $address['detail']; - $_order['address_id'] = $addressId; - Cashierclass::where(['id'=>$order_id,'uid'=>Request()->userId])->update($_order); + break; + case PayEnum::WECHAT_PAY: + $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; + if ($addressId != $order['address_id']) { + $address = UserAddress::where(['address_id' => $addressId, 'uid' => Request()->userId])->find(); + if ($address) { + $_order['real_name'] = $address['real_name']; + $_order['user_phone'] = $address['phone']; + $_order['user_address'] = $address['detail']; + $_order['address_id'] = $addressId; + Cashierclass::where(['id' => $order_id, 'uid' => Request()->userId])->update($_order); + } } - } - $result = PaymentLogic::pay($pay_type,'cashierclass', $order, $this->userInfo['terminal'], $redirectUrl); - if (false === $result) { - return $this->fail(PaymentLogic::getError()); - } - return $this->success('', $result); + $result = PaymentLogic::pay($pay_type, 'cashierclass', $order, $this->userInfo['terminal'], $redirectUrl); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError()); + } + return $this->success('', $result); + break; + case PayEnum::WECHAT_PAY_BARCODE: + $result = PaymentLogic::codepay($auth_code, $order); + if (PaymentLogic::hasError()) { + return $this->fail(PaymentLogic::getError(), $params); + } + return $this->success('', $result); + break; + default: + return $this->fail('支付方式错误'); } return $this->fail('支付失败'); - } - public function detail(){ + public function detail() + { $order_id = (int)$this->request->get('order_id'); - $where=[ - 'id'=>$order_id, - 'uid'=>$this->userId, + $where = [ + 'id' => $order_id, + 'uid' => $this->userId, ]; - $order=CashierclassLogic::detail($where); - if($order){ + $order = CashierclassLogic::detail($where); + if ($order) { return $this->data($order); - }else{ + } else { return $this->fail('订单不存在'); } } @@ -172,30 +202,32 @@ class RetailOrderController extends BaseApiController /** * 获取用户常用购买记录 */ - public function frequently_purchase(){ - $params=$this->request->get(); - $res=RetailOrderLogic::frequentlyPurchase($params); - if(RetailOrderLogic::hasError()){ + public function frequently_purchase() + { + $params = $this->request->get(); + $res = RetailOrderLogic::frequentlyPurchase($params); + if (RetailOrderLogic::hasError()) { return $this->fail(RetailOrderLogic::getError()); - }else{ + } else { return $this->data($res); } } - + /** * 取消订单 */ - public function cancel_order(){ + public function cancel_order() + { $order_id = (int)$this->request->post('order_id'); $value = (int)$this->request->post('value'); - $where=[ - 'id'=>$order_id, - 'uid'=>$this->userId, - 'paid'=>0, + $where = [ + 'id' => $order_id, + 'uid' => $this->userId, + 'paid' => 0, ]; - $order=Cashierclass::where($where)->find(); - if($order){ - $data=['data'=>$value,'delete_time'=>time()]; + $order = Cashierclass::where($where)->find(); + if ($order) { + $data = ['data' => $value, 'delete_time' => time()]; Cashierclass::where($where)->update($data); return $this->success('取消成功'); } @@ -205,36 +237,36 @@ class RetailOrderController extends BaseApiController /** * 确认收货 */ - public function confirm_receipt(){ + public function confirm_receipt() + { $order_id = (int)$this->request->post('order_id'); - $where=[ - 'id'=>$order_id, - 'uid'=>$this->userId, - 'paid'=>1, - 'status'=>1, + $where = [ + 'id' => $order_id, + 'uid' => $this->userId, + 'paid' => 1, + 'status' => 1, ]; - $order=Cashierclass::where($where)->find(); - if($order){ - $data=['status'=>2]; + $order = Cashierclass::where($where)->find(); + if ($order) { + $data = ['status' => 2]; Cashierclass::where($where)->update($data); return $this->success('确认成功'); } return $this->fail('确认失败'); - } /** * 再次购买 */ - public function purchase_again(){ + public function purchase_again() + { $order_id = (int)$this->request->get('order_id'); - $res=RetailOrderLogic::purchaseAgain($order_id); - if($res==false){ + $res = RetailOrderLogic::purchaseAgain($order_id); + if ($res == false) { return $this->fail('添加失败'); - }else{ + } else { return $this->success('添加成功'); } - } -} \ No newline at end of file +} diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 15c40ca..838c77f 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -50,8 +50,11 @@ class PayNotifyLogic extends BaseLogic */ public static function cashierclass($orderSn, $extra = []) { - $order = Cashierclass::where('number', $orderSn)->findOrEmpty(); + + if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) { + return true; + } $order->money = bcdiv($extra['amount']['payer_total'],100,2); $order->paid = 1; $order->status = 1; diff --git a/app/common/logic/PaymentLogic.php b/app/common/logic/PaymentLogic.php index df954e9..86498e7 100644 --- a/app/common/logic/PaymentLogic.php +++ b/app/common/logic/PaymentLogic.php @@ -50,7 +50,7 @@ class PaymentLogic extends BaseLogic ], 'attach' => $from ]; - $wechat=new PayService(1); + $wechat = new PayService(1); $result = $wechat->wechat->mini($order)->toArray(); break; default: @@ -59,4 +59,40 @@ class PaymentLogic extends BaseLogic } return $result; } + + /** + * 微信条码支付 + */ + public static function codepay($auth_code, $order) + { + $pattern = '/^(10|11|12|13|14|15)\d{16}$/'; + + if (!preg_match($pattern, (string)$auth_code)) { + self::$error = '请使用正确的微信收付款条码'; + return false; + } + $order = [ + 'description' => '条码商品', + 'out_trade_no' => $order['number'], + 'payer' => [ + 'auth_code' => $auth_code + ], + 'amount' => [ + 'total' => intval($order['actual'] * 100), + ], + 'scene_info' => [ + "store_info" => [ + 'id' => $order['mer_id'] + ] + ], + ]; + $wechat = new PayService(1); + try { + $result = $wechat->wechat->pot($order)->toArray(); + } catch (\Exception $e) { + self::$error = $e->getMessage(); + return false; + } + return $result; + } } diff --git a/config/payment.php b/config/payment.php index d855b9a..76a9d2c 100644 --- a/config/payment.php +++ b/config/payment.php @@ -9,9 +9,9 @@ return [ 'alipay' => [ 'default' => [ // 必填-支付宝分配的 app_id - 'app_id' => '20160909004708941', + 'app_id' => '2021004140622055', // 必填-应用私钥 字符串或路径 - 'app_secret_cert' => 'MIIEpAIBAAKCxxxxxxxxxxxxxxP4r3m4OUmD/+XDgCg==', + 'app_secret_cert' => 'MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDJBRXcWRsuH9h+FC7annXOqroHkkLNQhAbMqyKEG4S6v5d8W/h+7icuL7FtoeBn6PFXwrOqvb6dgARriMooJxTsxZmCqyrfB9C9JpMUhvfuvVOw2kZm34b642ovgsAfyO0Bs7+s8iakg2twdPXG1+LhLQZQI0Mm8G1buN60JnieyyWINgnzbDDRjrqBSmjGF+xjoy6zXV4LkthMPGx/KAXXm3OAb/eqhR2b3B1uv4TCFWr9ILGaU8H0cgOXOguznQrcBDJOjPLUZkpcDuxsyqzSaUnUfuw30HA9pQO8ssjxaVjdiOOH/cftQZJD4lTXub/vvi6hwzSHz9UAOIPDl4ZAgMBAAECggEAQxfxbfRHwCNc7Ofro4hUJcCSCsjkhwHPwmQtdn95XdMVR2G4SCLuQVDgS7lajQctcEkCoGwrtvj/mOcYUQ0aYE1b4n8Zlrr87Cob/fFB4HDUQ4+xWpytgMAkLfgQUAAfeIz1Ncnn4L9y1VXmUNuQDk8ilXMYIOczxkzWnD1Xv2FpBFEJRYcIuIFQbAU7HicMuYYf6tqm8FF5epRioci19qujlYg7wNZs7bgxVonIbT9aVhI173c47jrJ2j0S5LGK0IgALnLQE76cK4jvH+YSVUbfnsfz8QEmWGNofo7XWgGE3qfTdc+STzpg9m0xs5pjeU3nhk5efnEkZkLG8wOuiQKBgQD7zxNouoI8+NRAP9tUEg3OlW3sufx6Tn+IPeftzZNK0RPL/zqzbJ4BPZBtS9sdTUFQKipY9MQSxx7xG7o0o9NZUZ7LxDCCJMcMfCQ8XxWl2j7L/lpk4kR2b/r5gep3m3enWIpOOqKujMUaZGulMf8pQ4bXoXl87tlHwV8NQ89PkwKBgQDMXZqzNNWAq4BFFSjudIekdefqusbNNDX8UenCQfPhfQtuRpNZ9Mudk/UMloQEvJrDn6Z9yGjdWDnHXsmXFT1aElvUVYuqaU0PluI24XPyHkn3Q+PTR2rvWEPlneqlxiKb/VZPNaI3ziv1Jx0XB+IIu/IWJBorQKWw5CrpHQuvIwKBgQCJhX5EJBgc1IiWzkkqkPGZcWKndqu84EImVijkZvGaWrVFJA8bDqb8ZnMKPfNc/WQ+M8kM7CBpYPY1F7+KIC69CSaAgpU7fnp3BxWDIyrfeWHyZudXUXuyc1XODnXO/MlMS769GqjeS2MaFt3zSmrgTlDGUxnWcVuZJLKUvAPsoQKBgQC20J1M5lx/Z+us4Mrm1TNp+VdncYHZm3UXrCcScXE7K2MIWPYDqKpk4jB5/ENJAXbovG5xYs/8AapeIPrwvj1Nz9J51bTE0sHuyii+s1vHITVFjmPFYsPUK/9A7s0zUlUveaa14OKmTTahbUXmjlSzP33GNLF8M66E2PdR5XvIRQKBgQCig9ffDup8An/Gyt//mFySiKW014EWQmetyPtecb2h7qknD2G4Vy3hvG7aJeEbaqEve2Y13JP4yQhtcgoPAX0GTzRc4Fd4MuZmWWlZYlZfgCQOJy5zEwTidjPMrEOsTvVQvWOOLuFS+PigVK7fz/YEsPXx61zkGPcuwQvNhQtOLA==', // 必填-应用公钥证书 路径 'app_public_cert_path' => base_path() . '/payment/appCertPublicKey_2016090900470841.crt', // 必填-支付宝公钥证书 路径 @@ -19,13 +19,13 @@ return [ // 必填-支付宝根证书 路径 'alipay_root_cert_path' => base_path() . '/payment/alipayRootCert.crt', // 选填-同步回调地址 - 'return_url' => 'https://webman.tinywan.cn/payment/alipay-return', + 'return_url' => 'https://erp.lihaink.cn/api/pay/alipay-return', // 选填-异步回调地址 - 'notify_url' => 'https://webman.tinywan.cn/payment/alipay-notify', + 'notify_url' => 'https://erp.lihaink.cn/api/pay/alipay-notify', // 选填-服务商模式下的服务商 id,当 mode 为 Pay::MODE_SERVICE 时使用该参数 'service_provider_id' => '', // 选填-默认为正常模式。可选为: MODE_NORMAL, MODE_SANDBOX, MODE_SERVICE - 'mode' => \Yansongda\Pay\Pay::MODE_SANDBOX, + 'mode' => \Yansongda\Pay\Pay::MODE_NORMAL, ] ], 'wechat' => [