138 lines
4.7 KiB
PHP
138 lines
4.7 KiB
PHP
<?php
|
|
namespace member\models\forms;
|
|
|
|
use common\core\BaseModel;
|
|
use common\enums\OrderAcceptEnum;
|
|
use common\events\OrderEvent;
|
|
use common\helpers\ArrayHelper;
|
|
use common\helpers\FuncHelper;
|
|
use common\jobs\OrderSubmitJob;
|
|
use common\models\Activity;
|
|
use common\models\Mall;
|
|
use common\models\oldDb\Order;
|
|
use common\models\oldDb\PaymentOrder;
|
|
use common\models\OrderCommission;
|
|
use common\models\OrderGroup;
|
|
use common\models\OrderSubmitResult;
|
|
use common\services\WechatService;
|
|
use yii\base\Exception;
|
|
|
|
class OrderSubmitResultForm extends BaseModel
|
|
{
|
|
public function rules()
|
|
{
|
|
return [
|
|
];
|
|
}
|
|
|
|
public function getPayData($order_id)
|
|
{
|
|
$order = Order::find()->where([
|
|
'id' => $order_id,
|
|
'user_id' => \Yii::$app->user->identity->id,
|
|
])->one();
|
|
if (!$order) {
|
|
throw new Exception("订单不存在");
|
|
}
|
|
if($order->is_pay){
|
|
throw new Exception("订单已支付");
|
|
}
|
|
|
|
$pay_order_no = FuncHelper::generate_order_no('PY');
|
|
$total_fee = $order['total_pay_price'];
|
|
|
|
$paymentOrder = new PaymentOrder();
|
|
$paymentOrder->order_no = $order->order_no;
|
|
$paymentOrder->pay_order_no = $pay_order_no;
|
|
$paymentOrder->amount = $order->total_pay_price;
|
|
$paymentOrder->saveOrFail();
|
|
|
|
if(bccomp($total_fee,0,2) <= 0){ //无需支付
|
|
$t = \Yii::$app->db->beginTransaction();
|
|
try {
|
|
$this->paid(['order_no' => $order->order_no]);
|
|
$t->commit();
|
|
}catch (Exception $exception){
|
|
$t->rollback();
|
|
throw $exception;
|
|
}
|
|
return [
|
|
'is_paid' => 1,
|
|
'order_id' => $order['id'],
|
|
];
|
|
}
|
|
|
|
$result = WechatService::getInstance()->payment->order->unify([
|
|
'body' => '订单号:'.$order['order_no'],
|
|
'out_trade_no' => $pay_order_no,
|
|
'total_fee' => bcmul($total_fee,100,0), // 单位:分
|
|
'notify_url' => \Yii::$app->request->hostInfo.'/member/v1/callback/notify',
|
|
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
|
|
'openid' => \Yii::$app->user->identity->openid,
|
|
]);
|
|
|
|
/**
|
|
* array (size=9)
|
|
'return_code' => string 'SUCCESS' (length=7)
|
|
'return_msg' => string 'OK' (length=2)
|
|
'result_code' => string 'SUCCESS' (length=7)
|
|
'mch_id' => string '1613782197' (length=10)
|
|
'appid' => string 'wx80c3e46b0791970f' (length=18)
|
|
'nonce_str' => string '4Dgv26sAHXYFvsT4' (length=16)
|
|
'sign' => string '1FB6605C15426426C6299AC6225F3188' (length=32)
|
|
'prepay_id' => string 'wx08185934425439f262fcd8f03d80d80000' (length=36)
|
|
'trade_type' => string 'JSAPI' (length=5)
|
|
*/
|
|
|
|
if(!isset($result['return_code']) || $result['return_code'] != 'SUCCESS'){
|
|
throw new Exception('支付失败,失败原因:'.$result['return_msg']);
|
|
}
|
|
if(!isset($result['result_code']) || $result['result_code'] != 'SUCCESS'){
|
|
throw new Exception('支付失败,错误原因:'.ArrayHelper::getValue($result,'err_code_des','未知'));
|
|
}
|
|
$config = WechatService::getInstance()->payment->jssdk->sdkConfig($result['prepay_id']); // 返回数组
|
|
$config['is_paid'] = 0;
|
|
$config['order_id'] = $order['id'];
|
|
return $config;
|
|
}
|
|
|
|
public function paid($data)
|
|
{
|
|
$transaction_id = isset($data['transaction_id']) ? $data['transaction_id'] : '';
|
|
$order = Order::findOne(['order_no'=>$data['order_no']]);
|
|
if(!$order){
|
|
throw new Exception('订单不存在');
|
|
}
|
|
$payment_order = PaymentOrder::find()->where([
|
|
'order_no' => $data['order_no'],
|
|
])->orderBy(['id'=>SORT_DESC])->one();
|
|
if(!$payment_order){
|
|
throw new Exception('支付订单不存在');
|
|
}
|
|
|
|
if($order->is_pay && $payment_order->is_pay){
|
|
return true;
|
|
}
|
|
|
|
if($order->is_pay == 1){
|
|
return true;
|
|
}
|
|
$order->is_pay = 1;
|
|
$order->pay_time = time();
|
|
$order->pay_type = isset($data['pay_type']) ? $data['pay_type'] : 0;
|
|
$order->accept_status = OrderAcceptEnum::WAIT_ACCEPT;
|
|
$order->saveOrFail();
|
|
|
|
$payment_order->transaction_id = $transaction_id;
|
|
$payment_order->is_pay = 1;
|
|
$payment_order->pay_type = $order->pay_type;
|
|
$payment_order->saveOrFail();
|
|
|
|
$event = new OrderEvent();
|
|
$event->order = $order;
|
|
$event->sender = $this;
|
|
\Yii::$app->trigger(Order::EVENT_PAYED, $event);
|
|
return true;
|
|
}
|
|
}
|