sshgun/payphone-php

一个简单的SDK,用于使用payphone支付

dev-master 2024-06-26 19:18 UTC

This package is auto-updated.

Last update: 2024-09-26 19:59:32 UTC


README

一个简单的SDK,用于使用PHP调用Payphone开发者API。

I'm in no way affiliated with, maintained, authorized, sponsored, or
officially associated with PayPhone, PayPhone.app  or any of its subsidiaries
or affiliates. The official PayPhone website can be found at https://paypohne.app

PayPhone按钮框

PayPhone支付按钮框是由官方PayPhone API渲染的HTML表单。这个库只实现了一个简单的接口,用于从PHP中使用代码。完整的文档可以在这里找到。

使用这个库首先需要渲染PayPhone框。以下示例我将使用纯PHP。

<?php

use sshgun\PayphonePHP\PayButtonBox;

require 'vendor/autoload.php';

# Your Application token must be defined here
const PAYPHONE_TOKEN = "";

$id = bin2hex(random_bytes(5));

# here We use a fake transaction of 1$. the 100 is because the API amount must
# be multiplied by 100. on the process
$box = new PayButtonBox(PAYPHONE_TOKEN, $id, 100);

?>

<!DOCTYPE html>
<html>

<head>
    <!-- Include the box CSS-->
    <link rel="stylesheet" href="<?= $box->cssAsset() ?>">
</head>

<body>
    <!-- Include the official API box js -->
    <script type="module" src="<?= $box->jsAsset() ?>"></script>

    <!-- render the box with js, this use a DOMContentLoaded to render the box -->
    <?= $box->renderLoadJS() ?>
</body>

</html>

之后,PayPhone将向配置的响应URL发送请求。你可以使用这个库来处理这个请求。

<?php

use sshgun\PayphonePHP\PayphoneClient;

require "vendor/autoload.php";

# your app token here
const TEST_TOKEN = "";

# we create a payphone client that will make the http request to the server

$client = new PayphoneClient(TEST_TOKEN);

$id = $_GET['id'];
$clientId = $_GET['clientTransactionId'];

# You should validate $id and $clientId before use it on the request.
# please filter the inputs. ;)

$data = $client->postPaymentConfirmation($id, $clientId);
if (is_null($data)) {
    # something wrong happened you can get the error with the getError method
    $err = $client->getError();
    die();

}

# If everything was ok $data must containt the returned data for the Payphone
# transaction.

var_dump($data);

支付链接

PayPhone提供了一个重定向模型,我们可以创建一个用户必须点击的链接来进行支付。这个行为使用从你的网站到PayPhone域名的重定向,然后PayPhone服务器使用响应URL或取消URL将用户重定向回来。

要使用这个库创建支付,你只需要创建一个参数对象,并将用户重定向到准备端点返回的URL。

<?php

const PAYPHONE_TOKEN="YOUR app private token";

$args = new PreparedPaymentArgs;
$args->amount = 100; # 1$
$args->amountWithoutTax = 100; 
$args->responseUrl = "https://your-comback-url";
$args->cancellationUrl = "https://your-cancel-comback-url";
# the clientTransactionId is your internal identificator, can be the id of your
# orders or transaction table.
$args->clientTransactionId = "your-internal-id";

$client = new PayphoneClient(PAYPHONE_TOKEN); 

$data = $client->postCreatePaymentLinks($args);
if(is_null($data)){
    # an error ocurred. you can get the information on 
    $err = $client->getError();
    # show some information to the user.
    die();
}
# the operation was ok son wee need to redirect the user to the returned url
# here we only show the links.
?>

<a href="<?= $data->payWithCard =>">Pagar con Tarjeta</a>
<a href="<?= $data->payWithPayPhone =>">Pagar con Payphone</a>