drechsler-development/wordpress-paypal-client

WordPress 的简单 PayPal 客户端

v1.7 2024-05-22 14:08 UTC

This package is auto-updated.

Last update: 2024-09-22 14:45:50 UTC


README

此 PayPal 客户端可用于将 PayPal 集成到您的 WordPress 网站中。当然,它也可以用于其他目的,但它最初是开发用于 WordPress 网站的。它只是简化了官方 PayPal 包中已提供的集成方式。

依赖项

此软件包需要以下其他软件包

  • drechsler-development/class-library
  • paypal/paypal-checkout-sdk

使用方法

可以使用 composer 如下简单使用

composer require drechsler-development/wordpress-paypal-client

或者如果您更喜欢 composer.json 文件,请将项目名称添加到 require 段落中,如下所示

{  
    "require": {  
		"drechsler-development/wordpress-paypal-client": "v1.*"
	}
}

创建订单

假设您已经在脚本中某处捕获了所有必要的变量(以及适当的值),您只需将其传递给适当的方法即可。必需的变量包括

    /*your payPal Client ID you will get from your PayPal developer console at https://developer.paypal.com/dashboard/ */
    $payPalClientId = ''; //put in your PayPal Client ID 
    
    /*your PayPal Client Secret you will get from your PayPal developer console at https://developer.paypal.com/dashboard/ */
    $payPalClientSecret = ''; //put in your PayPal Client Secret
    
    //boolean value to define if you want to use the PayPal Sandox for testing (true) or PayPal Production (false)
    $payPalSandbox = true; // or false for production
    
    /* optional a reference ID that will be used for a reference for example in your ERP- or shop system */ 
    $referenceId = 'ABC123';
    /* Contact Details like */
    $firstName = 'John';
    $lastName = 'Doe';
    $email = 'john.doe@drechsler-development.de';
    $tel = '1234567890';
    $postCode = '12345';
    $city = 'Musterstadt';
    /* if you will pass street and number, address will become not necassary, as the Contact class method behind will concatenate the street and number with a space and will set that as the address. As well as you will pass rthe address it will be set as street and number, as long it contains a space as a divider between street and number */
    $street = 'Musterstraße';
    $number = '123';
    $address = 'Musterstraße 123'; //optional, if street and number are set, this will be ignored
    /* Items */
    $myItems[]; 
    /* each item should have */
    $item['name'] = 'Item Name'
    $item['quantity'] = 2;
    $item['netAmount'] = 100.00; //This is the net amount of the item (aka 'unit amount')
    $item['taxPercent'] = 19;
    $item['description'] = 'My beautiful item'; /* optional */

在此示例中,我使用文件 ajax.php 并通过 JavaScript/jQuery 或您所偏好的其他方式通过 POST 请求从另一个脚本中调用它。

<?php

use DD\Mailer\Mailer;
use DD\PayPal\Header;  
use DD\PayPal\Line;  
use DD\PayPal\Process;  
use DD\PayPal\Contact;

require_once __DIR__ . '/../vendor/autoload.php'; //or whereever your autoload.php is located

//Initialisze the response array and set the error to an empty string, to check in the calling script if all went well or if we have errors
$responseArray['error'] = '';

//Create PayPal object
$PayPal = new Process($payPalClientId, $payPalClientSecret, $payPalSandbox);  

//Create PayPal Header object
$OrderHeader = new Header();  
$OrderHeader->referenceId = $salesHeaderId;  
$OrderHeader->currencyCode = 'EUR';  

//Create Buyer (Contact) object
$Contact = new PayPalContact();
$Contact->setFirstName ($firstName);  
$Contact->setLastName ($lastName);  
$Contact->setEmail ($email);  
$Contact->setMobile ($tel);  
$Contact->setPostCode ($postCode);  
$Contact->setCity ($city);  
$Contact->setStreet ($street);  
$Contact->setNumber ($number);  
//$Contact->setAddress ($address ?? ''); //not necassary if street and number are set
 
//Declare an empty array to add (push) Line objects to it
$items = [];  
foreach ($myItems as $item) {
    $ItemLine = new Line();  
    $ItemLine->name = $item['name'];  
    $ItemLine->quantity = $item['quantity'];
    $ItemLine->unitPrice = $item['netAmount'] ?? 0;  
    $ItemLine->taxPercent = $item['taxPercent'] ?? 0;  
    $ItemLine->description = $item['description'] ?? '';  
    //Add created Item to the items array
    $items[] = $ItemLine;  
}
$successPage = '/success', 
$cancelPage = ''; //If empty it will take the original HTTP_HOST from the global $_SERVER array 

try {
    //Process (Post) a PayPal Order to PayPal API
    $PayPal->CreateOrder ($OrderHeader, $Contact, $items, $successPage, $cancelPage ?? '');  
  
    //if all went well we will get back a confirmation url
    // we need to use it to redirect the user to that page in the success method of our promise or ajax call
    // So that the user can log in into PayPal and finish the payment process  
    $responseArray['confirmationUrl'] = $PayPal->confirmationUrl ?? '';  
    $responseArray['debug'] = $PayPal->debug; //the $PayPal->debug array is only available in SandBox Box mode

}catch(Exception $e){
    
    //if something went wrong, The class will throw an error, we can catch and process in our way we want
    $responseArray['error'] = $e->getMessage();
    
}
    
//Return the response array to the calling script
echo json_encode($responseArray);
?>