本文爲CI集成支付寶即時到帳支付接口php
一、下載支付寶官方demo ;即時到帳交易接口(create_direct_pay_by_user)(DEMO下載) 原文地址:https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1瀏覽器
二、登陸支付寶https://www.alipay.com/ 下載CA證書。將證書放在下載的demo文件夾的根目錄下。
二、將下載的create_direct_pay_by_user-PHP-UTF-8文件夾改成AliPay;併到到CI框架libraries/ 目錄下。安全
三、在libraries/ 目錄下新建CI_Alipay.php 文件,內容以下:服務器
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
-
- require_once APPPATH.'libraries/AliPay/lib/alipay_submit.class.php';
-
- class CI_Alipay extends AlipaySubmit {
-
- public function __construct(){
-
- parent::__construct();
-
- }
- }
四、在CI框架application/config/目錄下 新建alipay_config.php 內容以下:app
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
-
-
- $config['partner'] = '';
-
- $config['seller_id'] = $config['partner'];
-
- $config['key'] = '';
-
- $config['notify_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/order/alipayNotifyUrl';
-
- $config['return_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/order/alipayReturnUrl';
-
- $config['sign_type'] = strtoupper('MD5');
-
- $config['input_charset']= strtolower('utf-8');
-
- $config['cacert'] = APPPATH.'libraries/AliPay/cacert.pem';
-
- $config['transport'] = 'http';
-
- $config['payment_type'] = "1";
-
- $config['service'] = "create_direct_pay_by_user";
-
-
-
-
- $config['anti_phishing_key'] = "";
-
- $config['exter_invoke_ip'] = "";
-
五、在項目控制器controllers/ 目錄下新建order.php 內容以下:框架
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
-
-
- class Order extends MY_Controller {
-
- public function __construct(){
- $this->isNeedLogin = TRUE;
- parent::__construct();
- }
-
-
- public function aliPay($id){
-
- $this->load->config('alipay_config',TRUE);
-
-
-
- $this->load->library('CI_Alipay',$this->config->item('alipay_config'));
- $parameter = array(
- 'service' => $this->config->item('service','alipay_config'),
- 'partner' => $this->config->item('partner','alipay_config'),
- 'payment_type' => $this->config->item('payment_type','alipay_config'),
- 'notify_url' => $this->config->item('notify_url','alipay_config'),
- 'return_url' => $this->config->item('return_url','alipay_config'),
- 'seller_id' => $this->config->item('seller_id','alipay_config'),
- 'out_trade_no' => '訂單編號',
- 'subject' => '填寫訂單商品',
- 'total_fee' => '訂單總額',
- 'body' => '商品描述',
- 'show_url' => '',
- 'anti_phishing_key' => '',
- 'exter_invoke_ip' => '',
- '_input_charset' => $this->config->item('input_charset','alipay_config')
- );
-
- $body = $this->ci_alipay->buildRequestForm($parameter,"get","確認");
-
- echo $body;
- }
-
- }
六、以上步驟完成,下面支付就交給支付寶了。能夠選擇直接掃二維碼支付,能夠登陸支付寶帳號後操做curl
七、支付完成後,支付寶會經過異步通知地址(即alipay_config.php文件中的$config['notify_url']地址),隨後支付寶經過同步通知地址(即alipay_config.php文件中的$config['return_url']地址)。異步
7-一、那麼通常狀況咱們會在異步通知內接收到支付成功信息後處理業務邏輯。注意:在異步通知內收到通知後,必定要返回success告知支付寶我已經收到你的通知,讓它閉嘴。不然它會在25小時之內完成8次通知(通知的間隔頻率通常是:4m,10m,10m,1h,2h,6h,15h);函數
7-二、經過同步通知下收到成功信息後處理一些簡單驗證後跳轉或提示成功信息。工具
八、異步通知 內容以下:我也寫在了order控制器內了。看你心情哈
- public function alipayNotifyUrl(){
-
- require_once(APPPATH."/libraries/AliPay/lib/alipay_notify.class.php");
-
- $this->load->config('alipay_config');
- $alconfig['partner']=$this->config->item('partner');
- $alconfig['seller_id']=$this->config->item('seller_id');
- $alconfig['key']=$this->config->item('key');
- $alconfig['notify_url']=$this->config->item('notify_url');
- $alconfig['return_url']=$this->config->item('return_url');
- $alconfig['input_charset']=$this->config->item('input_charset');
- $alconfig['cacert']=$this->config->item('cacert');
- $alconfig['transport']=$this->config->item('transport');
- $alconfig['payment_type']=$this->config->item('payment_type');
- $alconfig['service']=$this->config->item('service');
- $alconfig['sign_type']=$this->config->item('sign_type');
-
- $alipayNotify = new AlipayNotify($alconfig);
- $verify_result = $alipayNotify->verifyNotify();
-
-
-
- if($verify_result) {
-
-
-
- $out_trade_no = $_POST['out_trade_no'];
-
- $trade_no = $_POST['trade_no'];
-
- $trade_status = $_POST['trade_status'];
-
- if($_POST['trade_status'] == 'TRADE_SUCCESS') {
-
-
-
-
-
- }
- echo "success";
-
- } else {
-
-
- echo "fail";
-
-
-
- }
- }
九、同步通知 內容以下:我也寫在了order控制器內了。看你心情哈
- public function alipayReturnUrl(){
-
- require_once(APPPATH."/libraries/AliPay/lib/alipay_notify.class.php");
-
- $this->load->config('alipay_config');
- $alconfig['partner']=$this->config->item('partner');
- $alconfig['seller_id']=$this->config->item('seller_id');
- $alconfig['key']=$this->config->item('key');
- $alconfig['notify_url']=$this->config->item('notify_url');
- $alconfig['return_url']=$this->config->item('return_url');
- $alconfig['input_charset']=$this->config->item('input_charset');
- $alconfig['cacert']=$this->config->item('cacert');
- $alconfig['transport']=$this->config->item('transport');
- $alconfig['payment_type']=$this->config->item('payment_type');
- $alconfig['service']=$this->config->item('service');
- $alconfig['sign_type']=$this->config->item('sign_type');
-
-
- $alipayNotify = new AlipayNotify($alconfig);
- $verify_result = $alipayNotify->verifyReturn();
- if($verify_result) {
-
-
-
- $out_trade_no = $_GET['out_trade_no'];
-
- $trade_no = $_GET['trade_no'];
-
- $trade_status = $_GET['trade_status'];
-
- if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
-
-
-
-
-
-
- }else {
- echo "trade_status=".$_GET['trade_status'];
- }
-
- } else {
-
-
- echo "驗證失敗";
- }
- }
十、即時到帳有密退款接口也都差很少,我這裏就很少說了。想必作到這裏你也玩轉支付寶支付了吧。是否是so easy~
轉:http://blog.csdn.net/lyne_007/article/details/51752068
支付寶網頁即時到帳功能,可以讓用戶在線向開發者的支付寶帳號支付資金,交易資金即時到帳,幫助開發者快速回籠資金。
交易步驟少,簡單、方便。
交易資金三個月內可退款,提高買家購物保障。
本產品涵蓋如下接口功能: