yii2-整合PayPal SDK 待测

下载安装SDK包 install the paypal/rest-api-sdk-php package

然后按以下步骤操作

1. Create a component to glue Paypal to Yii2

Create a components folder in your @app directory. If you are using the basic template, this is the same folder as webroot; in the advanced template, this folder is in the app you want to enable payments in.

Create a PHP class file (CashMoney for example) with the following content

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();
  }
}

This is enough to get started. You may choose to add other configurations specific to PayPal later on.

2. Register your glue component with Yii2

In your app/config/main.php (or app/config/main-local.php), include the following to register the CashMoney component.

'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
    
        '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',
// You may choose to include other configuration options from PayPal // as they have specified in the documentation ], ...]

Now we have our payment component registered as CashMoney, we can access it using Yii::$app->cm. Cool, huh?

3. Make API calls

To Make Your First API call in Yii2,

Open the controller action in which you want to handle payments, and include the following

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

class PaymentsController { // or whatever yours is called
  ...
  public 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;
    }
    ...
  }

  ...

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值