valterjrdev/moip-php

MoIP集成库

此包的官方存储库似乎已丢失,因此该包已被冻结。

1.3 2017-01-27 18:45 UTC

This package is not auto-updated.

Last update: 2021-01-23 00:29:34 UTC


README

Moip-PHP是一个库,它实现了MoIP指令XML的抽象层,允许您集成API服务而不在代码中添加多个XML行。快速示例

include __DIR__ . '/vendor/autoload.php';

use Moip\Moip;

$moip = new Moip();
$moip->setEnvironment('test');
$moip->setCredential(array(
    'key' => 'ABABABABABABABABABABABABABABABABABABABAB',
    'token' => '01010101010101010101010101010101'
    ));

$moip->setUniqueID(false);
$moip->setValue('100.00');
$moip->setReason('Teste do Moip-PHP');

$moip->validate('Basic');

print_r($moip->send());

Moip-PHP使用Fluent接口模式,因此您可以使用以下方式执行上述示例

include __DIR__ . '/vendor/autoload.php';
 
use Moip\Moip;

$moip = new Moip(); 
print_r($moip->setEnvironment('test')
        ->setCredential(array(
    'key' => 'ABABABABABABABABABABABABABABABABABABABAB',
    'token' => '01010101010101010101010101010101'
    ))->setUniqueID(false)
        ->setValue('100.00')
        ->setReason('Teste do Moip-PHP')
        ->validate('Basic')
        ->send());

可用方法

下面是可用方法的列表和详细说明,您可以使用这些方法与Moip-PHP一起使用。

Moip()

构造函数。

Moip()

$moip = new Moip();

setEnvironment()

此方法用于定义请求将被处理的环境,'test'用于定义将使用Moip沙盒测试环境,省略此方法则表示请求将在Moip生产环境中处理。

注意:定义环境时,请确保您正在使用对应环境的认证,Moip中每个环境都有自己的API认证密钥。

setEnvironment($environment)

$moip->setEnvironment('test');

$environment : String ('test')

setCredential()

Moip需要您进行认证才能在API中处理请求,为此,在执行任何请求之前,您必须向Moip提供您的API凭证,由TOKEN和KEY组成。

参数$credentials是一个关联数组,包含键key和token(例如:array('key' => 'sua_key','token' => 'seu_token'))。如果您还没有这些数据,请查看如何在您的沙盒账户中获取它们。

setCredential($credential)

$moip->setCredential(array(
        'key' => 'SUA_KEY',
    	'token' => 'SEU_TOKEN'
    	));

$credential : Array('key','token')

validate()

  1. validate()方法将对您要处理的指令类型的必填数据进行验证,您可以选择两种验证级别之一:'Basic'和'Identification'。
  2. Basic:将验证为发送XML请求到MoIP所需的最小数据。

Identification:将验证处理带有MoIP标识的XML所需的数据,通常用于在MoIP结账页的第二步中重定向客户或使用MoIP透明支付。

validate($validateType)

$moip->validate('Identification');

$validateType : String ('Basic' 或 'Identification')

setUniqueID()

  1. setUniqueID()方法为XML中的<IdProprio>标签分配值。

<IdProprio>:您的唯一订单标识符,这些信息将在我们的状态变更通知中发送给您,以便您能够识别订单并处理其状态。

setUniqueID($id)

$moip->setUniqueID('ABCD123456789');

$id : String

setValue()

  1. setValue()方法为XML中的<Valor>标签分配值。

<Valor>:负责定义应支付的价值。

setValue($value)

$moip->setValue('100.00');	

setAdds()

setAdds() 方法将值分配给 Moip XML 中的 "<Acrescimo>" 标签。

  1. <Acrescimo>: 负责定义需要支付的额外金额。

setAdds($value)

setValue($value)

$moip->setAdds('15.00');	

setDeduct()

setDeduct() 方法将值分配给 Moip XML 中的 "<Deducao>" 标签。

  1. <Deducao>: 负责定义从应付总额中扣除的折扣金额。

setDeduct($value)

setValue($value)

$moip->setDeduct('15.00');

setReason()

setReason() 方法将值分配给 Moip XML 中的 "<Razao>" 标签。

  1. <Razao>: 负责定义付款的原因。
  2. 在付款指令中,此字段始终是必需的。

