首先在你项目下安装composer引入paypal的sdk
首先在composer.json添加引入paypal
在linux服务器你的项目地址执行指令
指令:composer require “braintree/braintree_php”
<?php
/**
* Created by PhpStorm.
* User: baiwuya
* Date: 2018/6/28
* Time: 下午5:25
* @文档 https://developers.braintreepayments.com/start/overview ##这里是文档
* @
*/
namespace app\api\controller;
class Test {
function token()
{
try {
$gateway = new \Braintree_Gateway([
'environment'=>'sandbox2',
'merchantId'=>'3wgqvfbrdh2ft57234',
'publicKey'=>'n54gjnnx25pq88d23',
'privateKey'=>'f7f9cbd5e4a1252861813a6031e2f732342'
]);
$clientToken = $gateway->clientToken()->generate();
exit($clientToken);
} catch(\Exception $e) {//捕获异常
var_dump($e);
}
}
function test()
{
file_put_contents('wz1.txt',date("Y-m-d H:i:s",time()) );
$gateway = new \Braintree_Gateway([
'environment'=>'sandbox2',
'merchantId'=>'n54gjnnx25pq88d23',
'publicKey'=>'n54gjnnx25pq88d23',
'privateKey'=>'f7f9cbd5e4a1252861813a6031e2f732342'
]);
//
/**
* 只能验证一次 ??? - yes | Cannot use a payment_method_nonce more than once.
* 只有验证成功 . 才算支付成功
*/
$nonceFromTheClient = $_POST["nonce"];
$amount = $_POST["amount"];
$merchantAccountId = $_POST["currency"];
//付款货币
if($merchantAccountId == 'PHP') {
$merchantAccountId = 'php-wahaha';
} elseif($merchantAccountId == 'USD') {
$merchantAccountId = 'baiyaya';
} elseif($merchantAccountId == 'CZK') {
$merchantAccountId = 'czk_zfg';
}
$result = $gateway->transaction()->sale([
'amount' => $amount,
'merchantAccountId' => $merchantAccountId, //客户端 支付货币 , 必须一致
'paymentMethodNonce' => $nonceFromTheClient, //客户端 支付成功 nonce - 非常重要
'options' => [
'submitForSettlement' => True
]
]);
// if (!empty($result['success']) &&$result['success'] == true)
echo json_encode(['code'=>'ok','res'=>$result]);
exit;
$result = $gateway->transaction()->sale([
"amount" => 0.01,
'merchantAccountId' => 'PHP',
"paymentMethodNonce" => $_POST["nonce"], //
// "paymentMethodNonce" => 'EOi-j5LzVxXXe9c6zUf6Dg2v5HzOtmNABrmDz82No_xeqpEZJuQU8tK1XJ7tjUggrh5NE97weU2YBRwK', //支付客户端
"orderId" => $_POST['Mapped to PayPal Invoice Number'], //
"descriptor" => [
"name" => "Descriptor displayed in customer CC statements. 22 char max"
],
"shipping" => [
"firstName" => "Jen",
"lastName" => "Smith",
"company" => "Braintree",
"streetAddress" => "1 E 1st St",
"extendedAddress" => "Suite 403",
"locality" => "Bartlett",
"region" => "IL",
"postalCode" => "60103",
"countryCodeAlpha2" => "US"
],
"options" => [
"paypal" => [
"customField" => $_POST["PayPal custom field"],
"description" => $_POST["Description for PayPal email receipt"]
],
]
]);
if ($result->success) {
echo $result->transaction->id;
exit;
print_r("Success ID: " . $result->transaction->id);
} else {
print_r("Error Message: " . $result->message);
}
// var_dump($gateway);
}
}