paypal支付过程



1.

表单发送

<html>
				<body>
					You will be redirected to the PayPal website in a few seconds.
					<img src="http://ecommerce.onfancy.com/skin/default/images/loader.gif" />
					<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" id="paypal_standard_checkout" name="paypal_standard_checkout" method="POST">
						<input id="business" name="business" value="zqy234api1-facilitator@126.com" type="hidden"/>
						<input id="invoice" name="invoice" value="En000000007" type="hidden"/>
						<input id="currency_code" name="currency_code" value="USD" type="hidden"/>
						<input id="paymentaction" name="paymentaction" value="sale" type="hidden"/>
						<input id="return" name="return" value="http://ecommerce.onfancy.com/paypal/standard/success" type="hidden"/>
						<input id="cancel_return" name="cancel_return" value="http://ecommerce.onfancy.com/paypal/standard/cancel" type="hidden"/>
						<input id="notify_url" name="notify_url" value="http://ecommerce.onfancy.com/paypal/ipn" type="hidden"/>

						<input id="cpp_header_image" name="cpp_header_image" value="http://ecommerce.onfancy.com/skin/default/images/onfancy-logo.gif" type="hidden"/>
						<input id="item_name" name="item_name" value="OnFancy" type="hidden"/>
						<input id="charset" name="charset" value="utf-8" type="hidden"/>
						<input id="" name="amount" value="9.99" type="hidden"/>
						<input id="tax" name="tax" value="0" type="hidden"/>
						<input id="shipping" name="shipping" value="0.00" type="hidden"/>
						<input id="discount_amount" name="discount_amount" value="0.00" type="hidden"/>
						<input id="item_number_1" name="item_number_1" value="huang-grdx01016 -042" type="hidden"/>
										<input id="item_name_1" name="item_name_1" value=" Fashion  Leather  Twining Watch " type="hidden"/>
										<input id="quantity_1" name="quantity_1" value="3" type="hidden"/>
										<input id="amount_1" name="amount_1" value="29.99" type="hidden"/><input id="item_number_2" name="item_number_2" value="grd03014 lhx-000053" type="hidden"/>
										<input id="item_name_2" name="item_name_2" value="Fashion Leisure Contracted Bracelet Watch" type="hidden"/>
										<input id="quantity_2" name="quantity_2" value="1" type="hidden"/>
										<input id="amount_2" name="amount_2" value="29.99" type="hidden"/><input id="item_number_3" name="item_number_3" value="gzxy0667" type="hidden"/>
										<input id="item_name_3" name="item_name_3" value="Fashion Contrast Color Deep V-neck Sleeveless Chiffon Dress" type="hidden"/>
										<input id="quantity_3" name="quantity_3" value="2" type="hidden"/>
										<input id="amount_3" name="amount_3" value="19.99" type="hidden"/><input id="item_number_4" name="item_number_4" value="lhx-000018" type="hidden"/>
										<input id="item_name_4" name="item_name_4" value="Retro Vintage Hand Rope Cross Mutiple leather Watch " type="hidden"/>
										<input id="quantity_4" name="quantity_4" value="1" type="hidden"/>
										<input id="amount_4" name="amount_4" value="29.99" type="hidden"/><input id="item_number_5" name="item_number_5" value="grande0032" type="hidden"/>
										<input id="item_name_5" name="item_name_5" value="Wholesale lovely design cartoon Silicone Cover Case For Samsung i9000 Galaxy S with hot sale" type="hidden"/>
										<input id="quantity_5" name="quantity_5" value="1" type="hidden"/>
										<input id="amount_5" name="amount_5" value="9.99" type="hidden"/>
						<input id="cmd" name="cmd" value="_cart" type="hidden"/>
						<input id="upload" name="upload" value="1" type="hidden"/>
						<input id="tax_cart" name="tax_cart" value="0" type="hidden"/>
						<input id="discount_amount_cart" name="discount_amount_cart" value="0" type="hidden"/>
						<input id="city" name="city" value="2121" type="hidden"/>
							<input id="country" name="country" value="US" type="hidden"/>
							<input id="email" name="email"  value="q@q.com" type="hidden"/>
							<input id="first_name" name="first_name" value="11" type="hidden"/>
							<input id="last_name" name="last_name" value="22" type="hidden"/>
							<input id="zip" name="zip" value="2121" type="hidden"/>
							<input id="state" name="state" value="2121" type="hidden"/>
							<input id="address1" name="address1" value="231212" type="hidden"/>
							<input id="address2" name="address2" value="12121" type="hidden"/>
							<input id="address_override" name="address_override" value="0" type="hidden"/>
							
						<span class="field-row">
						<input id="submit_to_paypal_button_ffe6e6319afa1dc2e9e4d822e58ca9ca" name="" value="Click here if you are not redirected within 10 seconds..." type="submit" class=" submit"/>
						</span>
					</form>
					<script type="text/javascript">
						function func(){
							document.getElementById("paypal_standard_checkout").submit();
						}
						window.οnlοad=func;
					</script>
				</body>
			</html>


