最新消息:文章中包含代码时,请遵守代码高亮规范!

微信支付问题总结【原创】

PHP ada 410浏览 0评论

下图是在微信公众平台配置信息

(支付授权目录要写支付页面的路由,支付页面的左斜杠支付页面结束的路由)

(文件放在web下)

在PayController.php写入要操作的对象,以及调用回调函数,该页面有两个action

<?php
/**
 * Created by PhpStorm.
 * User: zmit
 * Date: 5/24/17
 * Time: 6:52 PM
 */
namespace BaseBundle\Controller;

use BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

class PayController extends BaseController
{
 /**
 * WX——jaApipay
 *
 * @param $info
 * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
 */
 public function wxPayJsApiAction($info)
 {
// $info = id-1-key_word
 $info = explode('-', $info);

 if(count($info) >= 4){
 return $this->setError('数据格式不正确');
 }

 $conn = $this->get('database_connection');
 if($info[2] == 3){//收藏视频
 $order_info = $conn->fetchAssoc("SELECT id, price, type_id FROM video WHERE id = ?", array($info[0]));

 if(empty($order_info)){
 return $this->setError('订单号不存在');
 }

 $attach = 1;

 $amount = round($order_info['price'],2)*100;

 //拼接数字 用户id,会员类型id/或者视频id,时长,支付方式/视频收入类型, 支付类型4,当前时间
 $order_number = $info[1].'-'.$info[0].'-'.$info[0].'-2-3-'.date('YmdHis');

 $go_url = $this->generateUrl('user_video', array('video_type_id' => $order_info['type_id'], 'video_id' => $order_info['id']));
 }elseif($info[1] == 4){//升级vip

 $order_info = $conn->fetchAssoc("SELECT u.id, u.vip_type, u.vip_end_date, vp.keep, vp.price, vp.sale_price FROM user u LEFT JOIN vip_type vp ON u.vip_type = vp.id WHERE u.id = ?", array($info[0]));

 if(empty($order_info)){
 return $this->setError('订单号不存在');
 }

 if ($order_info['sale_price'] > 0) {
 $vip_price = $order_info['sale_price'];
 } else {
 $vip_price = $order_info['price'];
 }

 $vip_end_date = $order_info['vip_end_date'];
 $keep = $order_info['keep'];
 if( $vip_end_date == "0000-00-00 00:00:00" || $vip_end_date ==''){
 $vip_end_date = date('Y-m-d H:i:s');
 }
 if(strtotime($vip_end_date) >= time()){
 $vip_end_date = $vip_end_date ;
 }else{
 $vip_end_date = date('Y-m-d H:i:s');
 }

 $vip_end_date = date('Y-m-d H:i:s',strtotime("$vip_end_date +$keep month"));

 $attach = $vip_end_date;

 $amount = round($vip_price,2)*100;
 //拼接数字 用户id,会员类型id/或者视频,时长,支付方式/视频收入类型, 支付类型4,当前时间
 $order_number = $order_info['id'].'-'.$order_info['vip_type'].'-'.$order_info['keep'].'-1-4-'.date('YmdHis');
 $type_id = 1;
 $go_url = $this->generateUrl('user_personal_vip_success');
 }else{
 return $this->showErrorWeb('来源不正确');
 }

 //执行统一下单方法
 ini_set('date.timezone','Asia/Shanghai');

 require_once (dirname(dirname(__FILE__)) . "/wxJsSweep/example/config.php");//导入自己写入的config文件(便于引用)

 //初始化日志

 $logHandler= new \CLogFileHandler(dirname(dirname(__FILE__)) ."/wxJsSweep/logs/".date('Y-m-d').'.log');

 $log = \Log::Init($logHandler, 15);

 //①、获取用户openid

 $tools = new \JsApiPay();

 $openId = $tools->GetOpenid($this->getUrlCurrent());

 //②、统一下单
 $request = $this->get('request');
 $input = new \WxPayUnifiedOrder();

 $input->SetBody("梦航教育视频网站-付款");

 $input->SetOut_trade_no($order_number);
 $input->SetAttach($attach);//附加数据
 $input->SetTotal_fee($amount);
 $input->SetTime_start(date('YmdHis', time()));
 $input->SetTime_expire(date('YmdHis', time() + 600));
 $input->SetNotify_url($request->getSchemeAndHttpHost() . $this->generateUrl('public_pay_wxPayJsApiNotify'));
 $input->SetTrade_type("JSAPI");
 $input->SetOpenid($openId);
 $order = \WxPayApi::unifiedOrder($input);
 if($order['return_code'] == 'SUCCESS' AND $order['result_code'] == 'SUCCESS') {
 $data = array(
 'info' => $tools->GetJsApiParameters($order),
 'go_url' => $go_url
 );

 return $this->render('BaseBundle:Default:pay.html.twig',array('data' => $data));
 }else{
 if(isset($order['err_code_des'])){
 return $this->showErrorWeb($order['err_code_des']);
 }else{
 return $this->showErrorWeb($order['return_msg']);
 }
 }

 }

