0

我正在使用 curl 向支付系统的虚拟商户支付网关发送支付信息。这是我的代码:

$Url= "https://www.myvirtualmerchant.com/VirtualMerchant/process.do";
    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Now set some options (most are optional)

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);


    // Include header in result? (0 = yes, 1 = no)
   // curl_setopt($ch, CURLOPT_HEADER, 0);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);


    $fields = array(
            'ssl_card_number'=>urlencode($_POST['ssl_card_number']),
            'ssl_exp_date'=>urlencode($_POST['ssl_exp_date']),
            'ssl_cvv2cvc2'=>urlencode($_POST['ssl_cvv2cvc2']),
            'ssl_avs_address'=>urlencode($_POST['ssl_avs_address']),
            'ssl_avs_zip'=>urlencode($_POST['ssl_avs_zip']),
            'ssl_merchant_id'=>urlencode($_POST['ssl_merchant_id']),
            'ssl_user_id'=>urlencode($_POST['ssl_user_id']),
            'ssl_pin'=>urlencode($_POST['ssl_pin']),
            'ssl_transaction_type'=>urlencode($_POST['ssl_transaction_type']),
            'ssl_amount'=>urlencode($_POST['ssl_amount']),
            'ssl_show_form'=>urlencode($_POST['ssl_show_form']),
            'TransactionType'=>urlencode($_POST['TransactionType'])
        );

    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    // Download the given URL, and return output
    echo $output = curl_exec($ch);

    // Close the cURL resource, and free system resources
    curl_close($ch);

    print_r($output); 

但是在 $output 我什么也没得到,没有任何错误或消息。我做错了吗?请告诉我 ?

4

5 回答 5

1

首先,我会检查您的 mechant_id、pin 等。下面是我在解决类似问题后创建的工作代码。

<html>
    <body>
        <p>--start--</p>

<?php
//if you have a live account don't use the "demo" post url it won't work
$post_url = 'https://www.myvirtualmerchant.com/VirtualMerchant/process.do';

//replace the xxx's with your proper merchant_id, etc. 
//they will give you these when you activate your account

//I've set form to not show, and ssl_result_format =>ascii to get a string returned

$fields = array(
    'ssl_merchant_id'           =>'xxxxxx',
    'ssl_user_id'               =>'xxx',
    'ssl_pin'               =>'xxxxx',
    'ssl_show_form'             =>'false',
    'ssl_result_format'         =>'ascii',
    'ssl_test_mode'             =>'false',
    'ssl_transaction_type'      =>'ccsale',
    'ssl_amount'                =>'1.44',
    'ssl_card_number'           =>'5000300020003003',
     'ssl_exp_date'             =>'1214',
    'ssl_avs_address'           =>'Test 3',
    'ssl_avs_zip'               =>'123456',
    'ssl_cvv2cvc2'              =>'123',
);

//build the post string
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .=$key.'='.$value.'&'; }
rtrim($fields_string, "&");

//open curl session
// documentation on curl options at http://www.php.net/curl_setopt

$ch = curl_init();
//begin seting curl options
//set URL
curl_setopt($ch, CURLOPT_URL, $post_url);

//set method
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//set post data string
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//these two options are frequently necessary to avoid SSL errors with PHP
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$result = curl_exec($ch); 

if($result === FALSE) {
    //post failed
    die(curl_error($ch));

} else {
    //got a response
    //some people seem to get name/value pairs delimited with "&" 
    //but currently mine is \n 
    //support told me there's no way to change it..
    $response_array = explode("\n",$result); 

    //make it nice and useful
    foreach( $response_array as $k=>$v ){
        $v=explode("=",$v);
        $a[$v[0]]=$v[1];
    }

    //show the whole array
    print_r($a);

    //use a specific return value
    //returns "APPROVAL" if it went through
    echo('<h1>'. $a[ssl_result_message] . '</h1>');
}
?>

       <p>--end--</p>
    </body>
</html>

上面的代码应该给你一个像这样的屏幕:

--start--

Array ( [ssl_card_number] => 50**********3003 [ssl_exp_date] => 1214 [ssl_amount] => 1.44 [ssl_customer_code] => [ssl_salestax] => [ssl_invoice_number] => [ssl_description] => [ssl_departure_date] => [ssl_completion_date] => [ssl_company] => [ssl_first_name] => [ssl_last_name] => [ssl_avs_address] => Test 3 [ssl_address2] => [ssl_city] => [ssl_state] => [ssl_avs_zip] => 123456 [ssl_country] => [ssl_phone] => [ssl_email] => [ssl_result] => 0 [ssl_result_message] => APPROVAL [ssl_txn_id] => AA49315-1234567-F78F-468F-AF1A-F5C4ADCFFB1E [ssl_approval_code] => N53032 [ssl_cvv2_response] => [ssl_avs_response] => [ssl_account_balance] => 0.00 [ssl_txn_time] => 01/15/2014 11:53:15 AM )

APPROVAL

--end--

确保在完成测试后删除所有这些测试购买。我被告知他们会禁止发布您的真实购买。

于 2014-01-15T17:56:29.040 回答
0

您正在调用 HTTPS 页面。请参考以下链接

http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

于 2012-05-14T05:52:31.187 回答
0

尝试在 curl 关闭之前找到错误给出此代码:-

echo "Curl Error :--" . curl_error($ch);

如果没有发现错误,请这样做:-

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch); 

然后

print_r($result);
exit;
于 2012-05-14T05:57:47.727 回答
0

尝试这个:

$output = curl_exec($ch);
$response = curl_getinfo($ch);
echo "<pre>";
print_r($response);
echo "</pre>";

希望你能得到回应:)

于 2012-05-14T06:02:05.130 回答
0

试试这个找出错误

var_dump(curl_error($ch));

之前和打电话curl_exec($ch);

于 2012-05-14T05:39:17.807 回答