提交 aea7e471 编写于 作者: T Terry

appserver paypal standard payment

上级 6d700f69
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appserver\modules\Payment;
use fecshop\app\appserver\modules\AppserverController;
use Yii;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class PaymentController extends AppserverController
{
protected $_increment_id;
protected $_order_model;
public function checkOrder()
{
//$homeUrl = Yii::$service->url->homeUrl();
$this->_increment_id = Yii::$service->order->getSessionIncrementId();
if (!$this->_increment_id) {
return [
'code' => 401,
'content' => 'order is not exist',
];
}
$this->_order_model = Yii::$service->order->GetByIncrementId($this->_increment_id);
if (!isset($this->_order_model['increment_id'])) {
return [
'code' => 401,
'content' => 'order is not exist',
];;
}
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\appserver\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->post('token');
if(!$token){
return [
'code' => 401,
'content' => 'token is empty',
];
}
// 得到当前的订单信息
$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;
//$successRedirectUrl = Yii::$service->payment->getStandardSuccessRedirectUrl($paypal_standard);
//Yii::$service->url->redirect($successRedirectUrl);
//return true;
return [
'code' => 200,
'content' => 'order payment success',
];
}else{
// 如果订单支付过程中失败,将订单取消掉
$innerTransaction = Yii::$app->db->beginTransaction();
try {
if(Yii::$service->order->cancel()){
$innerTransaction->commit();
}else{
$innerTransaction->rollBack();
}
} catch (Exception $e) {
$innerTransaction->rollBack();
}
return [
'code' => 401,
'content' => 'order payment fail',
];
}
}else{
return [
'code' => 401,
'content' => 'order payment fail',
];
}
}
/**
* @property $token | String
* 通过paypal的api接口,进行支付下单
*/
public function doExpressCheckoutPayment($token)
{
$methodName_ = 'DoExpressCheckoutPayment';
//echo $token;
$nvpStr_ = Yii::$service->payment->paypal->getExpressCheckoutPaymentNvpStr($token);
// echo $nvpStr_ ;
//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;
}
}
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appserver\modules\Payment\block\paypal\standard;
use Yii;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class Start
{
public function startExpress()
{
$methodName_ = 'SetExpressCheckout';
$return_url = Yii::$app->request->post('return_url');
$cancel_url = Yii::$app->request->post('cancel_url');
$nvpStr_ = Yii::$service->payment->paypal->getStandardTokenNvpStr('login',$return_url,$cancel_url);
//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);
return [
'code' => 200,
'content' => $redirectUrl,
];
} elseif (strtolower($SetExpressCheckoutReturn['ACK']) == 'failure') {
return [
'code' => 403,
'content' => $SetExpressCheckoutReturn['L_LONGMESSAGE0'],
];
} else {
return [
'code' => 403,
'content' => $SetExpressCheckoutReturn,
];
}
}
}
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appserver\modules\Payment\controllers;
use fecshop\app\appserver\modules\Payment\PaymentController;
use Yii;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class CheckmoneyController extends PaymentController
{
public $enableCsrfValidation = false;
/**
* 支付开始页面.
*/
public function actionStart()
{
$checkOrder = $this->checkOrder();
if($checkOrder !== true){
return $checkOrder;
}
$payment_method = isset($this->_order_model['payment_method']) ? $this->_order_model['payment_method'] : '';
if ($payment_method) {
return [
'code' => 200,
'content' => 'check order generate order success, you can contact customer Offline payment',
];
}
}
}
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appserver\modules\Payment\controllers;
use fecshop\app\appserver\modules\AppserverController;
use Yii;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class SuccessController extends AppserverController
{
public function actionIndex()
{
$increment_id = Yii::$service->order->getSessionIncrementId();
if (!$increment_id) {
return [
'code' => 401,
'content' => 'current order is not exist',
];
}
$order = Yii::$service->order->getInfoByIncrementId($increment_id);
// 清空购物车。这里针对的是未登录用户进行购物车清空。
//if (Yii::$app->user->isGuest) {
Yii::$service->cart->clearCartProductAndCoupon();
//}
// 清空session中存储的当前订单编号。
Yii::$service->order->removeSessionIncrementId();
return [
'code' => 200,
'increment_id' => $increment_id,
'order' => $order,
];
}
}
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appserver\modules\Payment\controllers\paypal;
use fecshop\app\appserver\modules\AppserverController;
use Yii;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class StandardController extends AppserverController
{
public $enableCsrfValidation = false;
/**
* 1.start部分,跳转到paypal前的部分
*/
public function actionStart()
{
return $this->getBlock()->startExpress();
}
/**
* 2.Review 从paypal确认后返回的部分
*/
public function actionReview()
{
return $this->getBlock('placeorder')->getLastData();
}
/**
* IPN,paypal消息接收部分
*/
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);
}
}
/**
* paypal 取消后的部分。
*/
/*
public function actionCancel()
{
$innerTransaction = Yii::$app->db->beginTransaction();
try {
if(Yii::$service->order->cancel()){
$innerTransaction->commit();
}else{
$innerTransaction->rollBack();
}
} catch (Exception $e) {
$innerTransaction->rollBack();
}
return Yii::$service->url->redirectByUrlKey('checkout/onepage');
}
*/
}
......@@ -567,12 +567,20 @@ class Paypal extends Service
* 这里返回的的字符串,是快捷支付部分获取token和payerId的参数。
* 通过这些参数最终得到paypal express的token和payerId
*/
public function getStandardTokenNvpStr($landingPage = 'Login')
public function getStandardTokenNvpStr($landingPage = 'Login',$return_url='',$cancel_url='')
{
$nvp_array = [];
$nvp_array['LANDINGPAGE'] = $landingPage;
$nvp_array['RETURNURL'] = Yii::$service->payment->getStandardReturnUrl('paypal_standard');
$nvp_array['CANCELURL'] = Yii::$service->payment->getStandardCancelUrl('paypal_standard');
if ($return_url) {
$nvp_array['RETURNURL'] = $return_url;
} else {
$nvp_array['RETURNURL'] = Yii::$service->payment->getStandardReturnUrl('paypal_standard');
}
if ($cancel_url) {
$nvp_array['CANCELURL'] = $cancel_url;
} else {
$nvp_array['CANCELURL'] = Yii::$service->payment->getStandardCancelUrl('paypal_standard');
}
$nvp_array['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Sale';
$nvp_array['VERSION'] = $this->version;
// 得到购物车的信息,通过购物车信息填写。
......@@ -615,6 +623,9 @@ class Paypal extends Service
{
if(!$this->expressToken){
$token = Yii::$app->request->get('token');
if(!$token){
$token = Yii::$app->request->post('token');
}
$token = \Yii::$service->helper->htmlEncode($token);
if ($token) {
$this->expressToken = $token;
......@@ -630,6 +641,9 @@ class Paypal extends Service
{
if(!$this->expressPayerID){
$PayerID = Yii::$app->request->get('PayerID');
if(!$PayerID){
$PayerID = Yii::$app->request->post('PayerID');
}
$PayerID = \Yii::$service->helper->htmlEncode($PayerID);
if ($PayerID) {
$this->expressPayerID = $PayerID;
......@@ -649,7 +663,7 @@ class Paypal extends Service
//$increment_id = Yii::$service->order->getSessionIncrementId();
//echo "\n $increment_id \n\n";
//$order = Yii::$service->order->getByIncrementId($increment_id);
echo '########'.$token.'#####';
//echo '########'.$token.'#####';
$order = Yii::$service->order->getByPaymentToken($token);
$order_cancel_status = Yii::$service->order->payment_status_canceled;
// 如果订单状态被取消,那么不能进行支付。
......@@ -678,14 +692,14 @@ class Paypal extends Service
$order['paypal_order_datetime'] = date('Y-m-d H:i:s', $DoExpressCheckoutReturn['PAYMENTINFO_0_ORDERTIME']);
$PAYMENTINFO_0_PAYMENTSTATUS = $DoExpressCheckoutReturn['PAYMENTINFO_0_PAYMENTSTATUS'];
echo $this->payment_status_completed.'##'.$PAYMENTINFO_0_PAYMENTSTATUS."<br>";
//echo $this->payment_status_completed.'##'.$PAYMENTINFO_0_PAYMENTSTATUS."<br>";
if (strtolower($PAYMENTINFO_0_PAYMENTSTATUS) == $this->payment_status_completed) {
//echo 222;
// 判断金额是否相符
echo $currency."<br/>";
echo $order['order_currency_code']."<br/>";
echo $PAYMENTINFO_0_AMT."<br/>";
echo $order['grand_total']."<br/>";
//echo $currency."<br/>";
//echo $order['order_currency_code']."<br/>";
//echo $PAYMENTINFO_0_AMT."<br/>";
//echo $order['grand_total']."<br/>";
if ($currency == $order['order_currency_code'] && $PAYMENTINFO_0_AMT == $order['grand_total']) {
//echo 222;
$order->order_status = Yii::$service->order->payment_status_processing;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册