PHP微信支付功能示例

这篇文章主要介绍了PHP微信支付功能,结合实例形式分析了php微信支付功能相关sdk与使用技巧,需要的朋友可以参考下。

本文实例讲述了PHP微信支付功能,分享给大家供大家参考,具体如下:

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

  1. import("wxpay.lib.AppPay#Config", EXTEND_PATH, ".php");
  2. import("wxpay.lib.WxPay#Api", EXTEND_PATH, ".php");
  3. //$this->weixinpay();
  4. $config = new \AppPayConfig();
  5. //订单号
  6. $appId = $config->GetAppId();
  7. $key = $config->GetKey();
  8. $money = 0.01 * 100;
  9. $orderSn = 'M201810241754107557';//订单号;
  10. $curTime = time();
  11. $input = new \WxPayUnifiedOrder();
  12. $input->SetBody("用户红包");
  13. $input->SetOut_trade_no($orderSn);
  14. $input->SetTotal_fee($money);
  15. $input->SetTrade_type("APP");
  16. $unifiedOrder = \WxPayApi::unifiedOrder($config, $input);
  17. if ($unifiedOrder['result_code'] == 'SUCCESS' && $unifiedOrder['return_code'] == 'SUCCESS') {
  18. /*$unifiedOrder 组成:
  19. * ["appid"] => string(18) "wx2xxxx49"
  20. ["mch_id"] => string(10) "1xxxxxx01"
  21. ["nonce_str"] => string(16) "1GnEUXTuTcFtJVtb"
  22. ["prepay_id"] => string(36) "wx241933xxx68fa24ece70611692955"
  23. ["result_code"] => string(7) "SUCCESS"
  24. ["return_code"] => string(7) "SUCCESS"
  25. ["return_msg"] => string(2) "OK"
  26. ["sign"] => string(32) "7903xxxxx4710E460CB156"
  27. ["trade_type"] => string(3) "APP"*/
  28. $curTime = time();
  29. // 第一部分
  30. $wxpayResult = new \WxPayResults();
  31. $wxpayResult->SetData('appid', $unifiedOrder['appid']);
  32. $wxpayResult->SetData('partnerid', $unifiedOrder['mch_id']);
  33. $wxpayResult->SetData('prepayid', $unifiedOrder['prepay_id']);
  34. $wxpayResult->SetData('noncestr', $unifiedOrder['nonce_str']);
  35. $wxpayResult->SetData('timestamp',$curTime);
  36. $wxpayResult->SetData('package',"Sign=WXPay");
  37. $wxpayResult->SetData('sign',"");
  38. $wxpayResult->SetSign($config);
  39. $data = $wxpayResult->GetValues();
  40. // 第二部分
  41. $data['appid'] = $unifiedOrder['appid'];
  42. $data['partnerid'] = $unifiedOrder['mch_id'];
  43. $data['prepayid'] = $unifiedOrder['prepay_id'];
  44. $data['noncestr'] = $unifiedOrder['nonce_str'];;
  45. $data['timestamp'] = $curTime;
  46. $data['package'] = "Sign=WXPay";
  47. $data['sign'] = $this->wxappPaySign($key, $data);//$unifiedOrder['sign'];//签名,具体签名方案参见微信公众号支付帮助文档;
  48. $this->returnSuccess($data);
  49. } else {
  50. Log::write(var_export($unifiedOrder, true));
  51. $this->returnError(300, '微信支付失败');
  52. }
  53. private function wxappPaySign($key,$parameters)
  54. {
  55. $sign = md5(sprintf("app,
  56. $parameters['appid'],
  57. $parameters['noncestr'],
  58. $parameters['package'],
  59. $parameters['partnerid'],
  60. $parameters['prepayid'],
  61. $parameters['timestamp'],
  62. $key));
  63. return strtoupper($sign);
  64. }

第一部分和第二部分功能是相同的。