Official.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. private $trade_type;
  22. public $app;
  23. public function __construct($config)
  24. {
  25. $this->appid = $config['appid'];
  26. $this->merchant_id = $config['merchant_id'];
  27. $this->key = $config['key'];
  28. $this->secret = isset($config['secret']) ? $config['secret'] : '';
  29. $this->token = isset($config['token']) ? $config['token'] : '';
  30. $this->key_path = isset($config['key_path']) ? $config['key_path'] : '';
  31. $this->cert_path = isset($config['cert_path']) ? $config['cert_path'] : '';
  32. $this->trade_type = isset($config['trade_type']) ? $config['trade_type'] : 'JSAPI';
  33. $options = [
  34. 'app_id' => $this->appid,
  35. 'secret' => $this->secret,
  36. 'token' => $this->token,
  37. 'payment' => [
  38. 'merchant_id' => $this->merchant_id,
  39. 'key' => $this->key,
  40. 'cert_path' => public_path($this->cert_path),
  41. 'key_path' => public_path($this->key_path)
  42. ]
  43. ];
  44. $this->app = new Application($options);
  45. }
  46. function send($data)
  47. {
  48. $app = $this->app;
  49. $payment = $app->payment;
  50. $attributes = [
  51. 'trade_type' => $this->trade_type,
  52. 'body' => $data['body'],
  53. 'detail' => isset($data['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' => isset($data['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---------- order is: ' . $data['trade_no']);
  64. Log::info($result);
  65. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
  66. switch ($this->trade_type) {
  67. case 'JSAPI':
  68. $data = [
  69. 'appId' => $result->appid,
  70. 'package' => 'prepay_id=' . $result->prepay_id,
  71. 'nonceStr' => $result->nonce_str,
  72. 'timeStamp' => time(),
  73. 'signType' => 'MD5',
  74. ];
  75. $data['paySign'] = $this->MakeSign($data);
  76. break;
  77. case 'APP':
  78. $data = [
  79. 'appId' => $result->appid,
  80. 'mch_id' => $result->mch_id,
  81. 'prepay_id' => $result->prepay_id,
  82. 'nonce_str' => $result->nonce_str,
  83. 'sign' => $result->sign,
  84. 'trade_type' => $result->trade_type,
  85. ];
  86. break;
  87. }
  88. return $data;
  89. }
  90. Log::error('pay error order is: ' . $data['trade_no']);
  91. Log::error($result);
  92. return [];
  93. }
  94. protected function MakeSign($value)
  95. {
  96. $data = $value;
  97. //签名步骤一:按字典序排序参数
  98. ksort($data);
  99. $buff = "";
  100. foreach ($data as $k => $v) {
  101. if ($k != "sign" && $v != "" && !is_array($v)) {
  102. $buff .= $k . "=" . $v . "&";
  103. }
  104. }
  105. $buff = trim($buff, "&");
  106. //签名步骤二:在string后加入KEY
  107. $string = $buff . "&key=" . $this->key;
  108. //签名步骤三:MD5加密
  109. $string = md5($string);
  110. //签名步骤四:所有字符转为大写
  111. $result = strtoupper($string);
  112. return $result;
  113. }
  114. }