2.消息IPN验证


<?php
/*
存放一些基本的非数据库数据。
一般都是数组设置。

*/
namespace frontend\block\paypal\ipn;
use Yii; 
use frontend\block\Blockbase;
use frontend\models\core\Request;
use frontend\models\core\Url;
use frontend\models\core\Config;
use frontend\models\core\Currency;
use frontend\models\core\Session;
use frontend\models\core\Store;
use frontend\models\core\Date;
use frontend\models\core\Order;
use frontend\models\core\Image;
use frontend\models\core\Help;
use common\models\paypal\Ipnmessage;
use common\models\checkout\Salesorder;
class Index extends Blockbase
{
	
	public $_post ;
	public $_order;
	
	 /**
     * PayPal payment status possible values
     *
     * @var string
     */
    
	
	public function __construct(){
	
	}
	
	
	public function getLastData(){
		# 对传递过来的数据进行处理
		$this->processData();
	}
	# IPN消息过来后,把这个消息转换成get链接方式
	public function getPostUrl(){
		$sReq = '';
		$postData = Request::post();
		$this->_post = $postData;
		if($postData){
			foreach ($postData as $k => $v) {
				$sReq .= '&'.$k.'='.urlencode($v);
			}
		}
		$sReq .= "&cmd=_notify-validate";
		$sReq = substr($sReq, 1);
		$paypal = Config::param("payment_method");
		$submitAction = $paypal['paypal']['payments_standard']['redirect_url'];
		$getUrl = $submitAction."?".$sReq;
		return $getUrl;
	}
	# 处理数据
	public function processData(){
		# 得到Get参数链接
		$getUrl = $this->getPostUrl();
		# 访问Get链接,进行验证数据是否由paypal传递过来。
		$returnData = $this->curlGet($getUrl);
		# Log
		Yii::$app->mylog->log($getUrl);
		Yii::$app->mylog->log($returnData);
		if($returnData == 'VERIFIED'){
			Yii::$app->mylog->log('VERIFIED');
			# 验证IPN是否重复
			if($this->isNotDuplicate()){
				# 验证数据是否被篡改。
				if($this->isNotDistort()){
					# 
					Yii::$app->mylog->log('isNotDistort');
					$this->updateOrder();
					
				}else{
					# 如果数据和订单数据不一致,而且,支付状态为成功,则此订单
					# 标记为可疑的。
					Yii::$app->mylog->log('isNotDistort');
					$this->updateOrder(Order::ORDER_SUSPECTED_FRAUD);
				}
				
			}
			
		}else if($returnData == 'INVALID'){
			// 验证失败
		}
		
	}
	
