YinBaoPayment.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Biz\Pay\Payment;
  3. /**
  4. * 银宝支付
  5. */
  6. class YinBaoPayment extends \Biz\Pay\BasePayment {
  7. protected $getWay = "http://webapi.9vpay.com/zfapi/users/paiditem";
  8. protected $paymentName = "YinBao";
  9. function init() {
  10. $this->privateCertPath = '';
  11. $this->publicCertPath = '';
  12. $this->sellerInfo = array(
  13. 'busKey' => 'c96b2a7f77977aeaa7b281e30159d1ed',
  14. 'busAccount' => '24332', //
  15. );
  16. # code...
  17. }
  18. public function toPay() {
  19. print_r($this->dataAccess);
  20. $des = new DES3($this->sellerInfo['busKey']);
  21. $data = array(
  22. 'version' => 'v1.0.0.0',
  23. 'mchtid' => $this->sellerInfo['busAccount'],
  24. 'amount' => $des->encrypt($this->dataAccess->money),
  25. 'bankaccountname' => $des->encrypt($this->dataAccess->extra['accoutName']),
  26. 'bankaccountno' => $des->encrypt($this->dataAccess->extra['accountNo']), //360cd.cn
  27. 'branchname' => $des->encrypt($this->dataAccess->extra['bankName']), //360cd.cn
  28. 'bankcode' => $des->encrypt($this->dataAccess->bankCode), //360cd.cn
  29. 'transtime' => time(), //360cd.cn
  30. 'transid' => $this->dataAccess->orderSn, //360cd.cn
  31. );
  32. $sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
  33. $data['notifyurl'] = $this->notifyUrl;
  34. $data['sign'] = $sign;
  35. $data = json_encode($data, 320);
  36. print_r($data);
  37. $header = array();
  38. $header[] = "Content-Type: application/json; charset=utf-8";
  39. $header[] = "Content-Length: " . strlen($data);
  40. $data = $this->goPayCurl($this->getWay, $data, $header);
  41. print_r($data);
  42. $data = json_decode($data, 1);
  43. $code = $row['code'];
  44. if ($code == 1) {
  45. $data = $row['data'];
  46. $md5 = $this->md5mcrypt($row['data'], $this->sellerInfo['busKey']);
  47. if ($md5 == $data['sign']) {
  48. echo '代付返回签名正确! 结果描述:' . $row['info'];
  49. } else {
  50. echo '返回签名错误!';
  51. }
  52. } else {
  53. echo '系统提示:' . $row['info'];
  54. exit();
  55. }
  56. exit;
  57. }
  58. function md5mcrypt($data, $key) {
  59. ksort($data);
  60. $str = http_build_query($data) . $key;
  61. $str = urldecode($str);
  62. return md5($str);
  63. }
  64. public function redirect($order) {
  65. }
  66. public function notify($order) {
  67. $re = $this->check($order);
  68. if ($re) {
  69. return 'ok';
  70. }
  71. return '';
  72. }
  73. function check($order) {
  74. $data = array();
  75. $des = new DES3($this->sellerInfo['busKey']);
  76. $data['mchtid'] = $_POST['mchtid'];
  77. $this->dataAccess->orderSn = $data['orderno'] = $_POST['orderno'];
  78. $data['state'] = $_POST['state'];
  79. $this->dataAccess->money = $data['amount'] = $des->decrypt($_POST['amount']);
  80. $sign = $this->md5mcrypt($data, $this->sellerInfo['busKey']);
  81. $this->dataAccess->goodsName = '';
  82. $data['sign'] = $_POST['sign'];
  83. if ($sign == $data['sign']) {
  84. if ($data['state'] == '1') {
  85. return 1;
  86. } else {
  87. return 0;
  88. }
  89. }
  90. }
  91. public function getPaymentType() {
  92. # code...
  93. return array(
  94. "alipay" => "1", //"支付宝",
  95. "weixin" => "2", //"微信",
  96. "baidupay" => "4", //"百度钱包",
  97. "qqpay" => "8", //QQ钱包",
  98. "jdpay" => "16", //京东钱包",
  99. "union" => "32", //"银联钱包",
  100. );
  101. }
  102. }
  103. class DES3 {
  104. var $key = "111111111111111111111111";
  105. var $iv = "00000000";
  106. public function __construct($key) {
  107. $this->key = $key;
  108. }
  109. public function encrypt($input) {
  110. $size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
  111. $input = $this->pkcs5_pad($input, $size);
  112. $key = str_pad($this->key, 24, '0');
  113. $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
  114. if ($this->iv == '') {
  115. $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  116. } else {
  117. $iv = $this->iv;
  118. }
  119. @mcrypt_generic_init($td, $key, $iv);
  120. $data = mcrypt_generic($td, $input);
  121. mcrypt_generic_deinit($td);
  122. mcrypt_module_close($td);
  123. $data = base64_encode($data);
  124. return $data;
  125. }
  126. public function decrypt($encrypted) {
  127. $encrypted = base64_decode($encrypted);
  128. $key = str_pad($this->key, 24, '0');
  129. $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
  130. if ($this->iv == '') {
  131. $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  132. } else {
  133. $iv = $this->iv;
  134. }
  135. $ks = mcrypt_enc_get_key_size($td);
  136. @mcrypt_generic_init($td, $key, $iv);
  137. $decrypted = mdecrypt_generic($td, $encrypted);
  138. mcrypt_generic_deinit($td);
  139. mcrypt_module_close($td);
  140. $y = $this->pkcs5_unpad($decrypted);
  141. return $y;
  142. }
  143. function pkcs5_pad($text, $blocksize) {
  144. $pad = $blocksize - (strlen($text) % $blocksize);
  145. return $text . str_repeat(chr($pad), $pad);
  146. }
  147. function pkcs5_unpad($text) {
  148. $pad = ord($text{strlen($text) - 1});
  149. if ($pad > strlen($text)) {
  150. return false;
  151. }
  152. if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
  153. return false;
  154. }
  155. return substr($text, 0, -1 * $pad);
  156. }
  157. function PaddingPKCS7($data) {
  158. $block_size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
  159. $padding_char = $block_size - (strlen($data) % $block_size);
  160. $data .= str_repeat(chr($padding_char), $padding_char);
  161. return $data;
  162. }
  163. }
  164. ?>