如何使用Yii2的PayPal扩展集成支付网关

14
如何在yii2中使用paypal扩展。我正在使用此链接:
https://github.com/marciocamello/yii2-paypal
我安装了扩展并在配置文件中添加了代码。

但是没有更多的信息告诉我接下来该怎么做。请帮助我。

谢谢


2
也许最好向实际的开发人员询问。您可以在Github上创建问题,以解决文档中的问题。从我所看到的情况来看,文档相当差,也没有其他链接可以提供。另一个选择是通过查看源代码自己找出答案。除了init和demo之外,主类中只有一个方法和配置文件。也许扩展处于开发的初期阶段。 - arogachev
不是答案,只是想知道http://paypal.github.io/PayPal-PHP-SDK/是否有所帮助。我不是YII开发人员,因此不确定它是否需要特别考虑。PayPal-PHP-SDK在composer中可用作`paypal/rest-api-sdk-php`。 - Jay Patel - PayPal
4个回答

18

不需要使用扩展程序。您可以简单地安装paypal/rest-api-sdk-php软件包并执行以下步骤。

1. 创建一个组件将Paypal连接到Yii2

@app目录中创建一个components文件夹。如果您使用的是基本模板,则该文件夹与webroot相同;在高级模板中,此文件夹位于要启用支付的应用程序中。

创建一个名为PHP类文件(例如CashMoney)且具有以下内容:

use yii\base\Component;

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;

class CashMoney extends Component {
  public $client_id;
  public $client_secret;
  private $apiContext; // paypal's API context

  // override Yii's object init()
  function init() { 
    $this->apiContext = new ApiContext(
      new OAuthTokenCredential($this->client_id, $this->client_secret)
    );
  }

  public function getContext() {
    return $this->apiContext;
  }
}

这已经足够开始了。您可以选择稍后添加其他针对PayPal的配置。

2. 在Yii2中注册您的“粘合”组件

在您的app/config/main.php(或app/config/main-local.php)中,包含以下内容以注册CashMoney组件。

'components' => [
  ...
  'cm' => [ // bad abbreviation of "CashMoney"; not sustainable long-term
    'class' => 'app/components/CashMoney', // note: this has to correspond with the newly created folder, else you'd get a ReflectionError

     // Next up, we set the public parameters of the class
    'client_id' => 'YOUR-CLIENT-ID-FROM-PAYPAL',
    'client_secret' => 'YOUR-CLIENT-SECRET-FROM-PAYPAL',
    // You may choose to include other configuration options from PayPal
    // as they have specified in the documentation
  ],
  ...
]

现在我们已经将付款组件注册为CashMoney,我们可以使用Yii::$app->cm进行访问。很酷,是吧?

3. 进行API调用

要在Yii2中进行第一次API调用

打开你想处理付款的控制器操作,并包含以下内容:

use Yii;
...
use PayPal\Api\CreditCard;
use PayPal\Exception\PaypalConnectionException;

class PaymentsController { // or whatever yours is called
  ...
  public function actionMakePayments { // or whatever yours is called
    ...
    $card = new PayPalCreditCard;
    $card->setType('visa')
      ->setNumber('4111111111111111')
      ->setExpireMonth('06')
      ->setExpireYear('2018')
      ->setCvv2('782')
      ->setFirstName('Richie')
      ->setLastName('Richardson');

    try {
      $card->create(Yii::$app->cm->getContext());
      // ...and for debugging purposes
      echo '<pre>';
      var_dump('Success scenario');
      echo $card;
    } catch (PayPalConnectionException) {
      echo '<pre>';
      var_dump('Failure scenario');
      echo $e;
    }
    ...
  }

  ...

}

预期输出的格式与 PayPal 文档类似。

一旦连接成功,您应该能够执行其他任务。


1
"$this->apiContext = new ApiContext( new OAuthTokenCredential($this->client_id, $this->client_secret); );" 应该改为 "$this->apiContext = new ApiContext( new OAuthTokenCredential($this->client_id, $this->client_secret) );"。同时,"public function getContext() { return $this->apiContext(); }" 应该改为 "public function getContext() { return $this->apiContext; }"。请进行更正。由于字符限制,无法编辑(这很糟糕)。 - Gogol
你在哪里初始化CashMoney组件? - user3574492
你所提供的“Making the first call”链接与你添加的代码块没有任何相似之处,你可能需要更新代码,因为没有名为“PayPalCreditCard”的类。 - Muhammad Omer Aslam

