Official.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Illuminate\Support\Facades\Log;
  12. class Official implements PayMerchantInterface
  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 $app;
  22. public function __construct($config)
  23. {
  24. $this->appid = $config['appid'];
  25. $this->merchant_id = $config['merchant_id'];
  26. $this->key = $config['key'];
  27. $this->secret = isset($config['secret']) ? $config['secret'] : '';
  28. $this->token = isset($config['token']) ? $config['token'] : '';
  29. $this->key_path = isset($config['key_path']) ? $config['key_path'] : '';
  30. $this->cert_path = isset($config['cert_path']) ? $config['cert_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. public function send(array $data)
  45. {
  46. $app = $this->app;
  47. $trade_type = isset($data['trade_type']) ? $data['trade_type'] : 'JSAPI';
  48. $attributes = [
  49. 'trade_type' => $trade_type,
  50. 'body' => $data['body'],
  51. 'detail' => isset($data['detail']) ? $data['detail'] : '',
  52. 'out_trade_no' => $data['trade_no'],
  53. 'total_fee' => $data['price'],
  54. 'notify_url' => env('QUICK_APP_OFFICIAL_PAY_CALL_BACK_URL'),
  55. 'openid' => isset($data['openid']) ? $data['openid'] : '',
  56. 'spbill_create_ip' => $data['create_ip'],
  57. 'attach' => $data['remark']
  58. ];
  59. $order = new Wxorder($attributes);
  60. $result = $app->payment->prepare($order);
  61. if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
  62. switch ($trade_type) {
  63. case 'JSAPI':
  64. $data = [
  65. 'appId' => $result->appid,
  66. 'package' => 'prepay_id=' . $result->prepay_id,
  67. 'nonceStr' => $result->nonce_str,
  68. 'timeStamp' => time(),
  69. 'signType' => 'MD5',
  70. ];
  71. $data['paySign'] = $this->makeSign($data);
  72. break;
  73. case 'APP':
  74. $data = [
  75. 'appid' => $result->appid,
  76. 'partnerid' => $result->mch_id,
  77. 'prepayid' => $result->prepay_id,
  78. 'package' => "Sign=WXPay",
  79. 'noncestr' => $result->nonce_str,
  80. 'timestamp' => time(),
  81. ];
  82. $data['sign'] = $this->makeSign($data);
  83. break;
  84. case 'MWEB':
  85. $data = [
  86. 'appId' => $result->appid,
  87. 'mch_id' => $result->mch_id,
  88. 'prepay_id' => $result->prepay_id,
  89. 'nonce_str' => $result->nonce_str,
  90. 'sign' => $result->sign,
  91. 'trade_type' => $result->trade_type,
  92. 'mweb_url' => $result->mweb_url,
  93. ];
  94. break;
  95. }
  96. return $data;
  97. }
  98. Log::error('pay error order is: ' . $data['trade_no']);
  99. Log::error($result);
  100. return [];
  101. }
  102. public function makeSign(array $value)
  103. {
  104. $data = $value;
  105. //签名步骤一:按字典序排序参数
  106. ksort($data);
  107. $buff = "";
  108. foreach ($data as $k => $v) {
  109. if ($k != "sign" && $v != "" && !is_array($v)) {
  110. $buff .= $k . "=" . $v . "&";
  111. }
  112. }
  113. $buff = trim($buff, "&");
  114. //签名步骤二:在string后加入KEY
  115. $string = $buff . "&key=" . $this->key;
  116. //签名步骤三:MD5加密
  117. $string = md5($string);
  118. //签名步骤四:所有字符转为大写
  119. $result = strtoupper($string);
  120. return $result;
  121. }
  122. public function notify(array $data = [])
  123. {
  124. return $this->app->payment;
  125. }
  126. public function query(string $trade_no)
  127. {
  128. $result = $this->app->payment->query($trade_no);
  129. if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS' && $result['trade_state'] === 'SUCCESS') {
  130. return $result['transaction_id'];
  131. } else {
  132. return false;
  133. }
  134. }
  135. public function refund(array $data)
  136. {
  137. }
  138. }