17 changed files with 400 additions and 184 deletions
			
			
		- 
					31MySQL_change.sql
 - 
					15app/Admin/Controllers/SupplierController.php
 - 
					2app/Admin/Extensions/Grid/AuditIndustryProduct.php
 - 
					3app/Admin/Forms/Setting.php
 - 
					2app/AdminAgent/Controllers/IndustryOrderController.php
 - 
					110app/AdminSettled/Controllers/SupplierController.php
 - 
					2app/AdminSettled/routes.php
 - 
					44app/AdminSupplier/Controllers/ProductController.php
 - 
					2app/Http/Controllers/Api/AgentProductController.php
 - 
					9app/Http/Controllers/Api/OrderController.php
 - 
					4app/Http/Controllers/Api/TestController.php
 - 
					199app/Http/Controllers/Api/WxpayController.php
 - 
					133app/Http/Controllers/IndustryProductWxpay.php
 - 
					13app/Models/SettledOrder.php
 - 
					5app/Models/Supplier.php
 - 
					1config/admin-settled.php
 - 
					9routes/api.php
 
@ -1,133 +0,0 @@ | 
				
			|||
<?php | 
				
			|||
 | 
				
			|||
namespace App\Http\Controllers; | 
				
			|||
 | 
				
			|||
use App\Common\OrderStatus; | 
				
			|||
use App\Common\PayType; | 
				
			|||
use App\Models\AdminSetting; | 
				
			|||
use App\Models\IndustryOrder; | 
				
			|||
use App\Models\IndustryPayLog; | 
				
			|||
use App\Models\IndustryProduct; | 
				
			|||
use EasyWeChat\Factory; | 
				
			|||
use EasyWeChat\Kernel\Exceptions\Exception; | 
				
			|||
use EasyWeChat\Payment\Kernel\Exceptions\InvalidSignException; | 
				
			|||
use Illuminate\Support\Facades\DB; | 
				
			|||
use Illuminate\Support\Facades\Log; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * 行业产品微信支付回调 | 
				
			|||
 * Class IndustryProductWxpay | 
				
			|||
 * @package App\Http\Controllers | 
				
			|||
 */ | 
				
			|||
