pay-order-create.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. try {
  16. // 1. 手动加载入口文件
  17. include "../include.php";
  18. // 2. 准备公众号配置参数
  19. $config = include "./config.php";
  20. // 3. 创建接口实例
  21. // $wechat = new \WeChat\Pay($config);
  22. // $wechat = \We::WeChatPay($config);
  23. $wechat = \WeChat\Pay::instance($config);
  24. // 4. 组装参数,可以参考官方商户文档
  25. $options = [
  26. 'body' => '测试商品',
  27. 'out_trade_no' => time(),
  28. 'total_fee' => '1',
  29. 'openid' => 'o38gpszoJoC9oJYz3UHHf6bEp0Lo',
  30. 'trade_type' => 'JSAPI', // JSAPI--JSAPI支付(服务号或小程序支付)、NATIVE--Native 支付、APP--APP支付,MWEB--H5支付
  31. 'notify_url' => 'https://a.com/text.html',
  32. 'spbill_create_ip' => '127.0.0.1',
  33. ];
  34. // 生成预支付码
  35. $result = $wechat->createOrder($options);
  36. echo '<pre>';
  37. if ($options['trade_type'] === 'NATIVE') {
  38. echo '<h3>二维码 NATIVE 支付,直接使用 code_url 生成二维码即可</h3>';
  39. var_export($result);
  40. return;
  41. }
  42. // 创建JSAPI参数签名
  43. $options = $wechat->createParamsForJsApi($result['prepay_id']);
  44. echo "<h3>--- 创建 JSAPI 预支付码 ---</h3>";
  45. var_export($result);
  46. // array(
  47. // 'return_code' => 'SUCCESS',
  48. // 'return_msg' => 'OK',
  49. // 'result_code' => 'SUCCESS',
  50. // 'mch_id' => '1332187001',
  51. // 'appid' => 'wx60a43dd8161666d4',
  52. // 'nonce_str' => 'YIPDbGWT1jpLLM5R',
  53. // 'sign' => '7EBBA1B5F196CF122C920D10FA768D96',
  54. // 'prepay_id' => 'wx211858080224615a10c2fc9f6c824f0000',
  55. // 'trade_type' => 'JSAPI',
  56. // )
  57. echo "<h3>--- 生成 JSAPI 及 H5 支付参数 ---</h3>";
  58. var_export($options);
  59. // array(
  60. // 'appId' => 'wx60a43dd8161666d4',
  61. // 'timeStamp' => '1669028299',
  62. // 'nonceStr' => '5s7h0dyp0nmzylbqytb462fpnb0tmrjg',
  63. // 'package' => 'prepay_id=wx21185819502283c23cca162e9d787f0000',
  64. // 'signType' => 'MD5',
  65. // 'paySign' => 'BBE0F426B8E1EEC9E45AC4459E8AE9D6',
  66. // 'timestamp' => '1669028299',
  67. // )
  68. } catch (Exception $exception) {
  69. // 出错啦,处理下吧
  70. echo $exception->getMessage() . PHP_EOL;
  71. }