iT邦幫忙

0

PHP的paypal代碼

各位好,由於急需要寫一個鑲入在網站的paypal支付代碼

目前的問題是裡面的價格與一個客戶自訂的部分

$myPaypal->addField('amount', '100');
$myPaypal->addField('custom', 'muri-khao');

現在的代碼是如下

<?php

// Include the paypal library
include_once ('Paypal.php');

// Create an instance of the paypal library
$myPaypal = new Paypal();

// Specify your paypal email
$myPaypal->addField('business', '123@123.net');

// Specify the currency
$myPaypal->addField('currency_code', 'TWD');

// Specify the url where paypal will send the user on success/failure
$myPaypal->addField('return', '/paypal/paypal_success.php');
$myPaypal->addField('cancel_return', '/paypal/paypal_failure.php');

// Specify the url where paypal will send the IPN
$myPaypal->addField('notify_url', '/paypal/paypal_ipn.php');

// Specify the product information
$myPaypal->addField('item_name', '1:10 money');
$myPaypal->addField('amount', '100');
$myPaypal->addField('item_number', '001');
	

// Specify any custom value
$myPaypal->addField('custom', 'muri-khao');

// Enable test mode if needed
$myPaypal->enableTestMode();

// Let's start the train!
$myPaypal->submitPayment();

並且連上

看更多先前的討論...收起先前的討論...
Paypal.php
這裡面是什麼??
paypal的代碼就include "('PaymentGateway.php')"
<?php
include_once ('PaymentGateway.php');

class Paypal extends PaymentGateway
{
	public function __construct()
	{
        parent::__construct();

        // Some default values of the class
		$this->gatewayUrl = 'https://www.paypal.com/cgi-bin/webscr';
		$this->ipnLogFile = 'paypal.ipn_results.log';

		// Populate $fields array with a few default
		$this->addField('rm', '2');           // Return method = POST
		$this->addField('cmd', '_xclick');
	}

    /**
     * Enables the test mode
     *
     * @param none
     * @return none
     */
    public function enableTestMode()
    {
        $this->testMode = TRUE;
        $this->gatewayUrl = 'https://www.paypal.com/cgi-bin/webscr';
    }
    /**
	 * Validate the IPN notification
	 *
	 * @param none
	 * @return boolean
	 */
	public function validateIpn()
	{
		// parse the paypal URL
		$urlParsed = parse_url($this->gatewayUrl);

		// generate the post string from the _POST vars
		$postString = '';

		foreach ($_POST as $field=>$value)
		{
			$this->ipnData["$field"] = $value;
			$postString .= $field .'=' . urlencode(stripslashes($value)) . '&';
		}

		$postString .="cmd=_notify-validate"; // append ipn command

		// open the connection to paypal
		$fp = fsockopen($urlParsed[host], "80", $errNum, $errStr, 30);
		if(!$fp)
		{
			// Could not open the connection, log error if enabled
			$this->lastError = "fsockopen error no. $errNum: $errStr";
			$this->logResults(false);

			return false;
		}
		else
		{
			// Post the data back to paypal

			fputs($fp, "POST $urlParsed[path] HTTP/1.1\r\n");
			fputs($fp, "Host: $urlParsed[host]\r\n");
			fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
			fputs($fp, "Content-length: " . strlen($postString) . "\r\n");
			fputs($fp, "Connection: close\r\n\r\n");
			fputs($fp, $postString . "\r\n\r\n");

			// loop through the response from the server and append to variable
			while(!feof($fp))
			{
				$this->ipnResponse .= fgets($fp, 1024);
			}

		 	fclose($fp); // close connection
		}

		if (eregi("VERIFIED", $this->ipnResponse))
		{
		 	// Valid IPN transaction.
		 	$this->logResults(true);
		 	return true;
		}
		else
		{
		 	// Invalid IPN transaction.  Check the log for details.
			$this->lastError = "IPN Validation Failed . $urlParsed[path] : $urlParsed[host]";
			$this->logResults(false);
			return false;
		}
	}
}
PaymentGateway.php
這個裡面又是什麼XD,
PaymentGateway.php檔案代碼比較多
還是我寄給你呢!?
把檔案寄給我看看吧!
有載點嗎??
我信箱 chingfeng18@hotmail.com

1 個回答

2
老鷹(eagle)
iT邦高手 1 級 ‧ 2014-01-06 13:37:28
最佳解答

joey09533225提到:
$myPaypal->addField('amount', '100');
$myPaypal->addField('custom', 'muri-khao');

如果要更改可以使用POST或者GET的方法

&lt;pre class="c" name="code">
&lt;?php
.....
$myPaypal->addField('amount', $_POST['amount']); 
$myPaypal->addField('custom', $_POST['custom']);
....
?>
&lt;form method="post">
&lt;input type="text" name="amount">
&lt;input type="text" name="custom">
&lt;input type="submit" value="送出">
&lt;/form>

不知道老鷹有沒有誤會題意疑惑

joey09533225提到:
真不好意思 忘記還有個問題:
我還想加入:如果交易完成可不可以發api資訊回去給我的網站
例:(http://12.34.56.78/api?Password=banana&Player=Aces123&Amount=1000)

一般API都會回傳資料吧!
譬如老鷹最近再研究ebay api,我送資料過去,不管成功與否,都會回傳資料,告訴你成功或者錯誤訊息.
paypal api 因該也會吧!
把它回傳訊息,存入樓主你的資料庫,因該就OK了!

或是也可以寫成下拉式的amount選取單 這樣會不會比較好寫api回網站的數值

其實都可以,一種是固定選項 一種是USER KEY IN

我要發表回答

立即登入回答