setReason($value)

$value : 字符串

$moip->setReason('Pagamento de teste do Moip-PHP');

setPayer()

setPayer() 方法将值分配给 Moip XML 中的 "<Pagador>" 节点。

  1. <Pagador>: 包含进行付款的人的信息的节点。
  2. name : <Nome> : 付款人的全名
  3. email : <Email> : 付款人的电子邮件
  4. payerId : <IdPagador> : 付款人的唯一标识符
  5. identity : <Identidade> : 付款人的身份 (CPF)
  6. phone : <TelefoneCelular> : 付款人的次要联系电话
  7. billingAddress : <EnderecoCobranca> : 付款人的地址
  8. address : <Logradouro> : 付款人的街道、大道、道路等
  9. number : <Numero> : 付款人的住宅号码
  10. complement : <Complemento> : 付款人地址的补充信息
  11. city : <Cidade> : 付款人地址的城市
  12. neighborhood : <Bairro> : 付款人地址的街区
  13. state : <Estado> : 付款人地址的州,ISO-CODE 格式 (UF)
  14. country : <Pais> : 付款人地址的国家,ISO-CODE 格式
  15. zipCode : <CEP> : 付款人地址的邮政编码
  16. phone : <TelefoneFixo> : 付款人的联系电话

setPayer($value)

$value : 数组 ('name','email','payerId','identity', 'phone','billingAddress' => Array('address','number','complement','city','neighborhood','state','country','zipCode','phone'))

$moip->setPayer(array('name' => 'Nome Sobrenome',
    	'email' => 'email@cliente.com.br',
    	'payerId' => 'id_usuario',
        'billingAddress' => array('address' => 'Rua do Z�zinho Cora��o',
        		'number' => '45',
        		'complement' => 'z',
        		'city' => 'S�o Paulo',
        		'neighborhood' => 'Palha�o J�o',
        		'state' => 'SP',
        		'country' => 'BRA',
        		'zipCode' => '01230-000',
        		'phone' => '(11)8888-8888')));

addPaymentWay()

addPaymentWay() 方法将值分配给 Moip XML 中 "<FormasPagamento>" 节点的 "<FormaPagamento>" 标签。

<FormaPagamento>: 定义在 Moip 检查站向付款人显示的付款方式。

  1. billet : 在 Moip 检查站提供 "银行汇票" 作为付款方式。
  2. financing : 在 Moip 检查站提供 "分期付款" 作为付款方式。
  3. debit : 在 Moip 检查站提供 "账户扣款" 作为付款方式。
  4. creditCard : 在 Moip 检查站提供 "信用卡" 作为付款方式。
  5. debitCard : 在 Moip 检查站提供 "借记卡" 作为付款方式。

addPaymentWay($way)

$way : 字符串 ('billet','financing','debit','creditCard','debitCard')

$moip->addPaymentWay('creditCard');
$moip->addPaymentWay('billet');
$moip->addPaymentWay('financing');
$moip->addPaymentWay('debit');
$moip->addPaymentWay('debitCard');

setBilletConf()

setBilletConf() 方法将值分配给 Moip XML 中负责定义额外配置和银行汇票定制的 "<Boleto>" 节点。

  1. $expiration : 格式为 "AAAA-MM-DD" 的日期或天数。
  2. $workingDays : 如果 "$expiration" 是天数,您可以设置 "true" 以在非工作日计数,默认为连续日。
  3. $instructions : 打印在汇票上的附加消息,最多三条消息。
  4. $uriLogo : 您的徽标 URL,最大尺寸为 75px 宽度 x 40px 高度。

setBilletConf($expiration, $workingDays, $instructions, $uriLogo)

$expiration : 整数或日期

$workingDays : 布尔值

$instructions : 数组

$uriLogo : 字符串

$moip->setBilletConf("2011-04-06",
        	false,
        	array("Primeira linha",
            	"Segunda linha",
            	"Terceira linha"),
        	"http://seusite.com.br/logo.gif");

addMessage()

addMessage() 方法为 XML Moip 中的 "<Mensagens>" 节点的 "<Mensagem>" 标签赋值。

  1. <Mensagens>: 包含 "<Mensagens>" 的节点。
  2. <Mensagem>: 定义在 Moip 检出页显示的附加消息的标签。

