henshall/php-paypal-wrapper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

henshall/php-paypal-wrapper

Composer 安装命令:

composer require henshall/php-paypal-wrapper

包简介

Simple Paypal wrapper used to make purchases, with lots of examples

README 文档

README

Version: 1.0.0

This is a wrapper for the Paypals PHP Package 'paypal/rest-api-sdk-php'.

Working with Paypal can be an extremely frustrating experience. Their sandbox doesn't always work, their documentation is outdated or redundant, and they are very quick to ban and freeze your account if they don't like any of the transactions (ex. monetary amount too high).

I want to create a wrapper with some basic documentation so that users can accept payments as soon as possible. The following package lets users send a payment to your Paypal account, and allows parameters to be sent along (such as user id's) so that you can easily track who has paid, so that you can upgrade their account, or send a product to them, or whatever else you need to do.

Installation with Composer:

composer require henshall/php-paypal-wrapper

Pre-requisites

  • Obtain Paypal Client ID / Secret at https://developer.paypal.com
  • Place the following config file somewhere into your code, and change the settings to match your application.
return [
    
    /**
    * get from https://developer.paypal.com
    */
    'client_id' => 'PAYPAL_CLIENT_ID',
    /**
    * get from https://developer.paypal.com
    */ 
    'secret' => 'PAYPAL_CLIENT_SECRET', 
    /**
    * after a user authorizes the payment, they will be directed to this url. This is where we will place the logic from step 2 below.
    */
    'return_url' => 'RETURN_URL',
    /**
    * // url the user is sent to if they cancel the transaction. Maybe you can bring the user back to your homepage with a message saying they cancelled the transaction.
    */
    'cancel_url' => 'CANCEL_URL', 
    /**
    * SDK configuration
    */
    'settings' => array(
        /**
        * Available option 'sandbox' or 'live'
        */
        'mode' => "sandbox",  // make sure to change this to live!
        /**
        * Specify the max request time in seconds
        */
        'http.ConnectionTimeOut' => 1000,
        /**
        * Whether want to log to a file
        */
        'log.LogEnabled' => false,
        /**
        * Specify the file that want to write on
        */
        'log.FileName' => '/logs/paypal.log',
        /**
        * Available option 'FINE', 'INFO', 'WARN' or 'ERROR'
        *
        * Logging is most verbose in the 'FINE' level and decreases as you
        * proceed towards ERROR
        */
        'log.LogLevel' => 'FINE',
        
    ),
];

Usage:

This package is 100% on the back-end - so that you can create your own html/css/js front-end however you like, and when a user wants to make a purchase, simply send a post request to the back-end of your application and use this package.

Step 1.

A user click "pay" on your website, and you send a get/post request to the backend.

<a href="/pay">Pay</a>

Step 2.

In the pay.php file, or wherever you redirected them to, you will have the code below. This code will redirect them to paypals website where they will log in, and authorize the payment.

// Create the class
$paypalWrapper = new PaypalWrapper;
// Set the config file as shown above in the prerequisites section
$config = config('paypal_conf');
$validate = $paypalWrapper->validateConfigFile($config);
$paypalWrapper->setConfigFile($config);
// Set a Parameter - Here you can set a user_id, name, or email address to track which user has paid.
$paypalWrapper->setParam("useremail@email.com");
// Set the amount and the currency type (currency examples below)
$paypalWrapper->RedirectToPaypal(100, "USD");
if ($paypalWrapper->error) {
    // Where to put your logic if there is an error. (Save error to DB, or log file, or email to yourself etc.)
    // die($paypalWrapper->error);
}


//NOTE you can also check if any of the individual functions fail by see if they return false. 
// $setConfig = $paypalWrapper->setConfigFile($config);
// if (!$setConfig) {
//     // LOGIC IF FAILED TO SET CONFIG
// }

Step 3.

Once they authorize the payment, they will be redirected back to your website according to the 'redirect_url' variable you set in the config file. They will pass along two parameters: 1) PayerId 2) paymentID - we will need these parameters in order to execute the payment.

// Create the class
$paypalWrapper = new PaypalWrapper;
// Set the config file as shown above in the prerequisites section
$config = config('paypal_conf');
$validate = $paypalWrapper->validateConfigFile($config); 
$paypalWrapper->setConfigFile($config);
// Set the payment
$paypalWrapper->setPayment($data["PayerID"], $data["paymentId"] );
// Execute the payment
$executePayment = $paypalWrapper->executePayment();

if ($paypalWrapper->error) {
    // Where to put your logic if there is an error with the wrapper itself, or its config
}

if ($executePayment == true) {
    // write code for when payment is successful
}

if ($executePayment == false) {
    // write code for when payment is NOT successful (ex. insufficient funds, or blocked for some reason)
}

Currency Types Accepted:

Currency Code
Australian dollar AUD
Brazilian real 2 BRL
Canadian dollar CAD
Chinese Renmenbi 4 CNY
Czech koruna CZK
Danish krone DKK
Euro EUR
Hong Kong dollar HKD
Hungarian forint 1 HUF
Indian rupee 3 INR
Israeli new shekel ILS
Japanese yen 1 JPY
Malaysian ringgit 4 MYR
Mexican peso MXN
New Taiwan dollar 1 TWD
New Zealand dollar NZD
Norwegian krone NOK
Philippine peso PHP
Polish złoty PLN
Pound sterling GBP
Russian ruble RUB
Singapore dollar SGD
Swedish krona SEK
Swiss franc CHF
Thai baht THB
United States dollar USD

henshall/php-paypal-wrapper 适用场景与选型建议

henshall/php-paypal-wrapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 henshall/php-paypal-wrapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 henshall/php-paypal-wrapper 我们能提供哪些服务?
定制开发 / 二次开发

基于 henshall/php-paypal-wrapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2020-09-01