提交 a685ae0c 编写于 作者: T Terry

apphtml5部分支付更改

上级 82cbaa09
......@@ -50,7 +50,12 @@ class StandardController extends AppfrontController
}
public function actionCancel()
{
Yii::$service->order->cancel();
return Yii::$service->url->redirectByUrlKey('checkout/onepage');
}
......@@ -91,12 +96,7 @@ class StandardController extends AppfrontController
}
}
public function actionCancel()
{
Yii::$service->order->cancel();
return Yii::$service->url->redirectByUrlKey('checkout/onepage');
}
public function actionTest()
{
......
......@@ -55,7 +55,8 @@ class Placeorder
// 将购物车数据,生成订单。
$innerTransaction = Yii::$app->db->beginTransaction();
try {
$genarateStatus = Yii::$service->order->generateOrderByCart($this->_billing, $this->_shipping_method, $this->_payment_method);
# 生成订单,扣除库存,但是,不清空购物车。
$genarateStatus = Yii::$service->order->generateOrderByCart($this->_billing, $this->_shipping_method, $this->_payment_method,false);
if ($genarateStatus) {
// 得到当前的订单信息
//$orderInfo = Yii::$service->order->getCurrentOrderInfo();
......
......@@ -35,6 +35,11 @@ class Placeorder
public function getLastData()
{
$post = Yii::$app->request->post();
$token = Yii::$app->request->get('token');
if(!$token){
Yii::$service->helper->errors->add('token can not empty');
return false;
}
if (is_array($post) && !empty($post)) {
$post = \Yii::$service->helper->htmlEncode($post);
// 设置paypal快捷支付
......@@ -54,7 +59,7 @@ class Placeorder
// 将购物车数据,生成订单,生成订单后,不清空购物车,不扣除库存,在支付成功后在清空购物车。
$innerTransaction = Yii::$app->db->beginTransaction();
try {
$genarateStatus = Yii::$service->order->generateOrderByCart($this->_billing, $this->_shipping_method, $this->_payment_method, false);
$genarateStatus = Yii::$service->order->generateOrderByCart($this->_billing, $this->_shipping_method, $this->_payment_method, false,$token);
if ($genarateStatus) {
$innerTransaction->commit();
} else {
......@@ -66,10 +71,10 @@ class Placeorder
//echo 22;
if ($genarateStatus) {
// 得到当前的订单信息
$doExpressCheckoutReturn = $this->doExpressCheckoutPayment();
$doExpressCheckoutReturn = $this->doExpressCheckoutPayment($token);
//echo 333;
if ($doExpressCheckoutReturn) {
$ExpressOrderPayment = Yii::$service->payment->paypal->updateExpressOrderPayment($doExpressCheckoutReturn);
$ExpressOrderPayment = Yii::$service->payment->paypal->updateExpressOrderPayment($doExpressCheckoutReturn,$token);
// 如果支付成功,并把信息更新到了订单数据中,则进行下面的操作。
//echo 444;
if ($ExpressOrderPayment) {
......@@ -107,10 +112,10 @@ class Placeorder
return false;
}
public function doExpressCheckoutPayment()
public function doExpressCheckoutPayment($token)
{
$methodName_ = 'DoExpressCheckoutPayment';
$nvpStr_ = Yii::$service->payment->paypal->getExpressCheckoutPaymentNvpStr();
$nvpStr_ = Yii::$service->payment->paypal->getExpressCheckoutPaymentNvpStr($token);
//echo $nvpStr_;exit;
$DoExpressCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_);
//var_dump($DoExpressCheckoutReturn);
......
......@@ -424,14 +424,14 @@ class Review
public function expressReview()
{
$setTokenStatus = Yii::$service->payment->paypal->setExpressToken();
$setPayerIDStatus = Yii::$service->payment->paypal->setExpressPayerID();
if (!$setTokenStatus) {
$getToken = Yii::$service->payment->paypal->getExpressToken();
$getPayerID = Yii::$service->payment->paypal->getExpressPayerID();
if (!$getToken) {
Yii::$service->page->message->AddError('paypal express token is empty');
return [];
}
if (!$setPayerIDStatus) {
if (!$getPayerID) {
Yii::$service->page->message->AddError('paypal express PayerID is empty');
return [];
......
......@@ -19,6 +19,10 @@ class Start
{
public function startExpress()
{
$checkStatus = $this->checkStockQty();
if(!$checkStatus){
return;
}
$methodName_ = 'SetExpressCheckout';
$nvpStr_ = Yii::$service->payment->paypal->getExpressTokenNvpStr();
//echo $nvpStr_;exit;
......@@ -26,9 +30,14 @@ class Start
//var_dump($SetExpressCheckoutReturn);
if (strtolower($SetExpressCheckoutReturn['ACK']) == 'success') {
$token = $SetExpressCheckoutReturn['TOKEN'];
$redirectUrl = Yii::$service->payment->paypal->getSetExpressCheckoutUrl($token);
Yii::$service->url->redirect($redirectUrl);
exit;
# 生成订单,订单中只有id,increment_id,token 三个字段有值。
if($token){
if(!Yii::$service->order->generatePPExpressOrder($token)){
return false;
}
$redirectUrl = Yii::$service->payment->paypal->getSetExpressCheckoutUrl($token);
return Yii::$service->url->redirect($redirectUrl);
}
} elseif (strtolower($SetExpressCheckoutReturn['ACK']) == 'failure') {
echo $SetExpressCheckoutReturn['L_LONGMESSAGE0'];
} else {
......@@ -36,8 +45,31 @@ class Start
}
}
// 首先验证购物车中是否存在产品
//public function validateCart(){
//}
// 检查购物车中产品的库存。此步只是初步检查,在快捷支付完成返回网站的时候,生成订单的时候,还要进一步检查产品库存,
// 因为在支付的过程中,产品可能被买走。
public function checkStockQty(){
$stockCheck = Yii::$service->product->stock->checkItemsQty();
//var_dump($stockCheck);exit;
if(!$stockCheck){
Yii::$service->url->redirectByUrlKey('checkout/cart');
return false;
}else{
if(isset($stockCheck['stockStatus'])){
if($stockCheck['stockStatus'] == 2){
$outStockProducts = $stockCheck['outStockProducts'];
if(is_array($outStockProducts) && !empty($outStockProducts)){
foreach($outStockProducts as $outStockProduct){
$product_name = Yii::$service->store->getStoreAttrVal($outStockProduct['product_name'], 'name');
Yii::$service->helper->errors->add('product: ['.$product_name.'] is stock out.');
}
Yii::$service->page->message->addByHelperErrors();
Yii::$service->url->redirectByUrlKey('checkout/cart');
return false;
}
}
}
}
return true;
}
}
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\apphtml5\modules\Payment\block\paypal\standard;
use Yii;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class Placeorder
{
/**
* 用户的账单地址信息,通过用户传递的信息计算而来。
*/
public $_billing;
public $_address_id;
/**
* 用户的货运方式.
*/
public $_shipping_method;
/**
* 用户的支付方式.
*/
public $_payment_method;
public function getLastData()
{
$token = Yii::$app->request->get('token');
if(!$token){
Yii::$service->helper->errors->add('token can not empty');
return false;
}
//echo '33'.'<br>';
echo $token.'<br>';
// 得到当前的订单信息
$doExpressCheckoutReturn = $this->doExpressCheckoutPayment($token);
if ($doExpressCheckoutReturn) {
//var_dump($doExpressCheckoutReturn);
$ExpressOrderPayment = Yii::$service->payment->paypal->updateExpressOrderPayment($doExpressCheckoutReturn,$token);
// 如果支付成功,并把信息更新到了订单数据中,则进行下面的操作。
//echo 444;
//var_dump($ExpressOrderPayment);
if ($ExpressOrderPayment) {
//echo 55;
// 支付成功后,在清空购物车数据。而不是在生成订单的时候。
Yii::$service->cart->clearCartProductAndCoupon();
// (删除)支付成功后,扣除库存。
// (删除)Yii::$service->product->stock->deduct();
// echo 555;
// 发送新订单邮件
// 扣除库存和优惠券
// 在生成订单的时候已经扣除了。参看order service GenerateOrderByCart() function
// 得到支付跳转前的准备页面。
$paypal_standard = Yii::$service->payment->paypal->standard_payment_method;
//echo '<br>';
//echo $paypal_standard;
$successRedirectUrl = Yii::$service->payment->getStandardSuccessRedirectUrl($paypal_standard);
//echo '<br>';
//echo $successRedirectUrl;
Yii::$service->url->redirect($successRedirectUrl);
return true;
}
}else{
Yii::$service->url->redirectByUrlKey('checkout/onepage');
}
// 如果订单支付过程中失败,将订单取消掉
if (!$doExpressCheckoutReturn || !$ExpressOrderPayment) {
Yii::$service->order->cancel();
}
Yii::$service->page->message->addByHelperErrors();
return false;
}
public function doExpressCheckoutPayment($token)
{
$methodName_ = 'DoExpressCheckoutPayment';
$nvpStr_ = Yii::$service->payment->paypal->getExpressCheckoutPaymentNvpStr($token);
//echo '<br/>nvpStr_:<br/>"'.$nvpStr_.'<br/><br/>';
$DoExpressCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_);
//echo '<br/>DoExpressCheckoutReturn <br/><br/>';
//var_dump($DoExpressCheckoutReturn);
//echo '<br/>DoExpressCheckoutReturn <br/><br/>';
//exit;
if (strstr(strtolower($DoExpressCheckoutReturn['ACK']), 'success')) {
return $DoExpressCheckoutReturn;
} else {
if ($DoExpressCheckoutReturn['ACK'] == 'Failure') {
$message = $DoExpressCheckoutReturn['L_LONGMESSAGE0'];
// 添加报错信息。
//Message::error($message);
Yii::$service->helper->errors->add($message);
} else {
Yii::$service->helper->errors->add('paypal express payment error.');
}
return false;
}
}
/**
* @property $post | Array
* 登录用户,保存货运地址到customer address ,然后把生成的
* address_id 写入到cart中。
* shipping method写入到cart中
* payment method 写入到cart中 updateCart
*/
public function updateAddress($post)
{
return Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method);
}
/**
* 如果是游客,那么保存货运地址到购物车表。
*/
/*
public function updateCart(){
if(Yii::$app->user->isGuest){
return Yii::$service->cart->updateGuestCart($this->_billing,$this->_shipping_method,$this->_payment_method);
}else{
return Yii::$service->cart->updateLoginCart($this->_address_id,$this->_shipping_method,$this->_payment_method);
}
}
*/
/**
* @property $post | Array
* @return bool
* 检查前台传递的信息是否正确。同时初始化一部分类变量
*/
public function checkOrderInfoAndInit($post)
{
$address_one = '';
$billing = isset($post['billing']) ? $post['billing'] : '';
if (!Yii::$service->order->checkRequiredAddressAttr($billing)) {
return false;
}
$this->_billing = $billing;
$shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : '';
$payment_method = isset($post['payment_method']) ? $post['payment_method'] : '';
// 验证货运方式
if (!$shipping_method) {
Yii::$service->helper->errors->add('shipping method can not empty');
return false;
} else {
if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) {
Yii::$service->helper->errors->add('shipping method is not correct');
return false;
}
}
$this->_shipping_method = $shipping_method;
$this->_payment_method = $payment_method;
Yii::$service->payment->setPaymentMethod($this->_payment_method);
return true;
}
}
......@@ -17,120 +17,26 @@ use Yii;
*/
class Start
{
public function getLastData()
public function startExpress()
{
$loaderImg = Yii::$service->image->getImgUrl('images/loader.gif', 'apphtml5');
$paypalLogoImg = Yii::$service->image->getImgUrl('custom/logo.png', 'apphtml5');
$increment_id = Yii::$service->order->getSessionIncrementId();
if ($increment_id) {
$order = Yii::$service->order->getInfoByIncrementId($increment_id);
if ($order) {
//var_dump($order);exit;
Yii::$service->payment->setPaymentMethod($order['payment_method']);
$success_redirect_url = Yii::$service->payment->getStandardSuccessRedirectUrl();
$cancel_url = Yii::$service->payment->getStandardCancelUrl();
$ipn_url = Yii::$service->payment->getStandardIpnUrl();
$store_name = Yii::$service->store->currentStore;
$payment_action = 'sale';
$cmd = '_cart';
$upload = 1;
$tax = 0.00;
$tax_cart = 0.00;
$payment_url = Yii::$service->payment->getStandardPaymentUrl();
$account = Yii::$service->payment->getStandardAccount();
return [
'loader_img' => $loaderImg,
'paypal_logo_img' => $paypalLogoImg,
'order' => $order,
'success_redirect_url' => $success_redirect_url,
'cancel_url' => $cancel_url,
'ipn_url' => $ipn_url,
'store_name' => $store_name,
'payment_action' => $payment_action,
'cmd' => $cmd,
'upload' => $upload,
'tax' => $tax,
'tax_cart' => $tax_cart,
'payment_url' => $payment_url,
'account' => $account,
'product_items_and_shipping' => $this->getProductItemsAndShipping($order),
'address_html' => $this->getAddressHtml($order),
];
}
$methodName_ = 'SetExpressCheckout';
$nvpStr_ = Yii::$service->payment->paypal->getStandardTokenNvpStr();
//echo $nvpStr_;exit;
// 通过接口,得到token信息
$SetExpressCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_);
//var_dump($SetExpressCheckoutReturn);
if (strtolower($SetExpressCheckoutReturn['ACK']) == 'success') {
$token = $SetExpressCheckoutReturn['TOKEN'];
$increment_id = Yii::$service->order->getSessionIncrementId();
# 将token写入到订单中
Yii::$service->order->updateTokenByIncrementId($increment_id,$token);
$redirectUrl = Yii::$service->payment->paypal->getSetStandardCheckoutUrl($token);
Yii::$service->url->redirect($redirectUrl);
return;
} elseif (strtolower($SetExpressCheckoutReturn['ACK']) == 'failure') {
echo $SetExpressCheckoutReturn['L_LONGMESSAGE0'];
} else {
var_dump($SetExpressCheckoutReturn);
}
}
public function getAddressHtml($order)
{
$stateCode = $order['customer_address_state'];
$countryCode = $order['customer_address_country'];
$country = Yii::$service->helper->country->getCountryNameByKey($countryCode);
$state = Yii::$service->helper->country->getStateByContryCode($countryCode, $stateCode);
$str = '
<input id="city" name="city" value="'.$order['customer_address_city'].'" type="hidden"/>
<input id="country" name="country" value="'.$country.'" type="hidden"/>
<input id="email" name="email" value="'.$order['customer_email'].'" type="hidden"/>
<input id="first_name" name="first_name" value="'.$order['customer_firstname'].'" type="hidden"/>
<input id="last_name" name="last_name" value="'.$order['customer_lastname'].'" type="hidden"/>
<input id="zip" name="zip" value="'.$order['customer_address_zip'].'" type="hidden"/>
<input id="state" name="state" value="'.$state.'" type="hidden"/>
<input id="address1" name="address1" value="'.$order['customer_address_street1'].'" type="hidden"/>
<input id="address2" name="address2" value="'.$order['customer_address_street2'].'" type="hidden"/>
<input id="address_override" name="address_override" value="0" type="hidden"/>
';
return $str;
}
public function getProductItemsAndShipping($order)
{
$items = $order['items'];
$str = '';
$i = 1;
foreach ($items as $item) {
$sku = isset($item['sku']) ? $item['sku'] : '';
$name = isset($item['name']) ? $item['name'] : '';
$qty = isset($item['qty']) ? $item['qty'] : '';
$price = isset($item['price']) ? str_replace(',', '', number_format($item['price'], 2)) : number_format($item['price'], 0);
$custom_option_info = isset($item['custom_option_info']) ? $item['custom_option_info'] : '';
if ($sku && $qty && $price) {
$str .= '
<input id="item_number_'.$i.'" name="item_number_'.$i.'" value="'.$sku.'" type="hidden"/>
<input id="item_name_'.$i.'" name="item_name_'.$i.'" value="'.$name.'" type="hidden"/>
<input id="quantity_'.$i.'" name="quantity_'.$i.'" value="'.$qty.'" type="hidden"/>
<input id="amount_'.$i.'" name="amount_'.$i.'" value="'.$price.'" type="hidden"/>
';
if (is_array($custom_option_info) && !empty($custom_option_info)) {
$j = 0;
foreach ($custom_option_info as $co_label=>$co_value) {
$str .= '
<input id="on'.$j.'_'.$i.'" name="on'.$j.'_'.$i.'" type="hidden" value="'.$co_label.'" />
<input id="os'.$j.'_'.$i.'" name="os'.$j.'_'.$i.'" type="hidden" value="'.$co_value.'" />
';
$j++;
}
}
}
$i++;
}
$shipping_total = $order['shipping_total'];
$shipping_total = str_replace(',', '', number_format($shipping_total, 2));
$shipping_method = $order['shipping_method'];
$shipping_label = Yii::$service->shipping->getShippingLabelByMethod($shipping_method);
$str .= '
<input id="item_number_'.$i.'" name="item_number_'.$i.'" value="'.$shipping_label.'" type="hidden"/>
<input id="item_name_'.$i.'" name="item_name_'.$i.'" value="'.$shipping_method.'" type="hidden"/>
<input id="quantity_'.$i.'" name="quantity_'.$i.'" value="1" type="hidden"/>
<input id="amount_'.$i.'" name="amount_'.$i.'" value="'.$shipping_total.'" type="hidden"/>
';
return $str;
}
}
......@@ -42,4 +42,20 @@ class ExpressController extends AppfrontController
return $data;
}
}
public function actionIpn()
{
\Yii::info('paypal ipn begin', 'fecshop_debug');
$post = Yii::$app->request->post();
if (is_array($post) && !empty($post)) {
$post = \Yii::$service->helper->htmlEncode($post);
ob_start();
ob_implicit_flush(false);
var_dump($post);
$post_log = ob_get_clean();
\Yii::info($post_log, 'fecshop_debug');
//Yii::$service->payment->paypal->receiveIpn($post);
}
}
}
......@@ -18,65 +18,40 @@ use Yii;
*/
class StandardController extends AppfrontController
{
public $enableCsrfValidation = false;
private $use_local_certs = true;
public $enableCsrfValidation = true;
public function actionStart()
{
Yii::$service->page->theme->layoutFile = 'blank.php';
$data = $this->getBlock()->getLastData();
if (is_array($data) && !empty($data)) {
return $this->render($this->action->id, $data);
} else {
return Yii::$service->url->redirectByUrlKey('checkout/onepage');
}
return $this->getBlock()->startExpress();
}
// 2.Review 从paypal确认后返回
public function actionReview()
{
$this->getBlock('placeorder')->getLastData();
}
public function actionIpn()
{
\Yii::info('paypal ipn begin', 'fecshop_debug');
$post = Yii::$app->request->post();
if (is_array($post) && !empty($post)) {
$post = \Yii::$service->helper->htmlEncode($post);
ob_start();
ob_implicit_flush(false);
var_dump($post);
$post_log = ob_get_clean();
\Yii::info($post_log, 'fecshop_debug');
Yii::$service->payment->paypal->receiveIpn($post);
//Yii::$service->payment->paypal->receiveIpn($post);
}
}
public function actionCancel()
{
Yii::$service->order->cancel();
return Yii::$service->url->redirectByUrlKey('checkout/onepage');
}
public function actionTest()
{
$url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?invoice=1000000124&first_name=test&discount=7.11&mc_shipping=0.00&mc_currency=EUR&payer_status=verified&shipping_discount=0.00&payment_fee=&address_status=unconfirmed&payment_gross=&settle_currency=USD&address_zip=212121&address_country_code=FR&txn_type=cart&num_cart_items=3&mc_handling=0.00&verify_sign=AuRlNZvMOhdn8iDWY5YoMB9iRTDzAZppqkxKbGiGEUvmwrFAdvscLpXK&payer_id=FKL4V7D5GCACY&option_selection2_1=L&option_selection2_2=L&charset=windows-1252&tax1=0.00&receiver_id=H4KXD885J8LV2&tax2=0.00&tax3=0.00&mc_handling1=0.00&mc_handling2=0.00&mc_handling3=0.00&item_name1=Reindeer+Pattern+Glitter+Christmas+Dress&tax=0.00&item_name2=Sweet+Polka+Dot+Open+Back+Summer+Dress+For+Women&item_name3=fast_shipping&payment_type=instant&mc_shipping1=0.00&address_street=2121%0D%0A23232&mc_shipping2=0.00&mc_shipping3=0.00&txn_id=9NN80505PR451120Y&exchange_rate=1.29364&mc_gross_1=40.34&quantity1=2&mc_gross_2=30.69&quantity2=1&item_number1=22221&protection_eligibility=Eligible&mc_gross_3=18.51&quantity3=1&item_number2=sk0003&item_number3=Fast+Shipping%28+5-10+work+days%29&custom=&option_selection1_1=black&option_selection1_2=red&business=zqy234api1-facilitator%40126.com&residence_country=US&last_name=facilitator&address_state=Hautes-Alpes&payer_business_name=test+facilitator%27s+Test+Store&payer_email=zqy234api1-facilitator-1%40126.com&option_name2_1=My+size&option_name2_2=size&settle_amount=103.09&address_city=2121&payment_status=Completed&payment_date=22%3A40%3A06+Feb+20%2C+2017+PST&transaction_subject=&receiver_email=zqy234api1-facilitator%40126.com&mc_fee=2.74&notify_version=3.8&shipping_method=Default&address_country=France&mc_gross=82.43&test_ipn=1&insurance_amount=0.00&address_name=1111+22&option_name1_1=My+color&option_name1_2=color&ipn_track_id=26d73da3782c3&cmd=_notify-validate';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// This is often required if the server is missing a global cert bundle, or is using an outdated one.
if ($this->use_local_certs) {
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/cert/cacert.pem');
}
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Connection: Close']);
$res = curl_exec($ch);
echo $res;
}
}
......@@ -3,6 +3,7 @@ use fecshop\app\apphtml5\helper\Format;
?>
<div class="main container one-column">
<div class="col-main">
<?= Yii::$service->page->widget->render('flashmessage'); ?>
<?php if(is_array($cart_info) && !empty($cart_info)){ ?>
<div class="product_page">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册