addMessage($msg)

$msg : 字符串

$moip->addMessage('Seu pedido contem os produtos X,Y e Z.');

setReturnURL()

setReturnURL() 方法为 XML Moip 中的 "<URLRetorno>" 标签赋值,该标签负责在 Moip 检出页完成支付后,将买家重定向到指定的 URL。

setReturnURL($url)

$url : 字符串

$moip->setReturnURL('https://meusite.com.br/cliente/pedido/bemvindodevolta');

setNotificationURL()

setNotificationURL() 方法为 XML Moip 中的 "<URLNotificacao>" 标签赋值,该标签负责定义 Moip 向其发送支付状态更改通知的 URL。

setNotificationURL($url)

$url : 字符串

$moip->setNotificationURL('https://meusite.com.br/nasp/');

addComission()

addComission() 方法为 XML Moip 中的 "<Comissoes>" 标签赋值,负责为交易分配次级接收者。

  1. $reason : 次级接收者将收到的金额的原因/理由。
  2. $receiver: 接收金额的用户的 Moip 登录名。
  3. $value : 分配给次级接收者的金额。
  4. $percentageValue: 如果为 "true",则定义金额将根据交易总金额的百分比计算。
  5. $ratePayer: 如果为 "true",则定义该次级接收者将使用收到的金额支付 Moip 的费用。

addComission($reason, $receiver, $value, $percentageValue, $ratePayer)

$reason : 字符串

$receiver : 字符串

$value : 数字

$percentageValue: 布尔值

$ratePayer : 布尔值

$moip->addComission('Raz�o do Split',
		'recebedor_secundario',
		'5.00');
$moip->addComission('Raz�o do Split',
		'recebedor_secundario_2',
		'12.00',
		true,
		true);

addParcel()

addParcel() 方法为 XML Moip 中的 "<Parcelamentos>" 标签赋值,负责配置可供付款人选择的分期付款选项。

  1. $min : 付款人可用的分期付款的最小数量。
  2. $max : 付款人可用的分期付款的最大数量。
  3. $rate : 每期利息的月利率。
  4. $transfer : 如果为 "true",则定义 Moip 的标准利息值将由付款人支付。

addParcel($min, $max, $rate, $transfer)

$min : 数字

$max : 数字

$rate : 数字

$transfer : 布尔值

$moip->addParcel('2', '4');
$moip->addParcel('5', '7', '1.00');
$moip->addParcel('8', '12', null, true);

setReceiver()

setReceiver() 方法为 "<Recebedor>" 节点的 "<LoginMoIP>" 标签赋值,该标签用于识别将在 Moip 接收付款的 Moip 用户。

  1. $receiver : 主要接收者的 Moip 登录名。

setReceiver($receiver)

$receiver : 字符串

$moip->setReceiver('integracao@labs.moip.com.br');

getXML()

getXML() 方法将返回包含您已配置的所有属性的生成的 XML,该方法可以帮助您确切地知道您将发送给 Moip 的 XML。

getXML()

$moip = new Moip();
$moip->setEnvironment('test');
$moip->setCredential(array(
    'key' => 'ABABABABABABABABABABABABABABABABABABABAB',
    'token' => '01010101010101010101010101010101'
    ));
$moip->setUniqueID(false);
$moip->setValue('100.00');
$moip->setReason('Teste do Moip-PHP');
$moip->validate('Basic');

print_r($moip->getXML());

    //IR�? IMPRIMIR
    <?xml version="1.0" encoding="utf-8"?>
    <EnviarInstrucao>
        <InstrucaoUnica>
            <IdProprio></IdProprio>
            <Razao>Teste do Moip-PHP</Razao>
            <Valores>
                <Valor moeda="BRL">100.00</Valor>
            </Valores>
        </InstrucaoUnica>
    </EnviarInstrucao>

send()

send() 方法执行向 Moip 发送指令,并返回从 Moip 获得的响应数据。

  1. response : 成功时为 "true",发生错误时为 "false"。
  2. error : 当 "response" 为 "false" 时始终返回一个消息。
  3. xml: 当 "response" 为 "true" 时始终返回 Moip 的响应 XML。

send()

