pay.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * 调用支付
  4. *
  5. * 实现微信、支付宝支付的接口
  6. * @date 2019年6月22日
  7. * @copyright 重庆迅虎网络有限公司
  8. */
  9. require_once 'api.php';
  10. $trade_order_id = time();//商户网站内部ID,此处time()是演示数据
  11. $appid = '201906120002';//测试账户,
  12. $appsecret = '7a0967a8f830c9319c87e01e691b8f09';//测试账户,
  13. $my_plugin_id ='my-plugins-id';
  14. $data=array(
  15. 'version' => '1.1',//固定值,api 版本,目前暂时是1.1
  16. 'lang' => 'zh-cn', //必须的,zh-cn或en-us 或其他,根据语言显示页面
  17. 'plugins' => $my_plugin_id,//必须的,根据自己需要自定义插件ID,唯一的,匹配[a-zA-Z\d\-_]+
  18. 'appid' => $appid, //必须的,APPID
  19. 'trade_order_id'=> $trade_order_id, //必须的,网站订单ID,唯一的,匹配[a-zA-Z\d\-_]+
  20. 'payment' => 'wechat',//必须的,支付接口标识:wechat(微信接口)|alipay(支付宝接口)
  21. 'type' => 'WAP',//固定值"WAP" H5支付必填
  22. 'wap_url' => home_url(),//网站域名,H5支付必填
  23. 'wap_name' => home_url(),//网站域名,或者名字,必填,长度32或以内 H5支付必填
  24. 'total_fee' => '0.1',//人民币,单位精确到分(测试账户只支持0.1元内付款)
  25. 'title' => '耐克球鞋', //必须的,订单标题,长度32或以内
  26. 'time' => time(),//必须的,当前时间戳,根据此字段判断订单请求是否已超时,防止第三方攻击服务器
  27. 'notify_url'=> 'http://www.xx.com/notify.php', //必须的,支付成功异步回调接口
  28. 'return_url'=> 'http://www.xx.com/pay/success.html',//必须的,支付成功后的跳转地址
  29. 'callback_url'=>'http://www.xx.com/pay/checkout.html',//必须的,支付发起地址(未支付或支付失败,系统会会跳到这个地址让用户修改支付信息)
  30. 'modal'=>null, //可空,支付模式 ,可选值( full:返回完整的支付网页; qrcode:返回二维码; 空值:返回支付跳转链接)
  31. 'nonce_str' => str_shuffle(time())//必须的,随机字符串,作用:1.避免服务器缓存,2.防止安全密钥被猜测出来
  32. );
  33. $hashkey =$appsecret;
  34. $data['hash'] = XH_Payment_Api::generate_xh_hash($data,$hashkey);
  35. /**
  36. * 个人支付宝/微信官方支付,支付网关:https://api.xunhupay.com
  37. * 微信支付宝代收款,需提现,支付网关:https://pay.wordpressopen.com
  38. */
  39. $url = 'https://api.xunhupay.com/payment/do.html';
  40. try {
  41. $response = XH_Payment_Api::http_post($url, json_encode($data));
  42. /**
  43. * 支付回调数据
  44. * @var array(
  45. * order_id,//支付系统订单ID
  46. * url//支付跳转地址
  47. * )
  48. */
  49. $result = $response?json_decode($response,true):null;
  50. if(!$result){
  51. throw new Exception('Internal server error',500);
  52. }
  53. $hash = XH_Payment_Api::generate_xh_hash($result,$hashkey);
  54. if(!isset( $result['hash'])|| $hash!=$result['hash']){
  55. throw new Exception(__('Invalid sign!',XH_Wechat_Payment),40029);
  56. }
  57. if($result['errcode']!=0){
  58. throw new Exception($result['errmsg'],$result['errcode']);
  59. }
  60. $pay_url =$result['url'];
  61. header("Location: $pay_url");
  62. exit;
  63. } catch (Exception $e) {
  64. echo "errcode:{$e->getCode()},errmsg:{$e->getMessage()}";
  65. //TODO:处理支付调用异常的情况
  66. }
  67. ?>