/** * 软件兑换金币 * * @param unknown $user_id */ public function software_to_gold($user_id, $software_id) { try { $con_characters = Yii::app()->db_characters; $trans_characters = $con_characters->beginTransaction(); $table_name = sprintf('software_%02s', dechex($user_id % 256)); $ret = $con_characters->createCommand()->select('id')->from($table_name)->where('software_id=:ID AND status=1')->bindParam(':ID', $software_id, PDO::PARAM_INT, 11)->order('id DESC')->queryRow(); if ($ret) { return -2; } else { //查询软件推荐 $software_info = Common::model()->getSoftware($software_id); if (!$software_info) { return -3; } //获取用户基本信息 $player = Characters::model()->getCharactersInfo($user_id); //金币购买体力 $param['gold'] = (int) $player['gold'] + (int) $software_info[0]['gold']; //更新 加金币 Characters::model()->updateCharacters($user_id, $param); //解锁女神 加金币日志 $gold_params = array('user_id' => $user_id, 'type' => 10, 'value' => $software_info[0]['gold'], 'gold' => $param['gold'], 'create_ts' => date("Y-m-d H:i:s")); Gold::model()->createGold($user_id, $gold_params); //软件换金币记录 $p = array('user_id' => $user_id, 'software_id' => $software_id, 'gold' => $software_info[0]['gold'], 'status' => 1); $con_characters->createCommand()->insert($table_name, $p); //提交事务 $trans_characters->commit(); //获取角色信息 $info = Characters::model()->getCharactersInfo($user_id); $return['log']['gold'] = $software_info[0]['gold']; $return['log']['gold_after'] = $param['gold']; $return['result'] = array('point' => (int) $info['point'], 'exp' => (int) $info['exp'], 'vit' => (int) $info['vit'], 'vit_time' => (int) $info['vit_time'], 'level' => (int) $info['level'], 'gold' => (int) $info['gold'], 'flowers' => (int) $info['flowers']); } } catch (Exception $e) { error_log($e); $trans_characters->rollback(); return -1; } return $return; }
public function paySuccess($data) { $con_pay = Yii::app()->db_pay; $con_characters = Yii::app()->db_characters; $now = date('Y-m-d H:i:s'); $result = 'fail'; $pay_transaction = Yii::app()->db_pay->beginTransaction(); $trans_characters = $con_characters->beginTransaction(); try { $con_pay->createCommand()->insert('pay_notify_alipay', array('notify_time' => $data['notify_time'], 'notify_type' => $data['notify_type'], 'notify_id' => $data['notify_id'], 'sign' => $data['sign'], 'out_trade_no' => $data['out_trade_no'], 'subject' => $data['subject'], 'payment_type' => $data['payment_type'], 'trade_no' => $data['trade_no'], 'trade_status' => $data['trade_status'], 'seller_id' => $data['seller_id'], 'seller_email' => $data['seller_email'], 'buyer_id' => $data['buyer_id'], 'buyer_email' => $data['buyer_email'], 'total_fee' => $data['total_fee'], 'quantity' => $data['quantity'], 'body' => $data['body'], 'gmt_create' => $data['gmt_create'], 'gmt_payment' => $data['gmt_payment'], 'create_ts' => $now)); $order_no = $data['out_trade_no']; $res = $con_pay->createCommand()->select('id, price,coin, free, uid, status')->from('orders')->where('trade_no=:Order_no', array(':Order_no' => $order_no))->queryRow(); if ($res) { if ($res['status'] == 0) { //可以支付 if ($data['trade_status'] == 'TRADE_FINISHED') { $return_money = $data['total_fee'] * 100; //验证支付金额 if ($return_money != $res['price']) { $con_pay->createCommand()->update('orders', array('notify_ts' => $now), 'id=:Order_id', array(':Order_id' => $res['id'])); // 记录Log Log::model()->_pay_log($res['uid'], 'ORDER_PAY_RETURN_MONY_ERR', $now, $order_no, $data['trade_no']); $result = 'fail'; } else { $con_pay->createCommand()->update('orders', array('status' => 1, 'charge_ts' => $now, 'notify_ts' => $now), 'id=:Order_id ', array(':Order_id' => $res['id'])); //给玩家金币 $add_coin = $res['coin'] + $res['free']; $p_info = Characters::model()->getCharactersInfo($res['uid']); Characters::model()->updateCharacters($res['uid'], array('gold' => $p_info['gold'] + $add_coin)); //充值加金币日志 $gold_params = array('user_id' => $res['uid'], 'type' => 6, 'value' => $add_coin, 'gold' => $p_info['gold'] + $add_coin, 'create_ts' => $now); Gold::model()->createGold($res['uid'], $gold_params); //添加金币log Log::model()->_gold_log($res['uid'], $add_coin, $p_info['gold'] + $add_coin, 'PAY_BUY_GOLD', $now); // 交易成功Log Log::model()->_pay_log($res['uid'], 'ORDER_PAY_RETURN_OK', $now, $order_no, $data['trade_no']); $result = 'success'; } } } else { Log::model()->_pay_log($res['uid'], 'ORDER_PAY_RETURN_REPEAT', $now, $order_no, $data['trade_no']); $result = 'success'; } } else { // 记录Log Log::model()->_pay_log($res['user_id'], 'ORDER_PAY_RETURN_SELORDER_ERR', $now, $order_no, $data['trade_no']); $result = 'fail'; } $pay_transaction->commit(); $trans_characters->commit(); } catch (Exception $e) { error_log($e); $pay_transaction->rollback(); $trans_characters->rollback(); //数据库错误返回错误。 $result = 'fail'; } return $result; }