Swiftpass.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Libs\Pay\Merchants;
  3. use GuzzleHttp\Client;
  4. use Log;
  5. /**
  6. *
  7. *
  8. * commit e0bb38efab0b546f00ffd8f5f485032b7cda4ad9
  9. Author: zz <524914631@qq.com>
  10. Date: Thu Aug 3 18:00:57 2017 +0800
  11. dist
  12. commit 90a52f5073dac91e58487d9966faf55b55cf849f
  13. Author: zz <524914631@qq.com>
  14. Date: Thu Aug 3 17:33:32 2017 +0800
  15. dist
  16. commit 6e84098457780a2d0e57510eca4cfaf8a5b65ba5
  17. Author: zz <524914631@qq.com>
  18. Date: Wed Aug 2 13:39:44 2017 +0800
  19. apple online check
  20. */
  21. class Swiftpass {
  22. private $merchant_key;
  23. private $merchant_id;
  24. private $appid;
  25. function __construct()
  26. {
  27. //test
  28. $this->merchant_key = '9d101c97133837e13dde2d32a5054abb';//$merchant_key;
  29. $this->merchant_id = '7551000001';
  30. // online
  31. $this->merchant_key = 'f701231c6916ff1e0f695c051f7e53b3';//$merchant_key;
  32. $this->merchant_id = '101550129410';
  33. }
  34. //订单发送
  35. function send($trade_no,$fee=100,$ip="192.168.1.21",$return_url,$open_id)
  36. {
  37. $client = new client(['base_uri'=>'https://pay.swiftpass.cn/','timeout'=>20]);
  38. $data = [
  39. 'service'=>'pay.weixin.jspay',
  40. 'mch_id'=>$this->merchant_id,
  41. 'out_trade_no'=>$trade_no,
  42. 'body'=>'充值',
  43. 'sub_openid'=>$open_id,
  44. 'sub_appid'=>'wxebcb86ec4b80eaca',
  45. 'total_fee'=>$fee*100,
  46. 'mch_create_ip'=>$ip,
  47. 'notify_url'=>env('SWIFT_PAY_NOFITY_URL'),
  48. 'nonce_str'=>md5(time()),
  49. 'is_raw'=>1,
  50. 'callback_url'=>$return_url //交易完成后跳转的URL
  51. ];
  52. Log::info('client_request_data');
  53. Log::info($data);
  54. $data['sign'] = $this->sign($data);
  55. $response = $client->request('POST',"pay/gateway",['body'=>arrayToXml($data)])->getBody()->getContents();
  56. Log::info('client_$response');
  57. Log::info($response);
  58. return $this->getPayInfo($response);
  59. }
  60. function sign($params) {
  61. $signPars = "";
  62. ksort($params);
  63. foreach($params as $k => $v) {
  64. if("" != $v && "sign" != $k) {
  65. $signPars .= $k . "=" . $v . "&";
  66. }
  67. }
  68. $signPars .= "key=".$this->merchant_key;
  69. $sign = strtoupper(md5($signPars));
  70. return $sign;
  71. }
  72. function getPayInfo($response)
  73. {
  74. $return_info = simplexml_load_string($response, null, LIBXML_NOCDATA);
  75. Log::info('$return_info');
  76. Log::info($return_info->token_id);
  77. $pay_info = json_decode($return_info->pay_info,1);
  78. Log::info('origin_pay_info');
  79. Log::info($pay_info);
  80. $token = isset($return_info->token_id)?$return_info->token_id:'';
  81. $prepay_url = "https://pay.swiftpass.cn/pay/jspay?token_id=".$token;
  82. return [
  83. 'appId' => $pay_info['appId'],
  84. 'package' => $pay_info['package'],
  85. 'nonceStr' => $pay_info['nonceStr'],
  86. 'timeStamp' => $pay_info['timeStamp'],
  87. 'signType' => 'MD5',
  88. 'paySign' => $pay_info['paySign'],
  89. 'prepay_url' => $prepay_url
  90. ];
  91. }
  92. }