4

这是我的解决方案,它可以与yii2高级模板一起使用!

我将marciocamello/yii2-paypal/PayPal.php类从vendor文件夹移动到common/component/PayPal.php。

您需要在frontend\config\main.php的components部分中“包含”common\components\paypal:

 'components' => [
    'paypal'=> [
        'class'        => 'common\components\Paypal',
        'clientId'     => 'your id you can find it over your paypal develpoer account. Ypu ah to create an application',
        'clientSecret' => 'your id you can find it over your paypal develpoer account. Ypu ah to create an application',
        'isProduction' => false,
         // This is config file for the PayPal system
         'config'       => [
             'http.ConnectionTimeOut' => 30,
             'http.Retry'             => 1,
             'mode'                   => \common\components\Paypal::MODE_SANDBOX, 
             'log.LogEnabled'         => YII_DEBUG ? 1 : 0,
             'log.FileName'           => '@runtime/logs/paypal.log',
             'log.LogLevel'           => \common\components\Paypal::LOG_LEVEL_INFO,
        ]
    ],

在PPHttpConfig.php文件的第11行,我已经更改了以下行:
    //CURLOPT_SSLVERSION => 3,
    CURLOPT_SSLVERSION => 'CURL_SSLVERSION_TLSv1',

在PPLoggingManager.php的48-52行,我已经硬编码了日志路径。
if($this->isLoggingEnabled) {
        $this->loggerFile = $_SERVER['DOCUMENT_ROOT'].'/domain/frontend/runtime/logs/paypal.log';//($config['log.FileName']) ? $config['log.FileName'] : ini_get('error_log');
        $loggingLevel = strtoupper($config['log.LogLevel']);
        $this->loggingLevel = (isset($loggingLevel) && defined(__NAMESPACE__."\\PPLoggingLevel::$loggingLevel")) ? constant(__NAMESPACE__."\\PPLoggingLevel::$loggingLevel") : PPLoggingManager::DEFAULT_LOGGING_LEVEL;
    }

在网站控制器中,我调用了组件函数。
      echo '<pre/>';
  print_r(Yii::$app->paypal->payDemo());  
  exit();

我收到了一个成功的响应,其中包括重定向URL、交易ID等信息。

--------------  400 ERROR Debuging ----------

我一直遇到400错误,因为价格格式有问题,我使用number_format函数进行了修复。

我稍微修改了payDemo()函数。

    $amount = 16;    
$formattedAmount = number_format($amount,2);
        $payer = new Payer();
        $payer->setPayment_method("paypal");
        $item1 = new Item();
$item1->setName('Ground Coffee 40 oz')
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setPrice($formattedAmount);
$item2 = new Item();
$item2->setName('Granola bars')
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setPrice($formattedAmount);
$itemList = new ItemList();

$itemList->setItems(array($item1, $item2));
        //  $amountDetails = new Details();
        // $amountDetails->setSubtotal('7');
        // $amountDetails->setTax('0.00');
        // $amountDetails->setShipping('0.00');

         $amount = new Amount();
        $amount->setCurrency('USD');
        $amount->setTotal(number_format((2*$formattedAmount),2));
      //  $amount->setDetails($amountDetails);

        $transaction = new Transaction();
        $transaction->setDescription("creating a payment");
        $transaction->setItemList($itemList);
        $transaction->setAmount($amount);

        //$baseUrl = getBaseUrl();
        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturn_url("https://devtools-paypal.com/guide/pay_paypal/php?success=true");
        $redirectUrls->setCancel_url("https://devtools-paypal.com/guide/pay_paypal/php?cancel=true");

        $payment = new Payment();
        $payment->setIntent("sale");
        $payment->setPayer($payer);
        $payment->setRedirect_urls($redirectUrls);
        $payment->setTransactions(array($transaction));


        return $payment->create($this->_apiContext);

如果您仍然收到400或40x错误,可以在PPHttpConnection.php的107行打印整个PayPal响应与错误消息等内容。
    $ex->setData($result);
        // echo '<pre>';
        // print_r($ex);exit;
        throw $ex;

你好,能否提供您的电子邮件地址?我有一个使用PayPal模块的YII2高级入门项目,并且它运行良好。我可以通过电子邮件将其发送给您。 :) - Nagy Ervin
我刚刚发送了一封电子邮件:)。如果您有任何问题,请随时与我联系。 - Nagy Ervin
请检查您的收件箱。我已经发送了一个链接,您可以通过它下载项目。祝您有愉快的一天。 - Nagy Ervin
感谢nagy的回复,按照您的建议,它起作用了。我的问题是这样的。 1)当我们加入任何项目时,以https://www.elegantthemes.com/join.php为例,有三种类型的订阅。这就像订阅一样。在订阅期结束后,我将自动支付预付款,例如一个月或一年。 这是PayPal的循环付款,我如何在yii2中实现这一点。谢谢 - jatin vaghasiya
不需要将类移出供应商位置。只需从composer包含自动加载程序,并在Yii 2控制器类的顶部使用路径/到/类;以访问库类的逻辑。 - David J Eddy
显示剩余5条评论

