PHP微信支付功能示例
程序员文章站
2022-06-08 23:02:00
本文实例讲述了php微信支付功能。分享给大家供大家参考,具体如下:
微信开发sdk:文中用的是php_sdk_v3.0.9 :
import("wxpay.li...
本文实例讲述了php微信支付功能。分享给大家供大家参考,具体如下:
微信开发sdk:文中用的是php_sdk_v3.0.9 :
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 curl用法总结》、《php网络编程技巧总结》、《php字符串(string)用法总结》、《php中json格式数据操作技巧汇总》及《php针对xml文件操作技巧总结》
希望本文所述对大家php程序设计有所帮助。