如何在yii2中使用paypal扩展.我正在使用此链接
https://github.com/marciocamello/yii2-paypal.
我安装了扩展名,并在配置文件中添加了代码.
我安装了扩展名,并在配置文件中添加了代码.
但是没有更多的信息下一步做什么.所以请帮我
谢谢
没有必要使用扩展名.您可以简单地安装
paypal/rest-api-sdk-php软件包并执行以下步骤.
1.创建一个组件将PayPal粘贴到Yii2
在@app目录中创建一个组件文件夹.如果您使用的是基本模板,则与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调用
到07.01在Yii2,
打开要处理付款的控制器操作,并包括以下内容
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文档.
一旦你得到连接,你应该能够执行其他任务.