 /**
 * WX——jaApipay-回调
 *
 * @return JsonResponse
 */
 public function wxPayJsApiNotifyAction()
 {
 require_once dirname(dirname(__FILE__)) . "/wxJsSweep/example/notify.php";//导入回调文件
 //实例化回调
 \Log::DEBUG("begin notify");
 $notify = new \PayNotifyCallBack();
 $notify->Handle(false);
 $callback = $notify->GetValues();
 if($callback['return_code'] == 'SUCCESS') {
 $order_no = $callback['out_trade_no'];//得到之前存入的订单号
 $info = explode('-', $order_no);
 $user_id = $info[0];
 $video_or_vip = $info[1];
 $keep = $info[2];
 $pay_or_type = $info[3];

 $attach = $callback['attach'];//得到之前到期时间
 $amount = $callback['total_fee']/100;

 $payment_no = $callback['transaction_id'];

 //内部代码
 $conn = $this->get('database_connection');
 if($info[4] == 1){
 $conn->insert('finance', array('user_id' => $user_id, 'type_id' => $pay_or_type, 'source' => $video_or_vip, 'payment_no' => $payment_no, 'add_date' => date('Y-m-d H:i:s'), 'amount' => $amount));
 }elseif($info[4] == 2){
 $conn->insert('finance', array('user_id' => $user_id, 'type_id' => $pay_or_type, 'source' => $video_or_vip, 'payment_no' => $payment_no, 'add_date' => date('Y-m-d H:i:s'), 'amount' => $amount));
 }elseif($info[4] == 3){
 $conn->insert('finance', array('user_id' => $user_id, 'type_id' => $pay_or_type, 'source' => $video_or_vip, 'payment_no' => $payment_no, 'add_date' => date('Y-m-d H:i:s'), 'amount' => $amount));
 }elseif($info[4] == 4) {
 $conn->update('user', array('vip_end_date' => $attach), array('id' => $user_id));
 $conn->insert('vip_record', array('user_id' => $user_id, 'vip_type_id' => $video_or_vip, 'vip_price' => $amount, 'vip_keep' => $keep, 'add_date' => date('Y-m-d H:i:s'), 'pay_method' => $pay_or_type));
 $conn->insert('finance', array('user_id' => $user_id, 'type_id' => $pay_or_type, 'source' => $video_or_vip, 'payment_no' => $payment_no, 'add_date' => date('Y-m-d H:i:s'), 'amount' => $amount));
 }
 }
 return new JsonResponse();
 }

}
 

在实际需要支付的时候,调用函数:


$pay = (int)$vip_type."-".(int)$user_id."-3";
 return $this->redirect($this->generateUrl('public_pay_wxPayJsApi', array('info' => $pay)));
 

出现的问题

  1. 配置信息填写有误,导致参数获得有误
    1. key
    2. APPID
    3. MCHID
    4. APPSERECT
  2. 证书替换有误

3.支付授权目录填写有误

 

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/21473.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者ada的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!