	public function registerPaymentCapture(){
	
	}
	public function updateOrder($orderstatus = ''){
		# 设置最后的order id
		//if($this->_order->order_id){
		//	Yii::$app->mylog->log("session_order_id".$this->_order->order_id);
		//	Order::setLastPaymentOrderId($this->_order->order_id);
		//	Yii::$app->mylog->log("session_getorder_id".Order::getLastPaymentOrderId);
		//}
		
		# 设置订单数据
		if($this->_post['txn_type']){
			$this->_order->txn_type = $this->_post['txn_type'];
		}
		if($this->_post['txn_id']){
			$this->_order->txn_id = $this->_post['txn_id'];
		}
		if($this->_post['payer_id']){
			$this->_order->payer_id = $this->_post['payer_id'];
		}
		if($this->_post['ipn_track_id']){
			$this->_order->ipn_track_id = $this->_post['ipn_track_id'];
		}
		if($this->_post['receiver_id']){
			$this->_order->receiver_id = $this->_post['receiver_id'];
		}
		if($this->_post['verify_sign']){
			$this->_order->verify_sign = $this->_post['verify_sign'];
		}
		if($this->_post['charset']){
			$this->_order->charset = $this->_post['charset'];
		}
		if($this->_post['payment_fee']){
			$this->_order->payment_fee = $this->_post['payment_fee'];
		}
		if($this->_post['payment_type']){
			$this->_order->payment_type = $this->_post['payment_type'];
		}
		
		if($orderstatus){
			# 指定了订单状态
			$this->_order->order_status = $orderstatus;
			$this->_order->save();
			Yii::$app->mylog->log('save_'.$orderstatus);
		}else{
			switch (strtolower($this->_post['payment_status'])) {
					case Order::PAYMENTSTATUS_COMPLETED :
						
						$this->_order->order_status = Order::ORDER_PROCESSING;
						$this->_order->save();
						Yii::$app->mylog->log('save_'.Order::ORDER_PROCESSING);
						break;
					case Order::PAYMENTSTATUS_FAILED :
						$this->_order->order_status = Order::ORDER_CANCELED;
						$this->_order->save();
						Yii::$app->mylog->log('save_'.Order::ORDER_CANCELED);
						break;
					case Order::PAYMENTSTATUS_REFUNDED :
						$this->_order->order_status = Order::ORDER_CANCELED;
						$this->_order->save();
						Yii::$app->mylog->log('save_'.Order::ORDER_CANCELED);
						break;
						
					default:
						
			
			}
		}		
		//PAYMENTSTATUS_COMPLETED
		
		/*
		$this->_post;
		$increment_id = $this->_post['invoice'];
		
		$payment_fee = $this->_post['payment_fee'];
		$payment_type = $this->_post['payment_type'];
		$payment_gross = $this->_post['payment_gross'];
		$payment_status = $this->_post['payment_status'];
		
		$mc_currency = $this->_post['mc_currency'];
		
		$txn_id = $this->_post['txn_id'];
		$txn_type = $this->_post['txn_type'];
		$payer_id = $this->_post['payer_id'];
		$ipn_track_id = $this->_post['ipn_track_id'];
		$receiver_id = $this->_post['receiver_id'];
		$verify_sign = $this->_post['verify_sign'];
		$charset = $this->_post['charset'];
		*/
	}
	
	
	# 验证订单数据是否被篡改。
	# 通过订单号找到订单,查看是否存在
	# 验证邮件地址,订单金额是否准确。
	public function isNotDistort(){
		$increment_id = $this->_post['invoice'];
		$payment_gross = $this->_post['payment_gross'];
		$mc_currency = $this->_post['mc_currency'];
		
		
		if($increment_id && $payment_gross && $mc_currency){
			$this->_order = Salesorder::find()->where(['increment_id'=>$increment_id])->one();
			if($this->_order->order_id){
				$base_grand_total = $this->_order['base_grand_total'];
				$currentCurrencyGrandTotal = Currency::getCurrentPertyPrice($base_grand_total,$mc_currency);
				if((float)$currentCurrencyGrandTotal == (float)$payment_gross ){
					return true;
				}else{
					return false;
				}
			}
		}
		return false;
	}
	
