Official.php 3.1 KB

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