$moip = new Moip();
$moip->setEnvironment('test');
$moip->setCredential(array(
    'key' => 'ABABABABABABABABABABABABABABABABABABABAB',
    'token' => '01010101010101010101010101010101'
    ));
$moip->setUniqueID(false);
$moip->setValue('100.00');
$moip->setReason('Teste do Moip-PHP');
$moip->validate('Basic');

print_r($moip->send());

    //IR�? IMPRIMIR
    stdClass Object
    (
        [response] => 1
        [error] =>
        [xml] => <ns1:EnviarInstrucaoUnicaResponse xmlns:ns1="http://www.moip.com.br/ws/alpha/"><Resposta><ID>201209042007216380000000989104</ID><Status>Sucesso</Status><Token>M2C031R2Q0Z9W0Y4Q2S0H0W7E2G1Z6P3E8C0C0W050T01070Y9Y8V9G1F0F4</Token></Resposta></ns1:EnviarInstrucaoUnicaResponse>
    )

getAnswer()

getAnswer() 方法返回 Moip 的响应数据,以对象形式呈现。

  1. response:当 Moip 返回 "" 为 "Sucesso" 时为 "true",当返回 "Falha" 时为 "false"。
  2. error : 当 "response" 为 "false" 时始终返回一个消息。
  3. token:当 "response" 为 "true" 时,返回生成的支付 TOKEN。
  4. payment_url:当 "response" 为 "true" 时,返回准备好的 Moip 结账 URL,用于将客户重定向到包含支付 TOKEN 的页面。

getAnswer()

$moip = new Moip();
$moip->setEnvironment('test');
$moip->setCredential(array(
    'key' => 'ABABABABABABABABABABABABABABABABABABABAB',
    'token' => '01010101010101010101010101010101'
    ));

$moip->setUniqueID(false);
$moip->setValue('100.00');
$moip->setReason('Teste do Moip-PHP');
$moip->validate('Basic');
$moip->send();

print_r($moip->getAnswer());

//IR�? IMPRIMIR
stdClass Object
(
    [response] => 1
    [error] =>
    [token] => 92D091R2I0Y9X0E4T2K034L2H2V4H2J6L9R0S0T0K0N0L0T0Y9H879H144O8
    [payment_url] => https://desenvolvedor.moip.com.br/sandbox/Instrucao.do?token=92D091R2I0Y9X0E4T2K034L2H2V4H2J6L9R0S0T0K0N0L0T0Y9H879H144O8
)

queryParcel()

queryParcel() 方法返回一个 Array(),包含分期付款信息、每期付款金额以及基于模拟利率的总付款金额。

  1. REQUEST

  2. $login:用户的 Moip 登录名。

  3. $maxParcel:要查询的最大分期数。

  4. $rate:模拟利率。

  5. $simulatedValue:要模拟支付的金额。

  6. RESPONSE

  7. response:"true" 表示 Moip 的 "" 为 "Sucesso" 的响应,"false" 表示 "Falha" 的响应。

  8. installment:对应值的分期数。

  9. total:应支付的总金额。

  10. rate:分配的利率。

  11. value:每期付款金额。

queryParcel($login, $maxParcel, $rate, $simulatedValue)

$login:字符串。

$maxParcel:数字。

$rate : 数字

$simulatedValue:数字。

    $moip = new Moip();
    $moip->setEnvironment('test');
    $moip->setCredential(array(
        'key' => 'ABABABABABABABABABABABABABABABABABABABAB',
        'token' => '01010101010101010101010101010101'
        ));


    print_r($moip->queryParcel('integracao@labs.moip.com.br', '4', '1.99', '100.00'));


    //IR�? IMPRIMIR
    Array
    (
        [response] => 1
        [installment] => Array
            (
                [1] => Array
                    (
                        [total] => 100.00
                        [rate] => 1.99
                        [value] => 100.00
                    )


                [2] => Array
                    (
                        [total] => 103.00
                        [rate] => 1.99
                        [value] => 51.50
                    )


                [3] => Array
                    (
                        [total] => 104.01
                        [rate] => 1.99
                        [value] => 34.67
                    )


                [4] => Array
                    (
                        [total] => 105.04
                        [rate] => 1.99
                        [value] => 26.26
                    )


            )


    )