demo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use Xunhu\Hupijiao\Hupijiao;
  4. class Demo {
  5. private $config;
  6. private $data;
  7. public function __construct() {
  8. $this->url='https://'.$_SERVER['HTTP_HOST'];
  9. $this->config=[
  10. 'app_id'=>'2147483647',
  11. 'app_secret'=>'160130736b1ac0d54ed7abe51e44840b',
  12. 'api_url'=>'https://api.xunhupay.com/payment',
  13. ];
  14. //构建测试数据
  15. $orderID=time();
  16. $this->data=[
  17. 'version' => '1.1',
  18. 'appid' => $this->config['app_id'],
  19. 'trade_order_id'=> $orderID, //订单编号
  20. 'payment' => 'wechat',
  21. 'total_fee' => 0.1,
  22. 'title' => 'NO.'.$orderID,
  23. 'time' => time(),
  24. 'notify_url'=> '...', //异步回调地址
  25. 'return_url'=> '...', //支付成功跳转地址,可携带参数
  26. 'nonce_str' => md5(time()),
  27. 'plugins'=>'...' //备注字段
  28. ];
  29. }
  30. //微信扫码支付
  31. public function wxNative(){
  32. $Hupijiao=new Hupijiao($this->config);
  33. $data=$this->data;
  34. $data['payment']='wechat';
  35. $data['notify_url']=$this->url.'/hupijiao-composer/demo.php';
  36. $data['return_url']=$this->url.'/hupijiao-composer/demo.php?type=wx_native';
  37. $response=$Hupijiao->request('wx_native',$data);
  38. ob_start();
  39. ?>
  40. <!doctype html>
  41. <html lang="en">
  42. <head>
  43. <meta charset="UTF-8">
  44. <meta name="viewport"
  45. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  46. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  47. <title>微信扫码支付</title>
  48. </head>
  49. <style>
  50. html,body{
  51. height:100%;
  52. width:100%;
  53. overflow:hidden;
  54. margin:0;
  55. padding:0;
  56. }
  57. body{
  58. background-color: #444444;
  59. display: flex;
  60. flex-direction: column;
  61. align-items: center;
  62. justify-content: center;
  63. }
  64. body a{
  65. padding: 10px 50px;
  66. background-color: #808080;
  67. text-decoration: none;
  68. }
  69. body a:hover{
  70. background-color: #F4F4F4;
  71. }
  72. </style>
  73. <body>
  74. <a href="<?php echo $response['url'];?>">测试支付</a>
  75. </body>
  76. </html>
  77. <?php
  78. return ob_get_clean();
  79. }
  80. //支付成功跳转页面
  81. public function wxReturn(){
  82. ob_start();
  83. ?>
  84. <!doctype html>
  85. <html lang="en">
  86. <head>
  87. <meta charset="UTF-8">
  88. <meta name="viewport"
  89. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  90. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  91. <title>微信扫码支付</title>
  92. </head>
  93. <style>
  94. html,body{
  95. height:100%;
  96. width:100%;
  97. overflow:hidden;
  98. margin:0;
  99. padding:0;
  100. }
  101. body{
  102. background-color: #444444;
  103. display: flex;
  104. flex-direction: column;
  105. align-items: center;
  106. justify-content: center;
  107. }
  108. body span{
  109. font-weight: bold;
  110. font-size: 38px;
  111. color: #fff;
  112. }
  113. </style>
  114. <body>
  115. <span>支付成功</span>
  116. </body>
  117. </html>
  118. <?php
  119. return ob_get_clean();
  120. }
  121. //异步回调
  122. public function wxNotify(){
  123. $Hupijiao=new Hupijiao($this->config);
  124. $Hupijiao->checkResponse($_POST);
  125. echo 'success';
  126. }
  127. }
  128. $Demo=new Demo();
  129. if(isset($_POST['hash'])){
  130. $Demo->wxNotify();
  131. }elseif (isset($_GET['type'])){
  132. echo $Demo->wxReturn();
  133. }else{
  134. echo $Demo->wxNative();
  135. }