176 lines
6.2 KiB
PHP
176 lines
6.2 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\models\oldDb\ChineseRepice;
|
|
use common\models\oldDb\PaymentPrescripOrder;
|
|
use common\models\oldDb\PrescripOrderLog;
|
|
use common\models\oldDb\Prescription;
|
|
use common\models\oldDb\ProductOrder;
|
|
use common\models\oldDb\WestRepice;
|
|
use common\services\WechatService;
|
|
use yii\base\Exception;
|
|
|
|
//处方form
|
|
class PrescripOrderSubmitForm extends BaseModel
|
|
{
|
|
public function rules()
|
|
{
|
|
return [
|
|
];
|
|
}
|
|
|
|
|
|
public function getPayData($order_id)
|
|
{
|
|
$prescription = Prescription::find()->where([
|
|
'id' => $order_id,
|
|
])->one();
|
|
|
|
if (!$prescription ) {
|
|
throw new Exception("订单不存在");
|
|
}
|
|
|
|
if($prescription->is_pay){
|
|
throw new Exception("订单已支付");
|
|
}
|
|
|
|
$pay_order_no = FuncHelper::generate_order_no('PY');
|
|
$total_fee = $prescription['total_pay_price'];
|
|
|
|
$paymentPrescripOrder = new PaymentPrescripOrder();
|
|
$paymentPrescripOrder->order_no = $prescription->prescription_no;
|
|
$paymentPrescripOrder->pay_order_no = $pay_order_no;
|
|
$paymentPrescripOrder->amount = $prescription->total_pay_price;
|
|
$paymentPrescripOrder->saveOrFail();
|
|
|
|
if(bccomp($total_fee,0,2) <= 0){ //无需支付
|
|
$t = \Yii::$app->db->beginTransaction();
|
|
try {
|
|
$this->paid(['order_no' => $prescription->prescription_no]);
|
|
$t->commit();
|
|
}catch (Exception $exception){
|
|
$t->rollback();
|
|
throw $exception;
|
|
}
|
|
return [
|
|
'is_paid' => 1,
|
|
'order_id' => $prescription['id'],
|
|
];
|
|
}
|
|
$result = WechatService::getInstance()->payment->order->unify([
|
|
'body' => '订单号:'.$prescription['prescription_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'] = $prescription['id'];
|
|
return $config;
|
|
}
|
|
|
|
public function paid($data)
|
|
{
|
|
$transaction_id = isset($data['transaction_id']) ? $data['transaction_id'] : '';
|
|
$prescription = Prescription::findOne(['prescription_no'=>$data['prescription_no']]);
|
|
if(!$prescription){
|
|
throw new Exception('处方订单不存在');
|
|
}
|
|
$paymentPrescrip_Order = PaymentPrescripOrder::find()->where([
|
|
'order_no' => $data['prescription_no'],
|
|
])->orderBy(['id'=>SORT_DESC])->one();
|
|
if(!$paymentPrescrip_Order){
|
|
throw new Exception('支付订单不存在');
|
|
}
|
|
|
|
if($prescription->is_pay && $paymentPrescrip_Order->is_pay){
|
|
return true;
|
|
}
|
|
|
|
if($prescription->is_pay == 1){
|
|
return true;
|
|
}
|
|
$prescription->is_pay = 1;
|
|
$prescription->pay_time = time();
|
|
$prescription->pay_type = isset($data['pay_type']) ? $data['pay_type'] : 0;
|
|
$prescription->accept_status = OrderAcceptEnum::WAIT_ACCEPT;
|
|
$prescription->saveOrFail();
|
|
|
|
$paymentPrescrip_Order->transaction_id = $transaction_id;
|
|
$paymentPrescrip_Order->is_pay = 1;
|
|
$paymentPrescrip_Order->pay_type = $prescription->pay_type;
|
|
$paymentPrescrip_Order->saveOrFail();
|
|
|
|
|
|
PrescripOrderLog::saveLog($prescription->id,'触发处方订单支付事件');
|
|
$event = new OrderEvent();
|
|
$event->order = $prescription;
|
|
$event->sender = $this;
|
|
\Yii::$app->trigger(Prescription::EVENT_PAYED, $event);
|
|
return true;
|
|
}
|
|
|
|
//预览
|
|
public function Preview($id)
|
|
{
|
|
$p_id = ProductOrder::find()->select('p_id')->where([
|
|
'id' => $id,
|
|
])->column();
|
|
|
|
$prescription = Prescription::find()->select('cr_ids,wr_ids')->where(['id' => $p_id])->one();
|
|
|
|
$wr_ids = explode(',', $prescription['wr_ids']);
|
|
$cr_ids = explode(',', $prescription['cr_ids']);
|
|
$west = WestRepice::find()->select('content,number,total_price')->where([
|
|
'in', 'id', $wr_ids
|
|
])->asArray()->all();
|
|
$chinese = ChineseRepice::find()->select('content,total_price')->where([
|
|
'in', 'id', $cr_ids
|
|
])->asArray()->all();
|
|
|
|
foreach ($west as $value) {
|
|
$number += $value['number'];
|
|
$price += $value['total_price'];
|
|
}
|
|
|
|
foreach ($chinese as $val) {
|
|
$number += $val['number'];
|
|
$prices += $val['total_price'];
|
|
}
|
|
|
|
if (!empty($price)) {
|
|
ProductOrder::updateAll(['total_pay_price' => $price], ['id' => $id]);
|
|
}
|
|
if (!empty($prices)) {
|
|
ProductOrder::updateAll(['total_pay_price' => $prices], ['id' =>$id]);
|
|
}
|
|
}
|
|
} |