1.教程https://developers.braintreepayments.com/start/tutorial 2.braintreegateway与paypal账户关联群
3.https://sandbox.braintreegateway.com/merchants/g7hzdpsf3s75nbwv/home查看账户商家id,公钥,专用密钥。和消费的金额
4.代码
现在composer执行指令,前提composer.json有这个文件 “braintree/braintree_php”: “^3.34”,在项目地址那个路径执行这个指令: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;
use think\Db;
use think\Request;
class Test extends Common{
function token()
{
try {
$gateway = new \Braintree_Gateway([
'environment'=>'sandbox',
'merchantId'=>'g7hzdpsf3s75nbwv',
'publicKey'=>'jk2h7ch2mwmg67xk',
'privateKey'=>'08a8bfa1be9ac626c1eed9cd0593c54d'
]);
$clientToken = $gateway->clientToken()->generate();
return success($clientToken);
// exit($clientToken);
} catch(\Exception $e) {//捕获异常
var_dump($e);
}
}
function pay_pal()
{
$gateway = new \Braintree_Gateway([
'environment'=>'sandbox',
'merchantId'=>'g7hzdpsf3s75nbwv',
'publicKey'=>'jk2h7ch2mwmg67xk',
'privateKey'=>'08a8bfa1be9ac626c1eed9cd0593c54d'
]);
/**
* 只能验证一次 ??? - yes | Cannot use a payment_method_nonce more than once.
* 只有验证成功 . 才算支付成功
*/
$nonceFromTheClient = $_POST["nonce"];
$amount = $_POST["amount"];
// $merchantAccountId = $_POST["currency"];
$merchantAccountId = 'USD';
$order_no = $_POST['order_no'];
$type = $_POST['type'];
file_put_contents('1.txt',$nonceFromTheClient);
//付款货币
if($merchantAccountId == 'PHP') {
$merchantAccountId = 'php-test';
} elseif($merchantAccountId == 'USD') {
$merchantAccountId = 'zfg';
} elseif($merchantAccountId == 'CZK') {
$merchantAccountId = 'czk_zfg';
}
$result = $gateway->transaction()->sale([
'amount' => $amount,
'merchantAccountId' => $merchantAccountId, //客户端 支付货币 , 必须一致
'paymentMethodNonce' => $nonceFromTheClient, //客户端 支付成功 nonce - 非常重要
'options' => [
'submitForSettlement' => True
]
]);
file_put_contents('faker1.txt',json_encode($result));
$res = json_encode($result);
// file_put_contents('234.txt',$res);
// echo json_encode(['code'=>'ok','res'=>$result]);
// return success('支付成功');
// exit;
$re = $this->object_array(json_decode($res));
if($re['success']){
if($type=='1'){
file_put_contents('6.txt','sdf');
$order = Db::name('order')->where(['order_no' => $order_no])->find();
if ($order && $order['order_state'] == 'wait_pay') {
$data['order_state'] = 'wait_send';
$data['uptime'] = date("Y-m-d H:i:s", time());
//$data['returns'] = json_encode($result);
$s = Db::name('order')->where(['order_no' => $order_no])->update($data);
if ($s) {
$code['order_state'] = 'wait_send';
$code['update_time'] = date("Y-m-d H:i:s", time());
$code['pay_time'] = date("Y-m-d H:i:s", time());
$code['ping_no'] = ''; //ping++订单号
$code['pay_no'] = '';
$code['pay_way'] = 'paypal';
//$code['amount'] = $result['data']['object']['amount'] / 100;
$code['pay_charge'] = '';
$result = Db::name('order_merchants')->where(['order_id' => $order['order_id']])->update($code);
$member = Db::name('member')->where(['member_id' => $order['member_id']])->find();
$order_goods = Db::name('order_goods')->where(['order_id' => $order['order_id']])->select();
foreach ($order_goods as $v) {
$goods = Db::name('goods')->where(['goods_id' => $v['goods_id']])->find();
if ($goods) {
$goodsInfo['total_sales'] = $goods['total_sales'] + $v['goods_num'];
$goodsInfo['month_sales'] = $goods['month_sales'] + $v['goods_num'];
$goodsInfo['day_sales'] = $goods['day_sales'] + $v['goods_num'];
if ($goods['goods_stock'] > $v['goods_num']) {
$goodsInfo['goods_stock'] = $goods['goods_stock'] - $v['goods_num'];
Db::name('goods')->where(['goods_id' => $v['goods_id']])->update($goodsInfo);
if ($v['specification_id']) {
$specification = Db::name('goods_relation_specification')->where(['specification_id' => $v['specification_id']])->find();
if ($specification) {
if ($specification['specification_stock'] > $v['goods_num']) {
$goodsSpecification['specification_stock'] = $specification['specification_stock'] - $v['goods_num'];
} else {
$goodsSpecification['specification_stock'] = '0';
}
$goodsSpecification['specification_sales'] = $specification['specification_sales'] + $v['goods_num'];
}
Db::name('goods_relation_specification')->where(['specification_id' => $v['specification_id']])->update($goodsSpecification);
}
} else {
$goodsInfo['goods_stock'] = '0';
Db::name('goods')->where(['goods_id' => $v['goods_id']])->update($goodsInfo);
}
}
}
//交易记录
$tradeRecord['member_id'] = $member['member_id'];
$tradeRecord['order_no'] = $order_no;
$tradeRecord['type'] = 1;
$tradeRecord['intime'] = date("Y-m-d H:i:s", time());
$tradeRecord['pay_no'] = '';
$tradeRecord['amount'] = $amount;
$tradeRecord['pay_return'] = '';
Db::name('TradeRecord')->insert($tradeRecord);
return success('支付成功');
} else {
error("支付失败");
}
}
}else{
$order = Db::name('order_merchants')->where(['order_no' => $order_no])->find();
if ($order && $order['order_state'] == 'wait_pay') {
$code['order_state'] = 'wait_send';
$code['update_time'] = date("Y-m-d H:i:s", time());
$code['pay_time'] = date("Y-m-d H:i:s", time());
$code['ping_no'] = ''; //ping++订单号
$code['pay_no'] = '';
$code['pay_way'] = 'paypal';
//$code['amount'] = $result['data']['object']['amount'] / 100;
$code['pay_charge'] = '';
$result = Db::name('order_merchants')->where(['order_merchants_id' => $order['order_merchants_id']])->update($code);
$member = Db::name('member')->where(['member_id' => $order['member_id']])->find();
$order_goods = Db::name('order_goods')->where(['order_merchants_id' => $order['order_merchants_id']])->select();
foreach ($order_goods as $v) {
$goods = Db::name('Goods')->where(['goods_id' => $v['goods_id']])->find();
if ($goods) {
$goodsInfo['total_sales'] = $goods['total_sales'] + $v['goods_num'];
$goodsInfo['month_sales'] = $goods['month_sales'] + $v['goods_num'];
$goodsInfo['day_sales'] = $goods['day_sales'] + $v['goods_num'];
if ($goods['goods_stock'] > $v['goods_num']) {
$goodsInfo['goods_stock'] = $goods['goods_stock'] - $v['goods_num'];
Db::name('goods')->where(['goods_id' => $v['goods_id']])->update($goodsInfo);
if ($v['specification_id']) {
$specification = Db::name('goods_relation_specification')->where(['specification_id' => $v['specification_id']])->find();
if ($specification) {
if ($specification['specification_stock'] > $v['goods_num']) {
$goodsSpecification['specification_stock'] = $specification['specification_stock'] - $v['goods_num'];
} else {
$goodsSpecification['specification_stock'] = '0';
}
$goodsSpecification['specification_sales'] = $specification['specification_sales'] + $v['goods_num'];
}
Db::name('goods_relation_specification')->where(['specification_id' => $v['specification_id']])->update($goodsSpecification);
}
} else {
$goodsInfo['goods_stock'] = '0';
Db::name('goods')->where(['goods_id' => $v['goods_id']])->update($goodsInfo);
}
}
}
//交易记录
$tradeRecord['member_id'] = $member['member_id'];
$tradeRecord['order_no'] = $order_no;
$tradeRecord['type'] = 1;
$tradeRecord['intime'] = date("Y-m-d H:i:s", time());
$tradeRecord['pay_no'] = '';
$tradeRecord['amount'] = $amount;
$tradeRecord['pay_return'] = '' ;
Db::name('TradeRecord')->insert($tradeRecord);
return success('支付成功');
}
}
}else{
return error('支付失败');
}
}
/**
*
* 数据转换 stdClass Object转array
* @access public
*/
public function object_array($array) {
if(is_object($array)) {
$array = (array)$array;
} if(is_array($array)) {
foreach($array as $key=>$value) {
$array[$key] =self::object_array($value);
}
}
return $array;
}
}