request.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * 支付接口调测例子
  4. * ================================================================
  5. * index 进入口,方法中转
  6. * submitOrderInfo 提交订单信息
  7. * queryOrder 查询订单
  8. *
  9. * ================================================================
  10. */
  11. require('Utils.class.php');
  12. require('class/RequestHandler.class.php');
  13. require('class/ClientResponseHandler.class.php');
  14. require('class/PayHttpClient.class.php');
  15. Class Request{
  16. //$url = 'http://192.168.1.185:9000/pay/gateway';
  17. private $resHandler = null;
  18. private $reqHandler = null;
  19. private $pay = null;
  20. public function __construct($account,$merchantPrivateKey){
  21. $this->account = $account;
  22. $this->merchantPrivatekey = $merchantPrivateKey;
  23. $this->url = 'https://pay.swiftpass.cn/pay/gateway';
  24. $this->version = '1.0';
  25. $this->resHandler = new ClientResponseHandler();
  26. $this->reqHandler = new RequestHandler();
  27. $this->pay = new PayHttpClient();
  28. $this->reqHandler->setGateUrl($this->url);
  29. $this->reqHandler->setKey($this->merchantPrivatekey);
  30. }
  31. public function Request(){
  32. }
  33. public function index(){
  34. /*$method = isset($_REQUEST['method'])?$_REQUEST['method']:'submitOrderInfo';
  35. switch($method){
  36. case 'submitOrderInfo'://提交订单
  37. $this->submitOrderInfo();
  38. break;
  39. case 'queryOrder'://查询订单
  40. $this->queryOrder();
  41. break;
  42. case 'submitRefund'://提交退款
  43. $this->submitRefund();
  44. break;
  45. case 'queryRefund'://查询退款
  46. $this->queryRefund();
  47. break;
  48. case 'callback':
  49. $this->callback();
  50. break;
  51. }*/
  52. }
  53. /**
  54. * 提交订单信息
  55. */
  56. public function submitOrderInfo($date){
  57. $this->reqHandler->setReqParams($date,array('method'));
  58. $this->reqHandler->setParameter('service','unified.trade.pay');//接口类型:pay.weixin.native
  59. $this->reqHandler->setParameter('mch_id', $this->account);//必填项,商户号,由威富通分配
  60. $this->reqHandler->setParameter('version',$this->version);
  61. //$this->reqHandler->setParameter('op_shop_id','1314');
  62. //$this->reqHandler->setParameter('device_info','长江');
  63. //$this->reqHandler->setParameter('op_device_id','东风一号');
  64. $this->reqHandler->setParameter('limit_credit_pay','1'); //是否支持信用卡,1为不支持,0为支持
  65. //$this->reqHandler->setParameter('groupno','8111100093');
  66. //通知地址,必填项
  67. $this->reqHandler->setParameter('notify_url',$date['notify_url']);//通知回调地址,目前默认是空格,商户在测试支付和上线时必须改为自己的,且保证外网能访问到
  68. $this->reqHandler->setParameter('nonce_str',mt_rand(time(),time()+rand()));//随机字符串,必填项,不长于 32 位
  69. $this->reqHandler->createSign();//创建签名
  70. $data = Utils::toXml($this->reqHandler->getAllParameters());
  71. //var_dump($data);
  72. $this->pay->setReqContent($this->reqHandler->getGateURL(),$data);
  73. if($this->pay->call()){
  74. $this->resHandler->setContent($this->pay->getResContent());
  75. $this->resHandler->setKey($this->merchantPrivatekey);
  76. if($this->resHandler->isTenpaySign()){
  77. //当返回状态与业务结果都为0时才返回支付二维码,其它结果请查看接口文档
  78. if($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0){
  79. return (array('token_id'=>$this->resHandler->getParameter('token_id'),
  80. 'services'=>$this->resHandler->getParameter('services')));
  81. exit();
  82. }else{
  83. return (array('status'=>500,'msg'=>'Error Code:'.$this->resHandler->getParameter('err_code').' Error Message:'.$this->resHandler->getParameter('err_msg')));
  84. exit();
  85. }
  86. }
  87. return (array('status'=>500,'msg'=>'Error Code:'.$this->resHandler->getParameter('status').' Error Message:'.$this->resHandler->getParameter('message')));
  88. }else{
  89. return (array('status'=>500,'msg'=>'Response Code:'.$this->pay->getResponseCode().' Error Info:'.$this->pay->getErrInfo()));
  90. }
  91. }
  92. /**
  93. * 界面显示
  94. */
  95. public function queryRefund(){
  96. }
  97. /**
  98. * 异步通知方法,demo中将参数显示在result.txt文件中
  99. */
  100. public function callback($xml){
  101. $this->resHandler->setContent($xml);
  102. $this->resHandler->setKey($this->merchantPrivatekey);
  103. $result = array();
  104. if($this->resHandler->isTenpaySign()){
  105. if($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0){
  106. $result['status'] = intval($this->resHandler->getParameter('status'));
  107. $result['result_code'] =intval($this->resHandler->getParameter('result_code'));
  108. $result['out_trade_no'] = $this->resHandler->getParameter('out_trade_no');
  109. $result['out_transaction_id'] = $this->resHandler->getParameter('out_transaction_id');
  110. }else{
  111. $result['status'] = 400;
  112. $result['result_code'] =intval($this->resHandler->getParameter('result_code'));
  113. }
  114. }else{
  115. $result['status'] = 401;
  116. $result['result_code'] =intval($this->resHandler->getParameter('result_code'));
  117. }
  118. return $result;
  119. }
  120. }
  121. ?>