1

剧透:最低支持版本:PHP 5.5 - Gogol

1
我在我的网站上使用基于Rest API的Paypal Express Checkout。不需要任何第三方扩展。该解决方案通常适用于任何网站,而不仅限于Yii框架。要了解基本概念,请阅读https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/
以下是我的方法:
  1. https://developer.paypal.com中创建一个REST API APP。创建APP后,您将获得客户端ID和密钥,稍后用于身份验证。

  2. 在您希望显示结帐按钮的网页上,创建一个空div <div id="paypal-button"></div>

  3. 在您的资源中包含https://www.paypalobjects.com/api/checkout.js javascript。

4. 使用以下Javascript代码渲染Paypal按钮。

paypal.Button.render({
    env: 'sandbox', // Specify 'sandbox' for the test environment
    client: {
        sandbox: 'CLIENT-ID of your APP in step1'           
    },
    payment: function (resolve, reject) {

        /* URL which would create payment */
        var CREATE_PAYMENT_URL = '/transactions/create-paypal';

        paypal.request.post(CREATE_PAYMENT_URL)
            .then(function (data) {
                resolve(data.paymentID);
            })
            .catch(function (err) {
                reject(err);
            });
    },

    onAuthorize: function (data, actions) {

        // Execute the payment here, when the buyer authorize and approves the transaction
        var EXECUTE_PAYMENT_URL = '/transactions/execute-paypal';
        paypal.request.post(EXECUTE_PAYMENT_URL,
            {paymentID: data.paymentID, payerID: data.payerID, token: data.paymentToken,serviceId:serviceId})

            .then(function (data) {
                console.log(data);
                if(data.http_code == '200') {
                    /* payment done .. do something here */
                    handleCreateService(url);
                }else {
                    /* something didn't went right with payment */
                }
            })
            .catch(function (err) {
                /* catch any exceptions */
                console.log(err);
            });
    }

}, '#paypal-button');
  1. Now you need to code your create payment and execute payment methods in controller.

    /* Create Paypal function will pass token,
    paymentID back to JS in step 4. */
    
    public function actionCreatePaypal() {        
        $paypal = new Paypal();
        $paypal->getToken();
        $res = $paypal->createPayment();
        echo json_encode($res);
    }
    
    public function actionExecutePaypal() {
        $paymentID = $_POST['paymentID'];
        $payerID =  $_POST['payerID'];
        $access_token = $_POST['token'];
        $paypal = new Paypal(['paymentID'=>$paymentID,'payerID' =>$payerID,'access_token' =>$access_token]);
        $paypal->getToken();
        $res = $paypal->executePayment();
        echo json_encode($res);
    }    
    
  2. Finally create a component to do authentication/ generating token and executing payment.

    class Paypal {  
    
        public $url;
        public $env;
        public $clientId;
        public $clientSecret;
        public $access_token;
        public $paymentID;
        public $payerID;
        public $premiumService;
    
        public function __construct($params=0) {
            $this->access_token = '';
            /* for sandbox url is https://api.sandbox.paypal.com */
            $this->url = \Yii::$app->params['paypal_url'];
            $this->clientId = \Yii::$app->params['paypal_clientId'];
            $this->clientSecret = \Yii::$app->params['paypal_clientSecret'];
    
            if(isset($params['paymentID'])) {
                $this->paymentID = $params['paymentID'];
            }
    
            if(isset($params['payerID'])) {
                $this->payerID = $params['payerID'];
            }
    
            if(isset($params['access_token'])) {
                $this->access_token = $params['access_token'];
            }
    
            /* This is where you describe the product you are selling */    
            $this->premiumService = '{
            "intent":"sale",
            "redirect_urls":{
                 "cancel_url":"https://cancelurl.com",
                 "return_url":"https://returnurl.com"
            },
            "payer":{
                "payment_method":"paypal"
            },
            "transactions":[
            {
                "amount":{
                "currency":"USD",
                "total":"39.00"
                },
                "item_list":{
                    "items": [
                    {
                        "quantity": "1",
                        "name": "Premium Service",
                        "price": "39.00",
                        "currency": "USD",
                        "description": "Purchase allows one time use of this premium service"
                    }]
                },
                "description":"Premium Service"
    
            }]
            }';
    }
    public function getToken() {
        $curlUrl = $this->url."/v1/oauth2/token";
        $curlHeader = array(
            "Content-type" => "application/json",
            "Authorization: Basic ". base64_encode( $this->clientId .":". $this->clientSecret),
        );
        $postData = array(
            "grant_type" => "client_credentials"
        );
    
        $curlPostData = http_build_query($postData);
        $curlResponse = $this->curlCall($curlUrl, $curlHeader, $curlPostData);
        if($curlResponse['http_code'] == 200) {
            $this->access_token = $curlResponse['json']['access_token'];
        }
    }
    
    
    public function createPayment() {
        $curlUrl = $this->url."/v1/payments/payment";
        $curlHeader = array(
            "Content-Type:application/json",
            "Authorization:Bearer  ". $this->access_token,
        );
    
    
        $curlResponse = $this->curlCall($curlUrl, $curlHeader, $this->premiumService);
        $id = null;
        $approval_url = null;
        if($curlResponse['http_code'] == 201) {
            $id = $curlResponse['json']['id'];
            foreach ($curlResponse['json']['links'] as $link) {
                if($link['rel'] == 'approval_url'){
                    $approval_url = $link['href'];
                }
            }
        }
    
        $res = ['paymentID' =>$id,'approval_url'=>$approval_url];
        return $res;
    }
    
    public function executePayment() {
        $curlUrl = $this->url."/v1/payments/payment/".$this->paymentID."/execute";
    
        $curlHeader = array(
            "Content-Type:application/json",
            "Authorization:Bearer ".$this->access_token,
        );
        $postData = array(
            "payer_id" => $this->payerID
        );
    
        $curlPostData = json_encode($postData);
        $curlResponse = $this->curlCall($curlUrl, $curlHeader, $curlPostData);
    
        return $curlResponse;
    }
    
    function curlCall($curlServiceUrl, $curlHeader, $curlPostData) {
        // response container
        $resp = array(
            "http_code" => 0,
            "json"     => ""
        );
    
        //set the cURL parameters
        $ch = curl_init($curlServiceUrl);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
    
        //turning off the server and peer verification(TrustManager Concept).
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    
        curl_setopt($ch, CURLOPT_SSLVERSION , 'CURL_SSLVERSION_TLSv1_2');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        //curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeader);
    
        if(!is_null($curlPostData)) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPostData);
        }
        //getting response from server
        $response = curl_exec($ch);
    
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
        curl_close($ch); // close cURL handler
    
        // some kind of an error happened
        if (empty($response)) {
            return $resp;
        }
    
        $resp["http_code"] = $http_code;
        $resp["json"] = json_decode($response, true);
    
        return $resp;
    
    }
    }
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接