jlkaufman/cakephp-paypal-rest-client

此插件是Paypal API的完整REST客户端。它实现了Paypal REST API中所有可用的功能。

v0.1-alpha 2014-10-05 13:40 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:42:32 UTC


README

#CakePHP Paypal REST客户端(PaypalSource)

介绍

此插件是Paypal API的完整REST客户端。它实现了Paypal REST API中所有可用的功能。

不使用CURL,而是使用CakePHP的HTTPSocket类。

免责声明

Alpha版本...可能现在还不建议在生产环境中使用...

文档:目录

  1. 安装
  2. 配置
  3. 使用

安装

  1. 将仓库克隆到 app/Plugins/PaypalSource 目录
cd app/Plugins  
git clone https://github.com/jlkaufman/cakephp-paypal-rest-client.git PaypalSource
  1. 使用 composer 安装
require": {
    "jlkaufman/cakephp-paypal-rest-client": "0.1-alpha"
}

配置

database.php 中添加以下块并相应填写:

// Paypal
	public $Paypal = array(
		'datasource'     => 'PaypalSource.PaypalSource',
		'environment'    => 'sandbox', // Production: 'production'   SandBox: 'sandbox'
		'username'       => '',
		'password'       => '',
		'receiver_email' => ''
	);

使用

在控制器中包含Paypal模型

  • PaypalSource.Paypal 添加到您的 $uses 数组中。
    例如: public $uses = array('PaypalSource.Paypal');

    现在您可以从控制器中通过 $this->Paypal->method(); 调用模型。

关于返回响应的说明

Paypal类中的所有方法都将返回一个StdClass实例。在文档中,我们以JSON形式显示对象,仅为了可读性。您可以使用箭头符号访问对象的每个成员。

信用卡支付

创建信用卡支付非常简单。我们可以做两件事之一;我们可以创建一笔销售(最终支付),或者我们可以授权一个金额,稍后我们将捕获这笔支付。

  • $data 将包含我们将发送给Paypal的所有信息

信用卡

$data = array(
	'credit_card' => array(
		"number"       => "4417119669820331",
		"type"         => "visa",
		"expire_month" => 1,
		"expire_year"  => 2018,
		"cvv2"         => "874",
		"first_name"   => "Joe",
		"last_name"    => "Shopper"
	),
	'billing_address' => array(
		"line1"        => "52 N Main ST",
		"line2"        => "Apt. 211",
		"city"         => "Johnstown",
		"country_code" => "CA",
		"postal_code"  => "H0H 0H0",
		"state"        => "Quebec"
	),
	'transaction' => array(
		"amount" => array(
			"total"    => "7.47",
			"currency" => "USD",
			"details"  => array(
				"subtotal" => "7.41",
				"tax"      => "0.03",
				"shipping" => "0.03"
			)
		),
		"description" => "This is the payment transaction description."
	)
);

$response = $this->Paypal->creditCardPayment($data, $type);
	

信用卡令牌

$data = array(
	'credit_card_token' => array(
		"credit_card_id" => "CARD-7VH15004HC811510SKEGDHDI"
	),
	'transaction' => array(
		"amount" => array(
			"total"    => "7.47",
			"currency" => "USD",
			"details"  => array(
				"subtotal" => "7.41",
				"tax"      => "0.03",
				"shipping" => "0.03"
			)
		),
		"description" => "This is the payment transaction description."
	)
);

$response = $this->Paypal->creditCardPayment($data, $type);             
  • $type 可以是 authorizationsale
    sale:最终销售,并完成交易
    authorization:授权卡用于指定的金额。我们稍后必须捕获这笔支付。
示例响应
{
   "id": "PAY-30J08441N2038343CKLJHKEA",
   "status": "approved",
   "created": "2014-01-12 10:57:20",
   "modified": "2014-01-12 10:57:29",
   "payment_method": "credit_card",
   "type": "authorize",
   "payer": {
      "billing_address": {
         "line1": "52 N Main ST",
         "line2": "Apt. 211",
         "city": "Johnstown",
         "state": "Quebec",
         "postal_code": "H0H 0H0",
         "country_code": "CA"
      },
      "credit_card": {
         "type": "visa",
         "number": "xxxxxxxxxxxx0331",
         "expire_month": "1",
         "expire_year": "2018",
         "first_name": "Joe",
         "last_name": "Shopper"
      },
      "id": "",
      "email": ""
   },
   "approval_url": "",
   "transaction": {
      "amount": {
         "total": "7.47",
         "currency": "USD",
         "details": {
            "subtotal": "7.41",
            "tax": "0.03",
            "shipping": "0.03"
         }
      },
      "description": "This is the payment transaction description.",
      "sale": {
         "id": "",
         "parent_id": ""
      },
      "authorization": {
         "id": "10V50318J8770814T",
         "created": "2014-01-12 10:57:20",
         "parent_id": "PAY-30J08441N2038343CKLJHKEA"
      }
   },
   "error": {
      "code": false
   }
}

