首先PayPal官网下载PHP版SDK程序

通过php composer下载sdk:
composer require paypal/rest-api-sdk-php
或github下载:
git clone https://github.com/paypal/PayPal-PHP-SDK.git

PayPal入口程序

bootstrap.php

<?php
require 'autoload.php';
require 'common.php';use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;// Suppress DateTime warnings, if not set already
//date_default_timezone_set(@date_default_timezone_get());
// Adding Error Reporting for understanding errors properly
error_reporting(0);
ini_set('display_errors', '1');
//引入支付配置文件
require ROOT . 'config/api/pay/pay.php';
// Replace these values by entering your own ClientId and Secret by visiting https://developer.paypal.com/developer/applications///配置参数
if ($_REQUEST['uid'] == 664590) {//某个用户测试sandbox//sandbox测试配置$clientId = TEST_PAYPAL_CLIENT_ID;$clientSecret = TEST_PAYPAL_CLIENT_SECRET;$mode = 'sandbox';
}
else {//live正式配置$clientId = PAYPAL_CLIENT_ID;$clientSecret = PAYPAL_CLIENT_SECRET;$mode = 'live';
}$apiContext = getApiContext($clientId, $clientSecret, $mode);
return $apiContext;/*** @param $clientId* @param $clientSecret* @param $mode //模式,sandbox或live* @return ApiContext*/
function getApiContext($clientId, $clientSecret, $mode) {$apiContext = new ApiContext(new OAuthTokenCredential($clientId,$clientSecret));$apiContext->setConfig(array('mode' => $mode,'cache.enabled' => true,));return $apiContext;
}

pay.php

<?php// # Create Billing Agreement with PayPal as Payment Source
//
// This sample code demonstrate how you can create a billing agreement, as documented here at:
// https://developer.paypal.com/webapps/developer/docs/api/#create-an-agreement
// API used: /v1/payments/billing-agreements// Retrieving the Plan from the Create Update Sample. This would be used to
// define Plan information to create an agreement. Make sure the plan you are using is in active state.
/** @var Plan $createdPlan */
require 'bootstrap.php';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;$ordernum = $_REQUEST['ordernum'] ?? '';//自定义参数
$custom = json_encode(['infoid' => $_REQUEST['infoid'],'ordernum' => $ordernum,'paidtime' => $_SERVER['REQUEST_TIME'],'returnUrl' => $retUrl,
]);$amount = new Amount();
$amount->setCurrency("CAD")->setTotal($_REQUEST['payprice']);$transaction = new Transaction();
$transaction->setAmount($amount)->setDescription($_REQUEST['desc'])->setCustom($custom)->setInvoiceNumber(uniqid());
$baseUrl = getBaseUrl();$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($baseUrl . '/ExecutePayment.php?success=true&paytype=paypal&ordernum=' . $ordernum . '&infoid=' . $_REQUEST['infoid'])->setCancelUrl($baseUrl . '/ExecutePayment.php?success=false&paytype=paypal&ordernum=' . $ordernum . '&infoid=' . $_REQUEST['infoid']);$payer = new Payer();
$payer->setPaymentMethod("paypal");$payment = new Payment();
$payment->setIntent("sale")->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions(array($transaction));
$request = clone $payment;
try {$payment->create($apiContext);
} catch (Exception $ex) {ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);exit(1);
}
$approvalUrl = $payment->getApprovalLink();
header('Location:' . $approvalUrl);

支付回调程序

ExecutePayment.php

