PHPackages                             get2/a2uphp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. get2/a2uphp

ActiveLibrary[Payment Processing](/categories/payments)

get2/a2uphp
===========

PHP package to make App to User payment for Pi Network

1.9.3.x-dev(1y ago)285PHP

Since Dec 28Pushed 1y agoCompare

[ Source](https://github.com/jesssewovon/pi-php)[ Packagist](https://packagist.org/packages/get2/a2uphp)[ RSS](/packages/get2-a2uphp/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

[![Build Status](https://github.com/laravel/framework/workflows/tests/badge.svg)](https://packagist.org/packages/get2/a2uphp)[![Total Downloads](https://camo.githubusercontent.com/b0a7ee2f3f0208712ecb1081b9d6fcf05045c10cd6f345abcb924f5512c0d346/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c61726176656c2f6672616d65776f726b)](https://packagist.org/packages/get2/a2uphp)

Pi Network - PHP server-side package
====================================

[](#pi-network---php-server-side-package)

This is a Pi Network PHP package you can use to integrate the Pi Network apps platform with a PHP backend application.

Install
-------

[](#install)

Install this package as a dependency of your app:

```
# With composer:
composer require get2/a2uphp:dev-main

```

Example
-------

[](#example)

1. Initialize the SDK

```
require __DIR__ . '/vendor/autoload.php';
use Get2\A2uphp\PiNetwork;

// DO NOT expose these values to public
$apiKey = "YOUR_PI_API_KEY";
$walletPrivateSeed = "S_YOUR_WALLET_PRIVATE_SEED"; // starts with S
$pi = new PiNetwork($apiKey, $walletPrivateSeed);
```

2. Create an A2U payment

Make sure to store your payment data in your database. Here's an example of how you could keep track of the data. Consider this a database table example.

uidproduct\_idamountmemopayment\_idtxid`userUid`apple-pie-13.14Refund for apple pieNULLNULL```
$userUid = "user_uid_of_your_app";//recipient uid
$paymentData = [
    "payment" => [
	  "amount" => 1,
	  "memo" => "Refund for apple pie", // this is just an example
	  "metadata" => ["productId" => "apple-pie-1"],
	  "uid" => $userUid
    ],
];
// It is critical that you store paymentId in your database
// so that you don't double-pay the same user, by keeping track of the payment.
$paymentId = $pi->createPayment($paymentData);
```

3. Store the `paymentId` in your database

After creating the payment, you'll get `paymentId`, which you should be storing in your database.

uidproduct\_idamountmemopayment\_idtxid`userUid`apple-pie-13.14Refund for apple pie`paymentId`NULL4. Submit the payment to the Pi Blockchain

```
// It is strongly recommended that you store the txid along with the paymentId you stored earlier for your reference.
$txid = $pi->submitPayment($paymentId);
```

5. Store the txid in your database

Similarly as you did in step 3, keep the txid along with other data.

uidproduct\_idamountmemopayment\_idtxid`userUid`apple-pie-13.14Refund for apple pie`paymentId``txid`6. Complete the payment

```
$completedPayment = $pi->completePayment($paymentId, $txid);
```

Overall flow for A2U (App-to-User) payment
------------------------------------------

[](#overall-flow-for-a2u-app-to-user-payment)

To create an A2U payment using the Pi PHP SDK, here's an overall flow you need to follow:

1. Initialize the SDK

> You'll be initializing the SDK with the Pi API Key of your app and the Private Seed of your app wallet.

2. Create an A2U payment

> You can create an A2U payment using `createPayment` method. This method returns a payment identifier (payment id).

3. Store the payment id in your database

> It is critical that you store the payment id, returned by `createPayment` method, in your database so that you don't double-pay the same user, by keeping track of the payment.

4. Submit the payment to the Pi Blockchain

> You can submit the payment to the Pi Blockchain using `submitPayment` method. This method builds a payment transaction and submits it to the Pi Blockchain for you. Once submitted, the method returns a transaction identifier (txid).

5. Store the txid in your database

> It is strongly recommended that you store the txid along with the payment id you stored earlier for your reference.

6. Complete the payment

> After checking the transaction with the txid you obtained, you must complete the payment, which you can do with `completePayment` method. Upon completing, the method returns the payment object. Check the `status` field to make sure everything looks correct.

SDK Reference
-------------

[](#sdk-reference)

This section shows you a list of available methods.

### `createPayment`

[](#createpayment)

This method creates an A2U payment.

- Required parameter: `paymentArgs`

You need to provide 4 different data and pass them as a single object to this method.

```
$paymentArgs = [
	"payment" => [
	  "amount" => double, // the amount of Pi you're paying to your user
	  "memo" => string, // a short memo that describes what the payment is about
	  "metadata" => Array // an arbitrary Array that you can attach to this payment. This is for your own use. You should use this Array as a way to link this payment with your internal business logic.
	  "uid" => string // a recipient user uid of your app. You should have access to this value if a user has authenticated on your app.
	]
]
```

- Return value: `a payment identifier (paymentId: string)`

### `submitPayment`

[](#submitpayment)

This method creates a payment transaction and submits it to the Pi Blockchain.

- Required parameter: `paymentId`
- Return value: `a transaction identifier (txid: string)`

### `completePayment`

[](#completepayment)

This method completes the payment in the Pi server.

- Required parameter: `paymentId, txid`
- Return value: `a payment object (payment: PaymentDTO)`

The method return a payment object with the following fields:

```
PaymentDTO = {
  // Payment data:
  identifier: string, // payment identifier
  user_uid: string, // user's app-specific ID
  amount: number, // payment amount
  memo: string, // a string provided by the developer, shown to the user
  metadata: object, // an object provided by the developer for their own usage
  from_address: string, // sender address of the blockchain transaction
  to_address: string, // recipient address of the blockchain transaction
  direction: Direction, // direction of the payment ("user_to_app" | "app_to_user")
  created_at: string, // payment's creation timestamp
  network: string, // a network of the payment ("Pi Network" | "Pi Testnet")
  // Status flags representing the current state of this payment
  status: {
    developer_approved: boolean, // Server-Side Approval (automatically approved for A2U payment)
    transaction_verified: boolean, // blockchain transaction verified
    developer_completed: boolean, // Server-Side Completion (handled by the create_payment! method)
    cancelled: boolean, // cancelled by the developer or by Pi Network
    user_cancelled: boolean, // cancelled by the user
  },
  // Blockchain transaction data:
  transaction: null | { // This is null if no transaction has been made yet
    txid: string, // id of the blockchain transaction
    verified: boolean, // true if the transaction matches the payment, false otherwise
    _link: string, // a link to the operation on the Pi Blockchain API
  }
}
```

### `getPayment`

[](#getpayment)

This method returns a payment object if it exists.

- Required parameter: `paymentId`
- Return value: `a payment object (payment: PaymentDTO)`

### `cancelPayment`

[](#cancelpayment)

This method cancels the payment in the Pi server.

- Required parameter: `paymentId`
- Return value: `a payment object (payment: PaymentDTO)`

### `getIncompleteServerPayments`

[](#getincompleteserverpayments)

This method returns the latest incomplete payment which your app has created, if present. Use this method to troubleshoot the following error: "You need to complete the ongoing payment first to create a new one."

- Required parameter: `none`
- Return value: `an array which contains 0 or 1 payment object (payments: Array)`

If a payment is returned by this method, you must follow one of the following 3 options:

1. cancel the payment, if it is not linked with a blockchain transaction and you don't want to submit the transaction anymore
2. submit the transaction and complete the payment
3. if a blockchain transaction has been made, complete the payment

If you do not know what this payment maps to in your business logic, you may use its `metadata` property to retrieve which business logic item it relates to. Remember that `metadata` is a required argument when creating a payment, and should be used as a way to link this payment to an item of your business logic.

Troubleshooting
---------------

[](#troubleshooting)

### Error when creating a payment: "You need to complete the ongoing payment first to create a new one."

[](#error-when-creating-a-payment-you-need-to-complete-the-ongoing-payment-first-to-create-a-new-one)

See documentation for the `getIncompleteServerPayments` above.

Full example
------------

[](#full-example)

```