class IndustryProductWxpay | 
				
			|||
{ | 
				
			|||
	public function notify() | 
				
			|||
	{ | 
				
			|||
		$setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']); | 
				
			|||
		if (!isset($setting['payee_appid'], $setting['payee_mchid'], $setting['payee_mchkey'])) { | 
				
			|||
			return '获取系统配置失败'; | 
				
			|||
		} | 
				
			|||
 | 
				
			|||
		$config = [ | 
				
			|||
			'app_id' => $setting['payee_appid'], | 
				
			|||
			'mch_id' => $setting['payee_mchid'], | 
				
			|||
			'key' => $setting['payee_mchkey'], | 
				
			|||
		]; | 
				
			|||
 | 
				
			|||
		$app = Factory::payment($config); | 
				
			|||
		try { | 
				
			|||
			$response = $app->handlePaidNotify(function ($message, $fail) { | 
				
			|||
				//仅测试用,回调记录
 | 
				
			|||
				DB::table('pay_debugs')->insert(['agent_id' => 0, 'type' => 1, 'content' => json_encode($message)]); | 
				
			|||
 | 
				
			|||
				// 请求成功
 | 
				
			|||
				if ($message['return_code'] === 'SUCCESS') { | 
				
			|||
					//主要是为了区分定金支付和尾款支付,订单号带有-status后缀,分割后前面才是真正的订单号
 | 
				
			|||
					$order_no = explode('-', $message['out_trade_no'])[0]; | 
				
			|||
					$order = IndustryOrder::query() | 
				
			|||
						->where(['order_no' => $order_no]) | 
				
			|||
						->first(); | 
				
			|||
 | 
				
			|||
					//已经处理过的订单直接返回true
 | 
				
			|||
					if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::SUCCESS])) { | 
				
			|||
						return true; | 
				
			|||
					} | 
				
			|||
 | 
				
			|||
					//判断该微信支付订单号有没有处理过
 | 
				
			|||
					$exist_log = IndustryPayLog::where([ | 
				
			|||
						'agent_id' => $order->agent_id, | 
				
			|||
						'supplier_id' => $order->supplier_id, | 
				
			|||
						'industry_order_id' => $order->id, | 
				
			|||
						'type' => 1, | 
				
			|||
						'transaction_id' => $message['transaction_id'], | 
				
			|||
					])->first(); | 
				
			|||
					if ($exist_log) { | 
				
			|||
						return true; | 
				
			|||
					} | 
				
			|||
 | 
				
			|||
					// 支付成功
 | 
				
			|||
					if ($message['result_code'] === 'SUCCESS') { | 
				
			|||
						DB::beginTransaction(); | 
				
			|||
						try { | 
				
			|||
							//增加销量,库存在拍下时已经减了
 | 
				
			|||
							IndustryProduct::query() | 
				
			|||
								->where('id', $order->industry_product_id) | 
				
			|||
								->increment('sale', $order->num); | 
				
			|||
 | 
				
			|||
							$old_status = $order->status; | 
				
			|||
							$pay_type = $order->pay_type; | 
				
			|||
							$money = $message['total_fee'] / 100; | 
				
			|||
							//定金支付和首付款支付
 | 
				
			|||
							if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) { | 
				
			|||
								if ($old_status == OrderStatus::UNPAID) { | 
				
			|||
									$order->status = OrderStatus::PAY_EARNEST; | 
				
			|||
								} else if ($old_status == OrderStatus::PAY_EARNEST) { | 
				
			|||
									$order->status = OrderStatus::PAID_RETAINAGE; | 
				
			|||
									$order->verify_code = uniqid(); //生成核销码
 | 
				
			|||
								} | 
				
			|||
							} else if ($pay_type == PayType::ONLINE) { | 
				
			|||
								$order->status = OrderStatus::PAID; | 
				
			|||
								$order->verify_code = uniqid(); //生成核销码
 | 
				
			|||
							} | 
				
			|||
 | 
				
			|||
							$order->paid_at = now(); | 
				
			|||
							$order->paid_money = DB::raw('`paid_money` + ' . $money); | 
				
			|||
							$order->timeout = null; | 
				
			|||
							$order->save(); | 
				
			|||
 | 
				
			|||
							//资金流水
 | 
				
			|||
							IndustryPayLog::create([ | 
				
			|||
								'agent_id' => $order->agent_id, | 
				
			|||
								'supplier_id' => $order->supplier_id, | 
				
			|||
								'money' => $money, | 
				
			|||
								'industry_order_id' => $order->id, | 
				
			|||
								'type' => 1, | 
				
			|||
								'desc' => DB::raw("LEFT('购买产品:{$order->title}', 250)"), | 
				
			|||
								'transaction_id' => $message['transaction_id'], //微信支付订单号
 | 
				
			|||
								'created_at' => now(), //模型没有updated_at,无法自动写入时间
 | 
				
			|||
								'out_trade_no' => $message['out_trade_no'] ?? '', | 
				
			|||
							]); | 
				
			|||
 | 
				
			|||
							DB::commit(); | 
				
			|||
							return true; | 
				
			|||
						} catch (Exception $e) { | 
				
			|||
							DB::rollBack(); | 
				
			|||
							$fail('Unknown error'); | 
				
			|||
						} | 
				
			|||
					} // 支付失败
 | 
				
			|||
					else if ($message['result_code'] === 'FAIL') { | 
				
			|||
						return true; | 
				
			|||
					} | 
				
			|||
				} | 
				
			|||
 | 
				
			|||
				// 希望微信重试
 | 
				
			|||
				$fail('Unknown error 2'); | 
				
			|||
			}); | 
				
			|||
		} catch (InvalidSignException | Exception $e) { | 
				
			|||
			LOG::debug('行业产品支付', [$e->getFile(), $e->getLine(), $e->getMessage()]); | 
				
			|||
			return 'error'; | 
				
			|||
		} | 
				
			|||
 | 
				
			|||
		return $response; | 
				
			|||
	} | 
				
			|||
} | 
				
			|||
@ -0,0 +1,13 @@ | 
				
			|||
<?php | 
				
			|||
 | 
				
			|||
namespace App\Models; | 
				
			|||
 | 
				
			|||
use Illuminate\Database\Eloquent\Factories\HasFactory; | 
				
			|||
use Illuminate\Database\Eloquent\Model; | 
				
			|||
 | 
				
			|||
class SettledOrder extends Model | 
				
			|||
{ | 
				
			|||
    use HasFactory; | 
				
			|||
 | 
				
			|||
//	protected $guarded = ['id'];
 | 
				
			|||
} | 
				
			|||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue