watts25/naranja-payment-sdk

Naranja Ranty - 检查out SDK

0.1.4 2020-07-15 13:20 UTC

This package is auto-updated.

Last update: 2024-09-15 23:19:02 UTC


README

需求

  • Composer
  • PHP >=7.1
  • PHP 扩展:curl | json | mbstring | dom

安装

# Creamos la carpeta para el sdk
mkdir sdk
# Ingresamos a la carpeta
cd sdk
# Descomprimimos el SDK
unzip php-checkout-sdk.zip
# Instalamos las dependencias en /vendor
composer install

使用示例

第一步是创建一个包含我们想要接收的付款所有信息的 PaymentRequest

<?php
// Importamos el SDK y las clases
require_once(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../checkoutApi.php');

// Instanciamos la clase NaranjaCheckout
// Utilizamos el CLIENT_ID y CLIENT_SECRET provistos por Naranja
// En 'ENVIRONMENT', para las pruebas usamos 'sandbox' y para nuestro entorno productivo 'production'
// Tener en cuenta que el CLIENT_ID y el CLIENT_SECRET son distintos para sandbox y para producción

$naranjaCheckout = new NaranjaCheckout('CLIENT_ID','CLIENT_SECRET','ENVIROMENT');

// Definimos el primer producto
$product1 = new Naranja\CheckoutApi\Model\ProductItem();
$product1->setName('Veggies');
$product1->setDescription('Granja del sol');
$product1->setQuantity(2);

// Definimos unit_price
$unitPrice1 = new Naranja\CheckoutApi\Model\Amount();
$unitPrice1->setCurrency('ARS');
$unitPrice1->setValue('115');

// Agregamos el unitPrice al producto
$product1-> setUnitPrice($unitPrice1);

// Definimos el segundo producto
$product2 = new Naranja\CheckoutApi\Model\ProductItem();
$product2->setName('Croquetas');
$product2->setDescription('Granja del sol'); 
$product2->setQuantity(1);

// Definimos el unit price del segundo producto
$unitPrice2 = new Naranja\CheckoutApi\Model\Amount();
$unitPrice2->setCurrency('ARS');
$unitPrice2->setValue('115');

// Agregamos el unitPrice al producto
$product2->setUnitPrice($unitPrice2);

// Definimos el objeto transaccion
$transaction = new Naranja\CheckoutApi\Model\Transaction();

// Definimos el Amount
$amountTransaction = new Naranja\CheckoutApi\Model\Amount();
$amountTransaction->setCurrency('ARS'); 
$amountTransaction->setValue('345');

// Agregamos el objeto amount a la transaccion
$transaction->setAmount($amountTransaction);
$transaction->setSoftDescriptor('GOFRIZ CONGELADOS');
$transaction->setProducts([$product1, $product2]);

// Generamos el payment request
$paymentRequest = new Naranja\CheckoutApi\Model\PaymentRequest();
$paymentRequest->setPaymentType('web_checkout');
$paymentRequest->setAuthorizationMode('SALE');
$paymentRequest->setExternalPaymentId('123456789');
$paymentRequest->setTransactions([$transaction]);

// Definimos el Requests creation redirect
$requestsCreationRedirect = new Naranja\CheckoutApi\Model\RequestCreationRedirect();
$requestsCreationRedirect->setSuccessUrl('https://gofriz.com.ar/success');
$requestsCreationRedirect->setFailureUrl('https://gofriz.com.ar/failure');

// Agregamos el requests redirect al paymenRequests
$paymentRequest->setRequestCreationRedirect($requestsCreationRedirect);
$paymentRequest->setCallbackUrl('https://gofriz.com.ar/notification');

// Ejecutamos el metodo
$response = $naranjaCheckout->createPaymentRequest($paymentRequest);

// Transformamos el JSON a datos nativo de PHP
$response = json_decode($response, true);

print_r($response['checkout_url'] . PHP_EOL);

然后,为了接收通知,我们必须实现一个端点来接收这些通知(必须是我们在 $paymentRequest->setCallbackUrl 中指定的相同端点)

<?php
// Importamos el SDK y las clases
require_once(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../checkoutApi.php');

// Instanciamos la clase de NaranjaCheckout
$naranjaCheckout = new NaranjaCheckout('CLIENT_ID','CLIENT_SECRET','ENVIROMENT');

// Las notificaciones sobre el pago llegan via POST
// Por lo tanto solo nos preocupamos por ese verbo http
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // El id del pago llega via request body
    $body = json_decode(file_get_contents('php://input'), true);

    // Solicitamos los detalles del pago
    $payment = $naranjaCheckout->getPayment($body['id']);

    // Mostrar los detalles del pago
    $payment = $payment->__toString();
    print_r($payment);
}
?>

测试

为了测试检查out流程,一旦进入web表单,我们将要求您登录。我们有两个测试用户,您可以使用他们来模拟不同的场景

API

class NaranjaCheckout(string $client_id, string $client_secret, string $environment);
  • client_id:客户ID
  • client_secret:客户密钥
  • environment:我们想要使用的环境(sandbox | prod)

方法

createPaymentRequest

用于创建 paymentRequest

createPaymentRequest($paymentRequestInfo);
  • paymentRequestInfo:实例化的 Naranja\CheckoutApi\Model\PaymentRequest 模型

getPaymentRequest

getPaymentRequest(string $id);
  • id:要检查的 payment request ID

getPaymentByExternalId

getPaymentByExternalId(string $external_payment_id);
  • external_payment_id:在创建 payment request 时配置的外部 ID

getPayment

getPayment(string $id);
  • id:要检查的 payment ID

模型

Naranja\CheckoutApi\Model\PaymentRequest()

方法

setPaymentType( string )

付款类型。仅允许 "web_checkout"。

setAuthorizationMode( string )

授权模式。仅允许 "SALE"。

setExternalPaymentId( string )

一个外部 ID,以便卖家可以识别销售。

setTransactions( [ Naranja\CheckoutApi\Model\Transaction() ] )

即使只有一个交易,我们也必须传递一个数组。

setAdditionalInfo( string ) [optional]

附加信息。

setSellerData( Naranja\CheckoutApi\Model\SellerData() ) [optional]

表示卖家数据

setRequestCreationRedirect( Naranja\CheckoutApi\Model\RequestCreationRedirect() )

用户完成检查out流程后重定向到的URL。

setCallbackUrl( string )

用于发送有关交易状态的通知的URL。

Naranja\CheckoutApi\Model\ProductItem()[optional]

表示购买中的项。

方法

setName( string )

产品名称。

setDescription( string )

产品描述。

setQuantity( int )

购买的产品数量。

setUnitPrice( Naranja\CheckoutApi\Model\Amount() )

产品的单价。

Naranja\CheckoutApi\Model\Amount()

方法

setCurrency( string )

购买时使用的货币。仅接受 "ARS"。

setValue( string )

购买值。

Naranja\CheckoutApi\Model\Transaction()

方法

setAmount( Naranja\CheckoutApi\Model\Amount() )

交易总金额。

setSoftDescriptor( string )[optional]

用于注册消费并在账单摘要中显示的描述。

setProducts( [ Naranja\CheckoutApi\Model\ProductItem() ] )

购买产品。这些信息仅作为客户参考。

Naranja\CheckoutApi\Model\SellerData()

方法

setStoreId( 字符串 )

店铺标识符

setSellerId( 字符串 )

卖家或商家标识符

setPosId( 字符串 )

POS标识符

setSystem( 字符串 )

卖家系统名称

setGeocode( 字符串 )

地理编码

setInvoiceData( 字符串 )

发票票据的可选数据

Naranja\CheckoutApi\Model\RequestCreationRedirect()

方法

setSuccessUrl( 字符串 )

当交易成功时,将客户发送到的URL。

setFailureUrl( 字符串 )

当交易失败时,将客户发送到的URL。