2013-08-29 18 views
3

omnipay沒有完整的文檔!我正在嘗試在授權後進行捕獲,但我似乎無法做到。如何在授權後進行捕獲?以及如何退款? in omnipay

<?php 

if (!defined('BASEPATH')) 
    exit('No direct script access allowed'); 

use Omnipay\Common\GatewayFactory; 

class Welcome extends CI_Controller { 

    function __construct() { 
     parent::__construct(); 
     $this->load->helper('url'); 
    }  
    public function authorize() { 

     $gateway = GatewayFactory::create('PayPal_Express'); 
     $gateway->setUsername('***'); 
     $gateway->setPassword('***'); 
     $gateway->setSignature('***'); 
     $gateway->setTestMode(true); 

     $response = $gateway->authorize(
         array(
          'cancelUrl' => base_url('welcome/authorize_return'), 
          'returnUrl' => base_url('welcome/authorize_return'), 
          'amount' => '1.99', 
          'currency' => 'USD' 
         ) 
       )->send(); 

     $response->redirect(); 
    } 

    public function authorize_return() { 
     $gateway = GatewayFactory::create('PayPal_Express'); 
     $gateway->setUsername('***'); 
     $gateway->setPassword('***'); 
     $gateway->setSignature('***'); 
     $gateway->setTestMode(true); 

     $response = $gateway->completeAuthorize(
         array(
          'cancelUrl' => base_url('welcome/authorize_return'), 
          'returnUrl' => base_url('welcome/authorize_return'), 
          'amount' => '1.99', 
          'currency' => 'USD' 
         ) 
       )->send(); 

     echo $responsemsg = $response->getMessage(); 

     $data = $response->getData(); 
     $ref = $response->getTransactionReference(); 
     $response2 = $gateway->capture($data)->send(); 
     print_r($response2); 
    }  
} 

我需要從「待定」到「已完成」更改狀態(例如:之後我船的產品。)

enter image description here

還我該怎麼能做退款,當?如果交易狀態完成,我可以退款嗎?或僅限於特定狀態,它們是什麼?

我得到「你不能退還這筆交易類型」時,「付款狀態」是「待定」,當它爲「已完成」:

function __construct() { 
     parent::__construct(); 
     $this->load->helper('url'); 

     $this->gateway = GatewayFactory::create('PayPal_Express'); 
     $this->gateway->setUsername('***'); 
     $this->gateway->setPassword('***'); 
     $this->gateway->setSignature('***'); 
     $this->gateway->setTestMode(true); 
    } 
public function refund($transactionReference, $amount) { 

      $ref = $transactionReference; 

      $data = array(
       'transactionReference' => $ref, 
       'amount' => $amount, 
      ); 
      $response = $this->gateway->refund($data)->send(); 
      if ($response->isSuccessful()) { 
       // success 
       return 'done'; 
      } else { 
       return $response->getMessage(); 
      } 
     } 
+0

「捕獲」,你的意思是在數據庫中存儲詳細信息?你對這段代碼有什麼特別的問題? – halfer

+0

@halfer我需要完成付款 – Waqleh

回答

8

如果你想立即獲取支付,請在您的初始請求中購買purchase()completePurchase()而不是authorize()completeAuthorize()(購買進行組合授權和拍攝)。

如果您想在稍後捕獲付款(例如發貨時),則需要執行以下操作。

// after initial completeAuthorize() 
// store $ref in your database with the payment 
$ref = $response->getTransactionReference(); 

// then later, when you want to capture it 
$data = array(
    'transactionReference' => $ref, 
    'amount' => '10.00', // pass original amount, or can be less 
); 
$response = $gateway->capture($data)->send(); 
if ($response->isSuccessful()) { 
    // success 
} else { 
    // error, maybe you took too long to capture the transaction 
    echo $response->getMessage(); 
} 
+0

好的,謝謝你,我確實想要捕獲(發貨後),但我也想知道如何做退款我會在你的答案中使用asme $ data數組? – Waqleh

+0

你可以看看我在這個問題上的更新。 – Waqleh

+3

是的,退款您使用相同的數組,除非您不需要傳遞金額,只需交易參考。您只能在獲得交易後才能退款。確保你傳遞從capture()調用獲得的新的transactionReference,而不是來自初始authorize()的transactionReference。 –