<?php
// #Execute Agreement
// This is the second part of CreateAgreement Sample.
// Use this call to execute an agreement after the buyer approves itrequire 'bootstrap.php';// ## Approval Status
use PayPal\Api\ExecutePayment;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;// echo '<script>var FROM = "' . ($_GET['os'] ? 'app' : '') . '; var OS = "' . $_GET['os'] . '";</script>';
// Determine if the user accepted or denied the request
$infoid = (int)($_GET['infoid'] ?? 0);
$paymentId = Mstr()->encode($_GET['paymentId'] ?? '');
$ordernum = Mstr()->encode($_GET['ordernum'] ?? '');
if (isset($_GET['success']) && $_GET['success'] == 'true') {try {$payment = Payment::get($paymentId, $apiContext);$execution = new PaymentExecution();$execution->setPayerId(Mstr()->encode($_GET['PayerID'] ?? ''));$result = $payment->execute($execution, $apiContext); //这里如果支付失败,或异常则会抛出异常throw Exception,下面语句不会执行//记录支付成功状态程序echo "<script>goto('" . $returnUrl . "', 1, 1);</script>";} catch (Exception $ex) {echo "<script>goto('" . $returnUrl . "', 1, 1);</script>";}
}

webhook 签名验证和确认订单支付成功,修改订单状态

webhooks.php

<?php
//webhook 确认订单支付成功,修改订单状态$headers = getallheaders();
$headers = array_change_key_case($headers, CASE_UPPER);
$body = file_get_contents('php://input');
$returnData = json_decode($body, true);// 创建了webhook在PayPal开发者后台查找webhookId
$webhookId = '5BM77961TW281551X';
// 签名字符串
$signData = $headers['PAYPAL-TRANSMISSION-ID'] . '|' . $headers['PAYPAL-TRANSMISSION-TIME'] . '|' . $webhookId . '|' . crc32($body);// 加载证书并提取公钥
$pubKey = openssl_pkey_get_public(file_get_contents($headers['PAYPAL-CERT-URL']));
$key = openssl_pkey_get_details($pubKey)['key'];$signature = base64_decode($headers['PAYPAL-TRANSMISSION-SIG']);
//根据提供的签名验证数据
$verifyResult = openssl_verify($signData,$signature,$key,'sha256WithRSAEncryption'
);//签名验证成功
if ($verifyResult == 1) {//订单支付完成if ($returnData['event_type'] == 'PAYMENT.SALE.COMPLETED') {$ordernum = $returnData['resource']['invoice_number'];//由于webhook不能file_put_contents写入文件,将调试日志记录在数据库M('c')->insert('test_paypal', ['content' => json_encode($returnData),'log' => 'sigData:' . $signData . ' ++++ verifyResult:' . $verifyResult . ' ++++ key:' . $key . ' ++++ orderNo:' . $ordernum . ' ++++ headers:' . json_encode($headers),]);//以下是支付成功修改订单状态程序}else {return 'Not Completed';}
}
else {return 'sign error';//签名验证失败 或请求错误
}
return 'success';

退款

function returnMoney()
{try {$txn_id = "xxxxxxx";  //异步加调中拿到的id$amt = new Amount();$amt->setCurrency('USD')->setTotal('99');  // 退款的费用$refund = new Refund();$refund->setAmount($amt);$sale = new Sale();$sale->setId($txn_id);$refundedSale = $sale->refund($refund, $this->PayPal);} catch (\Exception $e) {// PayPal无效退款return json_decode(json_encode(['message' => $e->getMessage(), 'code' => $e->getCode(), 'state' => $e->getMessage()]));  // to object}// 退款完成return $refundedSale;
}

PayPal sdk v1版本php开发支付过程相关推荐

  1. Android PayPal支付的接入和SDK支付过程解析

    Android PayPal支付的接入和SDK支付过程解析 根据产品需求,要接入PayPal支付,在研究了几天支付后,总结下内容给新手使用,本篇文章如果与官方文档有差异的话,一切以官方为准.转载请注明 ...

  2. Java接入PayPal教程,Spring boot Demo源码,企业账号注册,支付,回调,退款全流程,最新PayPal SDK

    一.环境准备 1. 注册 2. 创建沙箱测试账号 3. Client ID及Secret 二.核心代码 1. 官方github sdk 2. pom引用 3. 代码讲解 4. 支付代码demo流程 5 ...

  3. VS2022打开项目之后提示:找不到 .NETFramework,Version=v4.0 的引用程序集。要解决此问题,请为此框架版本安装开发人员工具包(SDK/目标包)或者重新定向应用程序。

    问题: 使用 vs2022打开带有.net4.0的项目会报错 严重性 代码 说明 项目 文件 行 列 禁止显示状态 错误 MSB3644 找不到 .NETFramework,Version=v4.0 ...

  4. java支付接口开发原理_java对接微信支付接口开发的过程是什么?

    java对接微信支付接口开发的过程是什么?以下是小编整理的java对接微信支付接口实现的方法和过程,有需要的朋友可以学习以下的java对接微信支付接口内容. java对接微信支付接口开发的过程是什么? ...

  5. PHP版本对接支付宝支付接口 电脑网站 详细介绍 沙箱环境 超简单易懂

    PHP版本对接支付宝支付接口 其实理顺了思路后,按照我接下来的步骤来,真的超级简单啊,为啥有那么多的朋友们折腾了那么久呢,嘿嘿,接下来跟我走吧~ 我是昨天晚上洗完澡之后,大概八九点,才开始正式开工尝试 ...

  6. 开发者社区SDK对应版本更迭信息

    开发者社区SDK对应版本更迭信息 2021年1月12日 OpenNI SDK V2.3.0.66版本更新如下: 1.安卓上支持SELinux权限,提供NIViewer的安卓源码示例: 2.修改了部分模 ...

  7. Kinect SDK v1.7 新特性、交互框架与新概念

    Kinect SDK v1.7 新特性.交互框架与新概念 zouxy09@qq.com http://blog.csdn.net/zouxy09 2013年3月18日,微软Kinect for Win ...

  8. 智表ZCELL产品V1.4.0开发API接口文档 与 产品功能清单

    为了方便大家使用ZCELL,应网友要求,整理编写了相关文档,现与产品一起同步发布,供大家下载使用,使用过程中如有疑问,请与我QQ联系. 智表(ZCELL)V1.4.0版本  功能清单文档下载地址: 功 ...

  9. android 微信支付过程,android 微信 支付 接入流程总结

    [TOC] 客户端微信支付 登录微信官方开放平台微信,注册账号,在微信开放平台申请应用,输入已上线app的正式的应用包名和签名,微信提供了一个签名工具,一般使用它提供的签名工具生成签名,然后填写到对应 ...

最新文章

  1. 【 MATLAB 】通过案例学会编写一个 matlab 函数(小猫掉进山洞问题)
  2. 硬件——STM32,GPIO篇
  3. DFTug - Getting Started(上篇)
  4. python 垃圾回收哪时候执行_Python 中的垃圾回收机制是如何工作的?
  5. 浪潮业务稳定连续性获Global Data唯一“Leader”评级 蝉联数据中心整体能力“Very Strong”评级
  6. php防止跨域提交,PHP防止跨域提交表单的简单示例
  7. java 不生成文件下载_java 下载文件时怎么避免在服务器上生成文件
  8. sdut 1500 Message Flood(Trie树)
  9. python 做网站用服务器_Python实现简单的Web服务器 Part2—支持动态网站
  10. Java中集合list的add方法添加的不是值COPY而是地址
  11. init与clinit 与 类的初始化顺序
  12. sql server系统表详细说明(2)
  13. 注意点在XMind 6中的使用注意事项
  14. windows server 2012 安装 VC2015 安装失败0x80240017解决方法
  15. 百度贴吧诱导用户操作CPA项目
  16. 粒子滤波 particle filter—从贝叶斯滤波到粒子滤波—Part-V(粒子滤波 PF)
  17. 以下内容仅对你可见个性签名_微信个性签名以下内容仅对你可见模板
  18. 基于android2.3.5系统:源码下载及android体系架构
  19. 大数据基础之常用Linux命令
  20. Note For Linux By Jes(19)-Linux 备份策略

热门文章

  1. 产品经理学大数据——什么是大数据(2)
  2. 【小白必胜-服务器】全网最简单Linux建站环境新手安装步骤
  3. 南信大python期末试卷_南信大 软件工程期末试卷
  4. 车辆配送路径选择问题分析
  5. 从0到100:网课老师如何自媒平台卖课?
  6. 微信公众号H5跳转小程序
  7. 什么是CTG线路?和CN2线路有什么区别差异?
  8. 研究生、博士生全程只靠自己能否发一篇 SCI?
  9. creo扫描选择多条链作为轨迹_Proe/Creo如何使用可变截面扫描创建曲面之垂直于投影...
  10. selenium模块太强大了,网易云音乐都可下载