- 前言
paypal是一种国际支付,并且是一个免费的产品,用户支付并不需要扣除用户消费的手续费,只在商家端扣除的,是一个不错的国际支付
2.下载
直接到github下载php-sdk包,我下载完直接在extend中使用
使用
<?php
/*** Created by PhpStorm.* User: faker1* Date: 2018/5/19* Time: 下午4:34*/
namespace app\api\controller;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
//use PayPal\Api\Amount;
//use PayPal\Api\Details;
//use PayPal\Api\Item;
//use PayPal\Api\ItemList;
//use PayPal\Api\Payer;
//use PayPal\Api\Payment;
//use PayPal\Api\RedirectUrls;
//use PayPal\Api\Transaction;
//use Psr\Log\AbstractLogger;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Api\ShippingAddress;class Paypal
{public function pay_goods(){require "../extend/PayPal-PHP/autoload.php"; //载入sdk的自动加载文件$clientId = 'AQQ-TNT3ISxFoGSB0-E7nETfMCqt8I9jpoDvuDKQv0b33n9Ir6IJ4l3gJm3pkT8G7YcyRzPBS3EaG0eg';$clientSecret = 'EM5woX_Ic5GD7kdFynlMoboQtQcFNqCNqDU1ESz27H9qveZQ02x1ozFwxhnu8mGEL1ivhFAvuVzCbqvg';$apiContext = new ApiContext(new OAuthTokenCredential($clientId,$clientSecret));$apiContext->setConfig(array('mode' => 'sandbox','log.LogEnabled' => true,'log.FileName' => '../PayPal.log','log.LogLevel' => 'DEBUG', // PLEASE USE `INFO` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS'cache.enabled' => true,));$payer = new Payer();$payer->setPaymentMethod("paypal");$item1 = new Item();$item1->setName('test pro 1')->setCurrency('USD')->setQuantity(1)->setSku("testpro1_01")// Similar to `item_number` in Classic API->setPrice(20);$item2 = new Item();$item2->setName('test pro 2')->setCurrency('USD')->setQuantity(5)->setSku("testpro2_01")// Similar to `item_number` in Classic API->setPrice(10);$itemList = new ItemList();$itemList->setItems(array($item1, $item2));$address = new ShippingAddress();$address->setRecipientName('什么名字')->setLine1('什么街什么路什么小区')->setLine2('什么单元什么号')->setCity('城市名')->setState('浙江省')->setPhone('12345678911')->setPostalCode('12345')->setCountryCode('CN');$itemList->setShippingAddress($address);$details = new Details();$details->setShipping(5)->setTax(10)->setSubtotal(70);$amount = new Amount();$amount->setCurrency("USD")->setTotal(85)->setDetails($details);$transaction = new Transaction();$transaction->setAmount($amount)->setItemList($itemList)->setDescription("Payment description")->setInvoiceNumber(uniqid());$baseUrl = 'http://'.$_SERVER["HTTP_HOST"];$redirectUrls = new RedirectUrls();$redirectUrls->setReturnUrl("$baseUrl/api/Paypal/success")->setCancelUrl("$baseUrl/api/Paypal/cancel");$payment = new Payment();$payment->setIntent("sale")->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions(array($transaction));$payment->create($apiContext);$approvalUrl = $payment->getApprovalLink();dump($approvalUrl);}public function success(){echo 'success';}public function cancel(){echo 'cancel';}
}
end