Official.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2017/12/9
  6. * Time: 12:02
  7. */
  8. namespace App\Libs\Pay\Merchants;
  9. use GuzzleHttp\Client;
  10. use EasyWeChat\Foundation\Application;
  11. use EasyWeChat\Payment\Order as Wxorder;
  12. use Log;
  13. class Official
  14. {
  15. private $appid;
  16. private $secret;
  17. private $token;
  18. private $merchant_id;
  19. private $key;
  20. private $cert_path;
  21. private $key_path;
  22. public $app;
  23. public function __construct($config)
  24. {
  25. $this->appid = $config['appid'];
  26. $this->secret = $config['secret'];
  27. $this->token = $config['token'];
  28. $this->merchant_id = $config['merchant_id'];
  29. $this->key = $config['key'];
  30. $this->cert_path = $config['cert_path'];
  31. $this->key_path = $config['key_path'];
  32. $options = [
  33. 'app_id'=>$this->appid,
  34. 'secret'=>$this->secret,
  35. 'token'=>$this->token,
  36. 'payment'=>[
  37. 'merchant_id'=>$this->merchant_id,
  38. 'key'=>$this->key,
  39. 'cert_path'=>public_path($this->cert_path),
  40. 'key_path'=>public_path($this->key_path)
  41. ]
  42. ];
  43. $this->app = new Application($options);
  44. }
  45. function send($data)
  46. {
  47. Log::info('Official---enter--------------');
  48. $app = $this->app;
  49. $payment = $app->payment;
  50. $attributes = [
  51. 'trade_type' => 'JSAPI',
  52. 'body' => $data['body'],
  53. 'detail' => $data['detail'],
  54. 'out_trade_no' => $data['trade_no'],
  55. 'total_fee' => $data['price'],
  56. 'notify_url' => env('OFFICIAL_PAY_CALL_BACK_URL'),
  57. 'openid' => $data['openid'],
  58. 'spbill_create_ip' => $data['create_ip'],
  59. 'attach' => $data['remark']
  60. ];
  61. $order = new Wxorder($attributes);
  62. $result = $payment->prepare($order);
  63. Log::info('Official -------pay-----result----------');
  64. Log::info($result);
  65. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
  66. $data = [
  67. 'appId' => $result->appid,
  68. 'package' => 'prepay_id=' . $result->prepay_id,
  69. 'nonceStr' => $result->nonce_str,
  70. 'timeStamp' => time(),
  71. 'signType' => 'MD5',
  72. ];
  73. $data['paySign'] = $this->MakeSign($data);
  74. return $data;
  75. }
  76. return [];
  77. }
  78. protected function MakeSign($value)
  79. {
  80. $data = $value;
  81. //签名步骤一:按字典序排序参数
  82. ksort($data);
  83. $buff = "";
  84. foreach ($data as $k => $v) {
  85. if ($k != "sign" && $v != "" && !is_array($v)) {
  86. $buff .= $k . "=" . $v . "&";
  87. }
  88. }
  89. $buff = trim($buff, "&");
  90. //签名步骤二:在string后加入KEY
  91. $string = $buff . "&key=".$this->key;
  92. //签名步骤三:MD5加密
  93. $string = md5($string);
  94. //签名步骤四:所有字符转为大写
  95. $result = strtoupper($string);
  96. return $result;
  97. }
  98. }