从PHP代码转换为Ruby代码。它是否正确?

时间:2015-12-20 14:28:34

标签: php ruby ruby-on-rails-4

我正在尝试在线集成API进行付款。 API使用PHP代码。我的网站是ruby on rails,所以我必须将它转换为ruby。这是PHP代码:

$merchant_id = '17260';

$password = 'cecae5cffadff3cecfc45ce370ef1803';

$params = array(

'func'                                    => 'CardCharge',

'version'                              => '2.0',

'merchant_id'                      => $merchant_id,

'merchant_account'            => 'diendc@peacesoft.net',

'merchant_password'          => MD5($merchant_id.'|'.$password),

'pin_card'                            => '12345678912345',

'card_serial'                      => 'XYZ123',

'type_card'                          => 'VNP',// VNP hoặc VMS hoặc VIETTEL

'ref_code'                            => time(),

              'client_fullname'              => 'Do Cong Dien',

              'client_email'                    => 'diendc@gmail.com',

              'client_mobile'                  => '0904515105',



);
    $post_field = '';
foreach ($params as $key => $value){

if ($post_field != '') $post_field .= '&';

$post_field .= $key."=".$value;

}

$api_url = "https://www.nganluong.vn/mobile_card.api.post.v2.php";



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$api_url);
curl_setopt($ch, CURLOPT_ENCODING , 'UTF-8'); 
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field); 
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
$error = curl_error($ch);

这是我转换的Ruby代码:

    require 'digest/md5'
    require 'rest-client'

    merchant_id = '40432'

    password = 'xxxxxx'

    params = {
          'func'               =>  'CardCharge',
          'version'            =>  '2.0',
          'merchant_id'        =>  merchant_id,
          'merchant_account'   =>  'xxx@xxx.com',
          'merchant_password'  =>   Digest::MD5.hexdigest(merchant_id + '|' + password),
          'pin_card'           =>  @card.pin,
          'card_serial'        =>  @card.serial,
          'type_card'          =>  @card.type, # VNP hoặc VMS hoặc VIETTEL
          'ref_code'           =>  Time.now.to_i,
          'client_fullname'    =>  'My Name',
          'client_email'       =>  'xxx@xxx.com',
          'client_mobile'      =>  'xxxx33424'
          }

    #post_field = ''

    #params.each do |k, v|
    #  if (post_field != '')
    #     post_field += '&'
    #  end
    #  post_field += k.to_s+'='+v.to_s
    #end

    api_url = "https://www.nganluong.vn/mobile_card.api.post.v2.php"
    result = ''
    status = ''
    error = ''
    begin
      response = RestClient.post api_url, :params => params
      status = response.code # http status
      result = response.body # result
    rescue Exception => e
      error = e.message # error messages
    end

但是当我将数据发布到网址API时:https://www.nganluong.vn/mobile_card.api.post.v2.php"我总是收到错误:从商家发送的参数不准确。

任何人都可以帮我解决这个问题吗?非常感谢你。

0 个答案:

没有答案