下面以手机网站支付为例:
可先下载最新的支付宝对接 SDK https://docs.open.alipay.com/203/105910/
需 php5.5 以上环境。网站应用环境也必须要是 php5.5 以上。
下载后直接放入\ThinkPHP\Library\Vendor\Alipay 中。
特别注意的是密钥的生成 https://docs.open.alipay.com/common/104740
利用工具生成的公钥和查看的公钥是不一样的,需要特别注意。
新建支付服务类文件\Service\AlipayService.class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <?php namespace Home\Service;
class AlipayService {
function __construct() { $payConfig = array( 'app_id' => D('Config')->get('alipay_partner'), 'merchant_private_key' => '', 'notify_url' => C('PROTOCOL') . $_SERVER['SERVER_NAME'] . U('Pay/alipay_notify_url'), 'return_url' => C('PROTOCOL') . $_SERVER['SERVER_NAME'] . U('Pay/alipay_notify_url'), 'charset' => 'UTF-8', 'sign_type' => 'RSA2', 'gatewayUrl' => 'https://openapi.alipay.com/gateway.do', 'alipay_public_key' => '', 'pay_log_txt' => __ROOT__ . '/Public/alipay_log_bef74b0ac451f8e6e5301453f947499899.txt', ); $this->payConfig = $payConfig; }
public function getConfig() { return $this->payConfig; }
public function setConfig($data) { $payConfig = $this->payConfig; foreach ($data as $k => $v) { $payConfig[$k] = $v; } $this->payConfig = $payConfig; }
public function pay($data) { Vendor('Alipay.wappay.service.AlipayTradeService'); Vendor('Alipay.wappay.buildermodel.AlipayTradeWapPayContentBuilder'); $config = $this->payConfig; $out_trade_no = $data['out_trade_no']; $subject = $data['subject']; $total_amount = $data['total_amount']; $body = $data['body']; $timeout_express = "1m"; $payRequestBuilder = new \AlipayTradeWapPayContentBuilder(); $payRequestBuilder->setBody($body); $payRequestBuilder->setSubject($subject); $payRequestBuilder->setOutTradeNo($out_trade_no); $payRequestBuilder->setTotalAmount($total_amount); $payRequestBuilder->setTimeExpress($timeout_express); $payResponse = new \AlipayTradeService($config); $result = $payResponse->wapPay($payRequestBuilder, $config['return_url'], $config['notify_url']); }
public function setPaylog($data) { $config = $this->payConfig; $str = serialize($data) . "\r\n==================================================================================\r\n";; import("Org.Util.File"); $file = new \File($config['pay_log_txt']); $realfiel = $file->getRealFile(); file_put_contents($realfiel, $str, FILE_APPEND); } }
|
支付处理控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| <?php namespace Home\Controller;
use Think\Controller; use Common\Controller\HomeController;
class PayController extends HomeController {
public function alipay($order_number) { $order = D('Order')->where(array('order_number' => $order_number))->find(); $orderDetail = D('OrderDetail')->where(array('oid' => $order['id']))->field('pid')->select(); $firstProduct = D('Products')->where(array('id' => $orderDetail[0]['pid']))->field('title')->find(); $payData = array( 'out_trade_no' => $order['order_number'], 'subject' => D('Config')->get('alipay_subject') . $order_number, 'total_amount' => 0.01, 'body' => $firstProduct['title'] . '等', ); D('Alipay', 'Service')->pay($payData); }
public function alipay_notify_url() { D('Alipay', 'Service')->setPaylog($logArr);
Vendor('Alipay.wappay.service.AlipayTradeService'); $payConfig = D('Alipay', 'Service')->getConfig(); $arr = $_POST; $logArr = $arr; $alipaySevice = new \AlipayTradeService($payConfig); $alipaySevice->writeLog(var_export($_POST, true)); $result = $alipaySevice->check($arr); if ($result) { $out_trade_no = $_POST['out_trade_no']; $trade_no = $_POST['trade_no']; $trade_status = $_POST['trade_status']; if ($_POST['trade_status'] == 'TRADE_FINISHED') { } else if ($_POST['trade_status'] == 'TRADE_SUCCESS') { $re = D('Order')->setPay($out_trade_no, 1); } echo "success"; $logArr['result'] = "success"; } else { echo "fail"; $logArr['result'] = "fail"; } }
public function resultinfo() { $ModelObj = D('Order'); $order_number = I('out_trade_no'); $order = $ModelObj->where(array('order_number' => $order_number))->find(); $this->order = $order; $this->display('pay_ok'); } }
|