	# 判断是否重复,如果不重复,把当前的插入。
	public function isNotDuplicate(){
		
		$ipn = Ipnmessage::find()
				->asArray()
				->where([
				'txn_id'=>$this->_post['txn_id'],
				'payment_status'=>$this->_post['payment_status'],
				])
				->one();
		Yii::$app->mylog->log(json_encode($ipn));
		if(is_array($ipn) && !empty($ipn)){
			
			return false;
		}else{
			
			$Ipnmessage = new Ipnmessage();
			$Ipnmessage->txn_id = $this->_post['txn_id'];
			$Ipnmessage->payment_status = $this->_post['payment_status'];
			$Ipnmessage->updated_at = Date::getCurrentDateTime();
			$Ipnmessage->Insert();
			Yii::$app->mylog->log('5555');
			return true;
		}
	
	}
	
	
/*	
https://www.sandbox.paypal.com/cgi-bin/webscr?
invoice=En000000012&
first_name=test&
mc_shipping=0.00&
mc_currency=USD&
payer_status=verified&
payment_fee=7.26&
address_status=unconfirmed&
payment_gross=239.91&
address_zip=266326&
address_country_code=CN&
txn_type=cart&
num_cart_items=5&
mc_handling=0.00&
verify_sign=AeBA9KWvSFZADUHw4Q4V6axFbyTqAH-wn0-4P9aQw24Hn.LBgF-spwg0&
payer_id=FKL4V7D5GCACY&
charset=windows-1252&
tax1=0.00&
receiver_id=H4KXD885J8LV2&
tax2=0.00&
tax3=0.00&
tax4=0.00&
tax5=0.00&
mc_handling1=0.00&
mc_handling2=0.00&mc_handling3=0.00&mc_handling4=0.00&mc_handling5=0.00&
item_name1=+Fashion++Leather++Twining+Watch&tax=0.00&
item_name2=Fashion+Leisure+Contracted+Bracelet+Watch&
item_name3=Fashion+Contrast+Color+Deep+V-neck+Sleeveless+Chiffon+Dress&
item_name4=Retro+Vintage+Hand+Rope+Cross+Mutiple+leather+Watch&
payment_type=instant&
item_name5=Vintage+Wristband+Bracelet+leather+Rivet+Stud+Watch&
address_street=%1A%1A%1A%1A%1A%1A%1A%1A%1A%1A%1A%1A%1A%0D%0A301%1A%1A&
mc_shipping1=0.00&mc_shipping2=0.00&mc_shipping3=0.00&
txn_id=46Y09249RY633602D&mc_shipping4=0.00&mc_shipping5=0.00&
mc_gross_1=89.97&quantity1=3&mc_gross_2=29.99&quantity2=1&
item_number1=huang-grdx01016+-042&protection_eligibility=Eligible&
mc_gross_3=59.97&quantity3=3&item_number2=grd03014+lhx-000053&
mc_gross_4=29.99&quantity4=1&item_number3=gzxy0667&mc_gross_5=29.99&
quantity5=1&custom=&item_number4=lhx-000018&item_number5=lhx-000026&
business=zqy234api1-facilitator%40126.com&residence_country=US&
last_name=facilitator&address_state=%1A%1A%1A&
payer_business_name=test+facilitator%27s+Test+Store&
payer_email=zqy234api1-facilitator-1%40126.com&
address_city=%1A%1A&payment_status=Completed&
payment_date=03%3A23%3A01+Mar+12%2C+2015+PDT&transaction_subject=&
receiver_email=zqy234api1-facilitator%40126.com&mc_fee=7.26¬ify_version=3.8&
address_country=China&mc_gross=239.91&test_ipn=1&address_name=zhao+qiyong&
ipn_track_id=cddd024cb9c68&cmd=_notify-validate
ipn_track_id 对每一个ipn消息唯一
txn_id 对每一个交易唯一
*/
	
	
	
	public function curlGet($url){
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_VERBOSE, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT,30000);
		// Turn off the server and peer verification (TrustManager Concept).
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// Get response from the server.
		$httpResponse = curl_exec($ch);
		return $httpResponse;
	}
	
}


3.在成功页面,显示即可















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值