TestRefill.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. define('APP_ID', 'test');
  4. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  5. require_once(BASE_ROOT_PATH . '/global.php');
  6. require_once(BASE_CORE_PATH . '/lrlz.php');
  7. require_once(BASE_ROOT_PATH . '/fooder.php');
  8. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  9. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  10. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  11. class TestRefill extends TestCase
  12. {
  13. public static function setUpBeforeClass(): void
  14. {
  15. Base::run_util();
  16. }
  17. public function testFactory()
  18. {
  19. $providers = refill\RefillFactory::instance()->find_oil(100,mtopcard\SinopecCard);
  20. if(empty($providers)) return false;
  21. $input = [
  22. 'order_sn' => '200649600557717656',
  23. 'notifyurl' => "https://www.xyzshops.cn/mobile/signature.php"
  24. ];
  25. foreach ($providers as $provider) {
  26. $resp = $provider->add("1000111100021211884",mtopcard\SinopecCard,100,$input);
  27. }
  28. }
  29. public function testRefillPhone()
  30. {
  31. $providers = new \refill\beixt\RefillPhone();
  32. $resp = $providers->add(18500608333,50,'200649600557718888');
  33. //{"ack":"success","message":{"order_number":13281474,"charged_amount":"48.750","shipping_status":"5","shipping_status_desc":"未发货"}}
  34. // content={"order_number":13281474,"shipping_status":1,"shipping_status_desc":"已发货","shipping_status_message":"","sign":"05863f9931ed69a70e456222f057dfdd","voucher":"110103307162012081746340295254","vouchertype":"","voucherurl":"","tradeNo":"200649600557718888"}
  35. }
  36. public function testAddoil()
  37. {
  38. $params = ['mchid' => 1,'cardno' => '1000111100021211884','amount' => "100","act"=>"refill","op"=>"addoil",
  39. 'order_sn' => "13281474",
  40. 'notifyurl'=> BASE_SITE_URL . "/mobile/suhc_notify.php"];
  41. $this->send($params);
  42. }
  43. public function testSuhcCB()
  44. {
  45. $notifyurl = BASE_SITE_URL . "/mobile/refill_suhc.php";
  46. $params = ['onlystr' => '200660783085683671',
  47. 'amt' => 100,
  48. 'jdno'=>'',
  49. 'notifyurl' => $notifyurl,
  50. 'cardtype' => 'Sinoepc',
  51. 'batchid' => 15678,
  52. 'cardno' => '1000111100021211884',
  53. 'orgid' => '1590993600',
  54. 'status' => 2];
  55. $sign = $this->md5_sign($params);
  56. $params['sign'] = $sign;
  57. $resp = http_request($notifyurl,$params,'POST');
  58. Log::record($resp,Log::DEBUG);
  59. }
  60. private function md5_sign($params)
  61. {
  62. ksort($params);
  63. $body = "";
  64. $i = 0;
  65. foreach ($params as $k => $v)
  66. {
  67. if ($i == 0) {
  68. $body .= "{$k}";
  69. if(!empty($v)) {
  70. $body .= "=" . $v;
  71. }
  72. }
  73. else {
  74. $body .= "&" . "{$k}";
  75. if(!empty($v)) {
  76. $body .= "=" . $v;
  77. }
  78. }
  79. $i++;
  80. }
  81. $body .= "&key=7yDCLS6S2KzSAJQOUc3vsa";
  82. return md5($body);
  83. }
  84. public function testCallMech()
  85. {
  86. $logic = Logic('queue');
  87. $logic->NotifyMerchantComplete(['order_id' => 289]);
  88. }
  89. private function send($params)
  90. {
  91. $mchid = $params['mchid'];
  92. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  93. $key = file_get_contents($pri_key);
  94. $pri = openssl_get_privatekey($key);
  95. $body = $this->body($params);
  96. openssl_sign($body, $signed, $pri);
  97. $sign = base64_encode($signed);
  98. $params['sign'] = $sign;
  99. $resp = http_request(BASE_SITE_URL . "/mobile/index.php",$params,'POST');
  100. Log::record("resp:{$resp}",Log::DEBUG);
  101. }
  102. public function testCardType()
  103. {
  104. $cardno = '1000111100021211884';
  105. $ret = preg_match( '/^1[0-9]{18}$/',$cardno,$matches);
  106. }
  107. public function testMtrand()
  108. {
  109. }
  110. public function testHttpRefill()
  111. {
  112. $mchid = '000001';
  113. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  114. $key = file_get_contents($pri_key);
  115. $pri = openssl_get_privatekey($key);
  116. $params = ['MCHID' => $mchid,'cardno' => '1000111100021211884','amt' => "100","act"=>"refill","op"=>"addoil"];
  117. $body = $this->body($params);
  118. openssl_sign($body, $signed, $pri);
  119. $sign = base64_encode($signed);
  120. $params['sign'] = $sign;
  121. $resp = http_request(BASE_SITE_URL . "/mobile/index.php",$params,'POST');
  122. }
  123. private function body($params)
  124. {
  125. ksort($params);
  126. $stringToBeSigned = "";
  127. $i = 0;
  128. foreach ($params as $k => $v)
  129. {
  130. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1))
  131. {
  132. if ($i == 0) {
  133. $stringToBeSigned .= "{$k}" . "=" . urlencode($v);
  134. } else {
  135. $stringToBeSigned .= "&" . "{$k}" . "=" . urlencode($v);
  136. }
  137. $i++;
  138. }
  139. }
  140. return $stringToBeSigned;
  141. }
  142. private function checkEmpty($value)
  143. {
  144. if (!isset($value))
  145. return true;
  146. if ($value === null)
  147. return true;
  148. if (trim($value) === "")
  149. return true;
  150. return false;
  151. }
  152. public function testKsort()
  153. {
  154. $age= ["Peter"=>"35","Ben"=>"37","Joe"=>"43"];
  155. ksort($age);
  156. foreach($age as $x=>$x_value)
  157. {
  158. echo "Key=" . $x . ", Value=" . $x_value;
  159. echo "<br>";
  160. }
  161. }
  162. }