扣丁学堂PHP培训之微信支付功能示例详解

2019-03-26 13:39:36 217浏览

今天扣丁学堂PHP培训老师给大家介绍一下关于PHP微信支付功能的详细介绍,并结合实例形式分析了php微信支付功能相关sdk与使用技巧。



微信开发SDK:文中用的是php_sdk_v3.0.9:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

import("wxpay.lib.AppPay#Config", EXTEND_PATH, ".php");
import("wxpay.lib.WxPay#Api", EXTEND_PATH, ".php");
//$this->weixinpay();
$config = new \AppPayConfig();
//订单号
$appId = $config->GetAppId();
$key = $config->GetKey();
$money = 0.01 * 100;
$orderSn = 'M201810241754107557';//订单号;
$curTime = time();
$input = new \WxPayUnifiedOrder();
$input->SetBody("用户红包");
$input->SetOut_trade_no($orderSn);
$input->SetTotal_fee($money);
$input->SetTrade_type("APP");
$unifiedOrder = \WxPayApi::unifiedOrder($config, $input);
if ($unifiedOrder['result_code'] == 'SUCCESS' && $unifiedOrder['return_code'] == 'SUCCESS') {
  /*$unifiedOrder 组成:
   * ["appid"] => string(18) "wx2xxxx49"
  ["mch_id"] => string(10) "1xxxxxx01"
  ["nonce_str"] => string(16) "1GnEUXTuTcFtJVtb"
  ["prepay_id"] => string(36) "wx241933xxx68fa24ece70611692955"
  ["result_code"] => string(7) "SUCCESS"
  ["return_code"] => string(7) "SUCCESS"
  ["return_msg"] => string(2) "OK"
  ["sign"] => string(32) "7903xxxxx4710E460CB156"
  ["trade_type"] => string(3) "APP"*/
  $curTime = time();
  // 第一部分
  $wxpayResult = new \WxPayResults();
  $wxpayResult->SetData('appid', $unifiedOrder['appid']);
  $wxpayResult->SetData('partnerid', $unifiedOrder['mch_id']);
  $wxpayResult->SetData('prepayid', $unifiedOrder['prepay_id']);
  $wxpayResult->SetData('noncestr', $unifiedOrder['nonce_str']);
  $wxpayResult->SetData('timestamp',$curTime);
  $wxpayResult->SetData('package',"Sign=WXPay");
  $wxpayResult->SetData('sign',"");
  $wxpayResult->SetSign($config);
  $data = $wxpayResult->GetValues();
  // 第二部分
  $data['appid'] = $unifiedOrder['appid'];
  $data['partnerid'] = $unifiedOrder['mch_id'];
  $data['prepayid'] = $unifiedOrder['prepay_id'];
  $data['noncestr'] = $unifiedOrder['nonce_str'];;
  $data['timestamp'] = $curTime;
  $data['package'] = "Sign=WXPay";
  $data['sign'] = $this->wxappPaySign($key, $data);//$unifiedOrder['sign'];//签名,具体签名方案参见微信公众号支付帮助文档;
  $this->returnSuccess($data);
} else {
  Log::write(var_export($unifiedOrder, true));
  $this->returnError(300, '微信支付失败');
}
private function wxappPaySign($key,$parameters)
{
  $sign = md5(sprintf("appid=%s&noncestr=%s&package=%s&partnerid=%s&prepayid=%s×tamp=%s&key=%s",
    $parameters['appid'],
    $parameters['noncestr'],
    $parameters['package'],
    $parameters['partnerid'],
    $parameters['prepayid'],
    $parameters['timestamp'],
    $key));
  return strtoupper($sign);
}

以上就是关于扣丁学堂PHP培训之微信支付功能示例的详细介绍,

想要了解更多关于PHP开发方面内容的小伙伴,请关注扣丁学堂PHP培训官网、微信等平台,扣丁学堂IT职业在线学习教育有专业的PHP讲师为您指导,此外扣丁学堂老师精心推出的PHP视频教程定能让你快速掌握PHP从入门到精通开发实战技能。扣丁学堂PHP技术交流群:374332265。


                          


 【关注微信公众号获取更多学习资料】         【扫码进入HTML5进阶免费公开课】


查看更多关于“php培训资讯”的相关文章>>

标签: PHP培训 PHP视频教程 PHP在线视频 PHP学习视频

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号