捕获授权

要捕获授权,必须创建授权(将 $type 设置为 authorization 的信用卡支付)并从响应中获取授权ID。

以下是一个捕获授权的示例

$capture_data = array(
	'authorization_id' => '10V50318J8770814T',
	'currency'         => 'USD',
	'total'            => '7.47',
	'is_final_capture' => true
);

$response = $this->Paypal->captureAuthorization($data);
  • authorization_id 存储在 Paypal::creditCardPayment() 返回的响应对象中的 $response->transaction->authorization->id
示例捕获响应
{
   "id": "2E448764JU789501Y",
   "status": "completed",
   "created": "2014-01-12 10:57:30",
   "modified": "2014-01-12 10:57:42",
   "payment_method": null,
   "type": "capture",
   "payer": {
      "billing_address": {
         "line1": "",
         "line2": "",
         "city": "",
         "country_code": "",
         "postal_code": "",
         "state": ""
      },
      "credit_card": {
         "number": "",
         "type": "",
         "expire_month": "",
         "expire_year": "",
         "first_name": "",
         "last_name": ""
      },
      "id": "",
      "email": ""
   },
   "approval_url": "",
   "transaction": {
      "amount": {
         "total": "7.47",
         "currency": "USD"
      },
      "description": null,
      "sale": {
         "id": "",
         "parent_id": ""
      },
      "authorization": {
         "id": "",
         "created": ""
      }
   },
   "error": {
      "code": false
   }
}

取消授权

有各种理由可以取消授权(客户取消了交易,有些其他原因意味着你不应该向他们收费,等等)。

要取消授权,必须创建授权(将 $type 设置为 authorization 的信用卡支付)并从响应中获取授权ID。

以下是一个取消授权的示例

$data = array(
	'authorization_id' => '2073151243457584H'
);

$response = $this->Paypal->voidAuthorization($data);
  • authorization_id 存储在 Paypal::creditCardPayment() 返回的响应对象中的 $response->transaction->authorization->id
示例响应
{
   "id": "2073151243457584H",
   "status": "voided",
   "created": "2014-01-12 14:36:48",
   "modified": "2014-01-12 14:37:00",
   "payment_method": null,
   "type": null,
   "payer": {
      "billing_address": {
         "line1": "",
         "line2": "",
         "city": "",
         "country_code": "",
         "postal_code": "",
         "state": ""
      },
      "credit_card": {
         "number": "",
         "type": "",
         "expire_month": "",
         "expire_year": "",
         "first_name": "",
         "last_name": ""
      },
      "id": "",
      "email": ""
   },
   "approval_url": "",
   "transaction": {
      "amount": {
         "total": "7.47",
         "currency": "USD",
         "details": {
            "subtotal": "7.41",
            "tax": "0.03",
            "shipping": "0.03"
         }
      },
      "description": null,
      "sale": {
         "id": "",
         "parent_id": ""
      },
      "authorization": {
         "id": "",
         "created": ""
      }
   },
   "error": {
      "code": false
   }
}

退款销售

有时有必要退款。要退款销售,必须首先创建销售(将 $type 设置为 sale 的信用卡支付)并从响应中获取销售ID。

$data = array(
	'payment_id' => '3XX41928KR179661L',
	'currency'   => 'USD',
	'total'      => '7.47'
);

$response = $this->Paypal->refundPayment($data);
  • payment_id 存储在 Paypal::creditCardPayment() 返回的响应对象中的 $response->transaction->sale->id
示例响应
{
   "id": "7FN74449PP796325P",
   "status": "completed",
   "created": "2014-01-13 16:31:24",
   "modified": "2014-01-13 16:31:24",
   "payment_method": null,
   "type": "refund",
   "payer": {
      "billing_address": {
         "line1": "",
         "line2": "",
         "city": "",
         "country_code": "",
         "postal_code": "",
         "state": ""
      },
      "credit_card": {
         "number": "",
         "type": "",
         "expire_month": "",
         "expire_year": "",
         "first_name": "",
         "last_name": ""
      },
      "id": "",
      "email": ""
   },
   "approval_url": "",
   "transaction": {
      "amount": {
         "total": "7.47",
         "currency": "USD"
      },
      "description": null,
      "sale": {
         "id": "",
         "parent_id": ""
      },
      "authorization": {
         "id": "",
         "created": ""
      }
   },
   "error": {
      "code": false
   }
}